PHP Thumbnail
Written by Allan Wirth on June 20th, 2008 at 10:18 pm
While writing the backend to this blog, I came in need of a simple function for creating thumbnails in PHP. Because I like to use mostly code that I wrote myself in my projects (it gives me a sense of accomplishment), I went about creating a simple function called imagethumbnail that takes a GD image resource and returns another GD image resource that is the appropriate size.
Here it is:
function imagethumbnail($image, $targetsize = 150, $enlarge = False) {
$width = imagesx($image);
$height = imagesy($image);
if ($width > $targetsize || $height > $targetsize || $enlarge == True) {
if ($width > $height) {
$newx = $targetsize;
$newy = round($height*($newx/$width));
} else if ($height > $width) {
$newy = $targetsize;
$newx = round($width*($newy/$height));
} else if ($height == $width) {
$newx = $targetsize;
$newy = $targetsize;
}
$new = imagecreatetruecolor($newx, $newy);
imagecopyresized($new, $image, 0,0,0,0,$newx,$newy,$width,$height);
} else {
$new = $image;
}
return $new;
}
It takes three arguments, $image, $targetsize, and $enlarge.
$image - the GD image to make a thumbnail out of.
$targetsize - the maximum length of any side of the final thumbnail.
$enlarge - whether or not to enlarge the image if it is smaller than the specified thumbnail size.
Here is a line by line explanation of the function.
function imagethumbnail($image, $targetsize = 150, $enlarge = False) {
$width = imagesx($image);
$height = imagesy($image);
This declares our function and then sets the variables $width and $height to the width and height of our image, respectively.
if ($width > $targetsize || $height > $targetsize || $enlarge == True) {
Checks whether or not a resize is even necessary. If any of the sides are longer than the target size, or if $enlarge is true does it go on to resize the image.
if ($width > $height) {
$newx = $targetsize;
$newy = round($height*($newx/$width));
If the width is greater than the height, the new width is the maximum for one side and then new height is the old height times the ratio of the new width to the old width so that the new height is proportionate to the new width.
} else if ($height > $width) {
$newy = $targetsize;
$newx = round($width*($newy/$height));
The same as the last section except height and width are switched.
} else if ($height == $width) {
$newx = $targetsize;
$newy = $targetsize;
}
If the width equals the height then the image is square and we can set both the width and the height to the maximum.
$new = imagecreatetruecolor($newx, $newy);
This creates a new GD image resource with the width and height that we calculated earlier for us to put the thumbnail in.
imagecopyresized($new, $image, 0,0,0,0,$newx,$newy,$width,$height);
Finally, we copy a resized version of the original image into the thumbnail.
} else {
$new = $image;
}
If the image doesn't need to be resized then the thumbnail is merely a copy of the original image.
return $new;
}
Finally, we return the thumbnail.
I hope that this function shows how useful the GD library is!
Tags: Programming,GD,PHP,Scripts

13 Comments
paul wrote on June 22nd, 2008 at 12:15 pm:
Anonymous wrote on July 22nd, 2008 at 7:15 pm:
John wrote on July 23rd, 2008 at 7:23 pm:
http://www.redvodkajelly.com/code/php-image-resize-class/
Herb wrote on July 23rd, 2008 at 10:30 pm:
He did not use perl because he thought php would be better suited. (Or he does not know perl. Or he is trying to learn php. Or both, no wait, or all three).
Herb wrote on July 23rd, 2008 at 10:31 pm:
Shane wrote on July 24th, 2008 at 1:06 pm:
Jifbo wrote on July 24th, 2008 at 2:24 pm:
Allan Wirth wrote on July 25th, 2008 at 6:04 pm:
PHP Thumbnail Function my.bookmarks wrote on July 26th, 2008 at 2:01 am:
Nico wrote on July 28th, 2008 at 8:01 am:
Allan Wirth wrote on September 9th, 2008 at 3:16 pm:
Paul wrote on September 13th, 2008 at 3:33 pm:
Lucas wrote on September 19th, 2008 at 2:27 pm: