Image Resize script php

php-sniplets Add comments

Image Resize

You will be able to use this function to generate html “width/height” for use with showing a thumbnail image
using the original image. This will help save in making duplicate images merely for display as a thumbnail.

It takes only two arguments, the path to where the image is stored and the wanted width or height.

/** resize_image()
*
* This functions resizes an image to any size
* defined.
*
* @ARGUMENTS: 2
*                         – $image: This is the path to the image to be resized
*                         – $target: This is the target width/height
* @RETURNS:
*                            – htm width/height string for an image tag
**/
function resize_image($image,$target)
{
// Get the actual image size
$image = getimagesize($image);

// Get the percent to size the image
if ($image[0] > $image[1])
{
$percent = $target / $image[0];
}
else
{
$percent = $target / $image[1];
}

// Get the new sizet
$w = round($image[0] * $percent);
$h = round($image[1] * $percent);

// Generate as an htm widht/height property
$htmWH = “width=\”" . $w . “\” height=\”" . $h . “\”";
return $htmWH;
}

This way won’t decrease download time if you’re resizing a huge image. This is just telling the web browser to
show the image at a certain size when the image is downloaded.

Leave a Reply

You must be logged in to post a comment.

Impressum Entries RSS Log in