Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Create Image Thumbnails Using PHP

18 Responses »

I create a lot of photo galleries for my customers. A website is meant to be a marketing tool and what better way to market your product than showing numerous pictures of the product? One issue that comes up often is that the customer doesn't have a means for creating their own thumbnails. That problem is compounded by the fact that they don't want to pay me to create them. The compromise is the PHP thumbnail generation function I created.

The PHP

function make_thumb($src,$dest,$desired_width)
{

	/* read the source image */
	$source_image = imagecreatefromjpeg($src);
	$width = imagesx($source_image);
	$height = imagesy($source_image);
	
	/* find the "desired height" of this thumbnail, relative to the desired width  */
	$desired_height = floor($height*($desired_width/$width));
	
	/* create a new, "virtual" image */
	$virtual_image = imagecreatetruecolor($desired_width,$desired_height);
	
	/* copy source image at a resized size */
	imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
	
	/* create the physical thumbnail image to its destination */
	imagejpeg($virtual_image,$dest);
}

The above code uses the PHP GD2 library functionality. The only drawback to using PHP for image creation is that the thumbnails don't look as good as thumbnails created in Photoshop or GIMP.

Would you have use for this? Suggestions? Share them!

Discussion

  1. daniele
    September 29, 2008 @ 10:36 am

    You can use a quality parameter in the imagejpeg function!
    I always set to 100% and it seems working quite good… don’t forget it’s a thumb :)

  2. miguel palazzo
    September 29, 2008 @ 11:24 am

    There are many ways to do this David :) It is useful if you’re having a CMS or a proper frontend module to upload certain amount of photos and such to create its thumbnail. Best thing about GD2 it’s well, you can do almost everything on an image w/o using Photoshop or GIMP, worst thing is… I dunno you guys but, size does get increased, no compression methods i guess!

  3. fabian beiner
    September 29, 2008 @ 12:29 pm

    I prefer ImageMagick or GraphicsMagick over GD.

  4. September 29, 2008 @ 3:22 pm

    I agree with Danielle that you should include the quality parameter. I generally use 83 for any resizing I do. Why 83? It works out to be about equal (in image quality and filesize) to Photoshop’s Save For Web using JPG at 60 quality. Using 100 at quality works even better if you’re not worried about file size or load time with lots of thumbnails.

  5. September 30, 2008 @ 12:41 am

    As far as I have seen, imagecopyresampled gives far better results than imagecopyresized, altough it takes considerately more processing time.

  6. October 1, 2008 @ 6:04 am

    Am I right in thinking this script creates a physical file for the thumbnail? I’ve always generated them on the fly using a header directive to make the browser render the PHP file as an image.

  7. fabian beiner
    October 1, 2008 @ 6:21 am

    Yes, but generating them once is the better choice.

  8. October 1, 2008 @ 7:45 am

    @Lewis: I agree with Fabian.

  9. October 1, 2008 @ 12:49 pm

    As good as it feels to code your own functions, it’s good to know that there’s someone who’s gone to great lengths to code a spectacular class.
    phpThumb (phpthumb.sourceforge.net) is fantastic, easy to implement and very flexible.

  10. October 1, 2008 @ 6:30 pm

    I guess on large scale sites creating a thumbnail file makes a lot of sense, even if it is one more thing to keep track off (in terms of CRUD).

    My one concern is if the desired size of thumbnail changes. For example on one of the sites I developed some of the same image files are displayed on different pages at different sizes. For example, I have one gallery system that shows all the images in that gallery at 120px width. On another page random images from that gallery are shown at 400px. At the moment there is one copy of the file on the server and the iamges are resized depending on the requested page.

    Should I create thumbnail files for use in the gallery and generate the random image on the fly, or should I look at creating a third set of images scaled to 400px?

  11. March 25, 2009 @ 8:54 pm

    Thank you man! Nice piece of code! :)

  12. steve
    July 9, 2009 @ 12:15 am

    I have seen “convert” used from ImageMagick with some success. The resulting image is pretty clean..

  13. September 28, 2009 @ 6:35 am

    In the past I was using phpthumb. But nowadays my host server disabled popen and system function, which causing the phpthumb script cannot working properly.

    This might be a solution for me.
    Thank’s

  14. hamideh
    October 18, 2009 @ 3:40 am

    @Fabian Beiner: But The ImageMagick has problem with Safe_mode.It requiress this option to be off which is not secure.

  15. October 18, 2009 @ 3:41 am

    @Fabian Beiner: But The ImageMagick has problem with Safe_mode.It requiress this option to be off which is not secure.

    @Fabian Beiner:

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!