getButterfly Logo getButterfly

Ever since I discovered PHP I tried to simplify everything by splitting files into most used parts and using include() to add them, reading files from directories and generating galleries on the fly, or by using many tricks to make my developer life easier.

Today, I’ll show you how to use PHP and GD library to show perfectly cropped thumbnails for your large images. This script even creates a cache folder, so the next time a user visits your site, the images will be retrieved automatically.

I’m using a script called Smart Image Resizer, the current version being 1.4.1. You can download it from the author’s page.

Each time you need to display a thumbnail, you call the image.php script like this:

[xhtml]<a href=”images/image.jpg”><img src=”image.php/image.jpg?width=75&amp;height=75&amp;cropratio=1:1&amp;quality=100&amp;image=” alt=”” /></a>[/xhtml]

You need to use a full path for the image parameter. The other parameters are self-explanatory. My example will show a square thumbnail, 75px*75px, and a quality of 100 out of 100. The cropratio parameter tells the script to generate a square thumbnail by cropping the wider sides.

That’s it! Good luck.

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

3 thoughts on “PHP Smart Image Resizing – Thumbnails On The Fly

  1. I am using phpThumb on a website of mine. I chose this one because of 5 main reasons:

    1. it uses cache
    2. it can output 5 formats: jpg, gif, png, bmp, ico
    3. phpThumb’s basic features will work even if the server has no GD support, as long as ImageMagick is installed
    4. It can apply (if necessary) a non-permanent watermark on any picture
    5. A number of filters can be applied (most require GD v2.0+ and/or PHP v4.3+, or ImageMagick)

    I hope that maybe someday this information will be good for anyone who reads your blog constantly ;)
    .-= Specificatii motociclete´s last blog ..Polaris Sportsman 800 EFI =-.

  2. Hi,
    How would I save the thumbnail to a web server as opposed to displaying on screen?
    (apologies if this is a daft question!)

  3. This script generates thumbnails on-the-fly. You may be able to save them by using some PHP copying function and copying the thumbs to a directory. I can’t help you with a script like that. Not my area of expertise.

Leave a Reply

Your email address will not be published. Required fields are marked *