PHP Thumbnail

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

Bookmark and Share

13 Comments

paul wrote on June 22nd, 2008 at 12:15 pm:
y didnt u use perl
Anonymous wrote on July 22nd, 2008 at 7:15 pm:
because php is cooler
John wrote on July 23rd, 2008 at 7:23 pm:
This is a nice php class that I use in many of my projects.

http://www.redvodkajelly.com/code/php-image-resize-class/
Herb wrote on July 23rd, 2008 at 10:30 pm:
Neither 'y' nor 'u' are words. Stop butchering the language and learn to type faster.

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:
Haha, didn't realize that is the same paul from his about page. Kind of makes me wonder about the author of the blog :\
Shane wrote on July 24th, 2008 at 1:06 pm:
Nice work on the image resizing. PHP rocks.
Jifbo wrote on July 24th, 2008 at 2:24 pm:
I created something similar in making my Band Website... Almost the same actually. I agree with writing your own stuff for a sense of achievement :) Nice one dude!
Allan Wirth wrote on July 25th, 2008 at 6:04 pm:
@Herb. He was joking around, he's not really like that.
PHP Thumbnail Function my.bookmarks wrote on July 26th, 2008 at 2:01 am:
Pingback from http://jryj.com/php-thumbnail-function-141/
Nico wrote on July 28th, 2008 at 8:01 am:
Nice script! I've made a similar one a while back, but I also included storing the thumbnail to disk. The thumbnail creation script would then only have to run once for each image. The next page load would just find the thumbnail image on the disk and show it right away. On high traffic sites with lots of thumbnails this can save you quite some processing time.
Allan Wirth wrote on September 9th, 2008 at 3:16 pm:
Nico, I use a similar method, and save all the thumbnails to the disk. This isn't included in the function to make it more multi-purpose.
Paul wrote on September 13th, 2008 at 3:33 pm:
credibility--;
Lucas wrote on September 19th, 2008 at 2:27 pm:
Script looks good, site looks REALLY RED.

Post a Comment: