PHP Thumbnail Script

A script for converting images to different sizes inline, with support for server caching.


/images/sample.jpg /images/sample.jpg /images/sample.jpg images/sample.jpg

Very useful for creating multiple sizes of image urls from single images. Creates a thumbnail image depending on variables passed to it via get.

Images are cached on the server, so server processing overhead is only needed for the first time the script runs on a particular image.

Usage:

<img src="thumb.php?file=FILE&size=SIZE"> - to size the thumbnail  to fit a square

<img src="thumb.php?file=FILE&sizex=SIZEX&sizey=SIZEY"> - to  size the thumbnail to fit a rectangle

<img src="thumb.php?file=FILE&size=SIZE&quality=QUALITY&nocache=NOCACHE"> -  to set a thumbnails quality 

Where:

  • FILE = the file to retrieve
  • SIZE = the maximum size of the thumbnail in pixels
  • SIZEX = the maximum width of the thumbnail (height adjusted accordingly)
  • SIZEY = the maximum height of the thumbnail (width adjusted accordingly)
  • QUALITY = an integer from 0-100 specifying the resulting jpeg quality of the image
  • NOCACHE = an integer 1 or 0. If set to 1, the cached thumbnail is deleted and recreated (use if the source image changes)

Features:

  • Server caching of images
  • Creation of jpeg thumbnails inline, on the fly
  • Can be used for non-local images, if allow_fopen_url in the php configuration is set to true

Requires:

GD Library

Installation:

The script should work with GD versions 1 and 2, and Linux and Windows servers

Licensed under the GNU Lesser General Public License as published by the Free Software Foundation.
Copyright © 2005 Chris Tomlinson.

Download:

Download thumb.php v1.1

Version Changes:

Changes:
0.1 – first release
0.2 – converted cache thumbnail from png to jpeg
0.3 – fixed error where files weren’t being cached properly
0.4 – allowed non local urls (if allow_url_fopen is on), quality and nocache switches
0.5 – allowed maximum x and y settings (for scaling images to fit non square sizes)
0.6 – allowed tagging of images (with the get query placing the text in the bottom left hand corner of the image)
0.7 – fixed gd_info error for php<4.3
0.8 – added gif support (for gd 2.0.28)
0.9 – now supports native outputting of png, jpg and gif formats
1.0 – doesn’t fail if the cache file can’t be created
1.1 – removed a few more notices

60 Comments »

  1. dmsims says:

    Great script – very easy to implement and options if you need them (and quick as well)

    Thank you very much

  2. PsychoFisch says:

    OMG!
    I love you! xD
    I´ve tried about 10 different thumbnail-scripts and yours is the first that actually worked!
    And it is just one file!
    Very nice work!
    Thank you! =D

  3. Jan says:

    My script wasn’t working anymore after my hosting upgraded to PHP 5.4

    To fix this issue just change following line:

    imagejpeg($thumbnail,”,$quality);
    =>
    imagejpeg($thumbnail,NULL,$quality);

    http://download.hkvstore.com/forums/viewtopic.php?f=18&t=27045

    Apparently in PHP 5.4 empty strings as filenames are not valid.

  4. sebastian says:

    On very large image the thumbnails does not show ..
    works fine for image below 4Mb but a HD Image lets say 10mb 2000px by 3000px no thumbnails… do you have a fix ?

    • Christo says:

      Try changing your memory size. You can do this with

      ini_set('memory_limit', '1024M');

      Depending on the speed of your server, you might also need to change the php script execution time limit with

      ini_set('max_execution_time', 0); (this will run the script forever until it finishes).

  5. Miro Hristov says:

    Very easy to implement and use.

  6. B00Mer says:

    Thanks!!!!
    Great script.. works great!!! #1

  7. Max says:

    Thanks!!!!!
    …better than phpThumb in my opinion.

  8. Antony says:

    Very nice system. Thanks.

  9. Mike Lo says:

    Thanks for the script very handy and lightweight. Using it for our rubber stamps website!

  10. Otso says:

    Actually i’m not listing the files. The thumb.php file is in the same folder as the .jpg files
    and i have in the index.php next php script:

    <?php
    // define directory path
    $dir = "kuvat/websites";

    // iterate through files
    // look for JPEGs
    if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
    if (preg_match("/.jpg/", $file)) {

    // get thumbnails
    // link to full image
    echo "
    “;

    }
    }
    closedir($dh);
    }
    }
    ?>

    I got also another question:
    Should i see the cached thumbnail files in the server?
    I set the thumbnail cache folder like this:
    $site_config['path_thumbnail']=$site_config['absolute_uri'].’/qqq/’; //where to cache thumbnails on the server, relative to the DOCUMENT_ROOT

    So do I have to create the “qqq” folder inside the folder where thumb.php and pictures are located?
    Do I have to alter any other part of the code to get the cache work?

    Thanks a millions in advance! :)

  11. Otso says:

    Hi!

    This is awesome script, so simple and powerful.
    I’m using this script in my website to generate thumbnails for my image gallery. The only thing that is not working right is image sorting. How can I make the script to sort the images by filename? The website is http://www.contentunion.net/otsokaukomies/

    • Christo says:

      Not sure how you’re listing out your files, but if you have them as an array you could sort them with sort($listOfFiles)

  12. kapil says:

    thanxs man..cool script…i used it…

  13. Numan says:

    Hi
    My folder structure is ‘C:\xampp\htdocs\thumb\’ and my image folder is ‘C:\xampp\htdocs\thumb\cache\images’. I call this code ” on my view.php page but not showing any image. Please help me!

  14. konaya says:

    I had the same problem as Lousis L. The problem was human error, hehe… “Relative to DOCUMENT_ROOT” I took to mean “relative to the document root”, but it really means “relative to wherever you stuck this php script”. And that’s kinda logical, so my bad.

  15. subbu says:

    This is a very good script…I am using it

  16. Selmanh says:

    hey! ty for sharing this.

  17. Mark says:

    It’s 3 AM and after 5 hours of searching, I have finally found this great script. Thanks a lot!!!!!!!!!!!!!!!!!!!!

  18. Chris says:

    Hi…have been looking for something like this for ages, but am struggling to get it working.

    I have uploaded the thumb.php script to my site, and have used the following line to try and use the script…

    img src=”/templates/communitypolls/default2/thumb.php?file=/communitypolls/images/20110120185138-DSC_0412.JPG&size=100″

    I get the following error reported in firebug…

    Image corrupt or truncated: http://www.mysite.com/templates/communitypolls/default2/thumb.php?file=/communitypolls/images/20110120185138-DSC_0412.JPG&size=100

    I’m no expert and am probably making a basic mistake…would be very grateful for your help.

    Many thanks,

    Chris.

    PS Sorry, missed my script line from first posting

  19. Chris says:

    Hi…have been looking for something like this for ages, but am struggling to get it working.

    I have uploaded the thumb.php script to my site, and have used the following line to try and use the script…

    I get the following error reported in firebug…

    Image corrupt or truncated: http://www.mysite.com/templates/communitypolls/default2/thumb.php?file=/communitypolls/images/20110120185138-DSC_0412.JPG&size=100

    I’m no expert and am probably making a basic mistake…would be very grateful for your help.

    Many thanks,

    Chris.

  20. arun prashar says:

    Fantastic script and its quality, Easy to use.

  21. Wes Cargill says:

    One more question…is it possible to prevent the upscaling of images if they are smaller than the x and y dimensions passed through the URL?

  22. Wes Cargill says:

    A very handy script and easy to use too. One problem I am having is my generated thumbnails of transparent gifs have black backgrounds. Any thoughts?

  23. JB says:

    This works perfectly when the image is saved on my server but i am having problems when it’s remote? Any tips?

    • Christo says:

      You will need to ensure that safe mode is not turned on on your server otherwise your script won’t be able to access files on another url. Another way to test this is to run something like
      echo file_get_contents("http://domain.com/image.jpg");
      If something gets returned, then you should be able to retrieve external images.

  24. siva says:

    I am a beginner in php. This script helps me a lot. Thanks.

  25. thanks dude, u saved my life. i was using another script to create the thumbnails on the fly and was getting an “image truncated” error. no matter how i googled, i couldnt find the solution. i decided to look for another script and ur script was at the top of the google search. it worked! thanks again.

  26. Luke Oliff says:

    Excellent script, very well writen and extremely easy to implement. Does what it says on the tin, with no bells and whistles to fall off.

  27. Nathan Bunn says:

    I have tried the script on my local server and have found that sizex works on its own but sizey doesn’t. I am using RockMelt (Chrome 10 offspring). Is there a glitch in the script?

  28. anant says:

    this is really a brilliant script and very easy to implement, many many thanks

  29. Jake says:

    Cool! However I can’t get sizey to work. sizex works fine, and so does size. But Y doesn’t do anything, all it does is default to the smallest size available…

  30. This is a brilliant script. I have used it to great effect in my website about the solar power array on our house. All the thumbnails for the many graphs on the site are generated painlessly with this script. It’s a brilliant, brilliant way around the many problems of creating and storing and addressing thumbnails.

    –Julian

  31. Chema says:

    script, fast and everything ok, except not it works with remote pictures

    works well with remote images?

  32. Chris says:

    Hey, thanks for the script. Very useful.
    I was just wondering how to stop it renaming files.
    If I upload a picture “photo1.jpg” I’d like photo1.jpg put into the images and thumbs folder without the name being changed. Any ideas on how I could mod the script to do this?
    Any help is appreciated.
    Thanks

  33. Louis L says:

    Thanks for your quick turnaround Christo. I’m guilty of not giving you all the information on the first try. I tried your script on local servers (SuSe & WAMP) and it worked fine – generating AND saving the thumbnails. It worked irrespective of the folder name I specified in thumb.php – as long as the folder existed on the web site. When I pushed the implementation out to an ISP I use, everything still works – except the saving of the thumbnails. It’s not a GD or GD version issue, because the thumbnails are generated.

    As it is, the on-the-fly thumbnail generation every time isn’t a huge problem, but I can see it becoming a resource hog as images and visitors increase.

    Have you ever implemented this using a full URL, e.g. http://www.mydomain.ext/myphotofolder – instead of a relative path?

  34. Louis L says:

    Thanks for a wonderfully simple script. It was a breeze to implement and works like a charm. I do have one little problem. Seems that no matter what permissions I set on my cache folder, the thumbnails aren’t being cached. They’re created and displayed fine – but no caching. Is it possible that my ISP has disabled caching on the server?

    • Christo says:

      Try changing the location of the cache folder in the thumb.php script. By default it looks for a folder called ‘images’ inside a ‘cache’ folder in the top level of your website. Eg. if you just have a ‘cache’ folder, try changing this:
      $site_config['path_thumbnail']=$site_config['absolute_uri'].'/cache/images/'; //where to cache thumbnails on the server, relative to the DOCUMENT_ROOT
      to this:
      $site_config['path_thumbnail']=$site_config['absolute_uri'].'/cache/'; //where to cache thumbnails on the server, relative to the DOCUMENT_ROOT

  35. Jubydul Haque says:

    Cool script, it helps me alot. Thanks. But there is a bit problem i face when used it in a linux based server. Is there any trick to get rid of this problem?

    • Christo says:

      What is the nature of the problem you’re having? It may be a permission problem: the cache folder that stores the images needs to be writeable by the webserver. This can be achieved by running the following command from the root of your website:

      chmod -R 777 cache/images

      This will allow the thumb.php file to create the image files within the cache/images folder.

  36. marduk says:

    thx a lot, perfect script !!! works great

  37. classifieds says:

    this script is great!
    I will use it on future projects
    THANK YOU !!!!

  38. Shawpy says:

    How can you make this script create a thumbnail and then set the thumbnail onto a sqaure black background? (for vertical centering purposes on an image gallery) Also what is the server load creating these thumbnails on the fly each time a person visits the page? I have a lot of visitors to the site I am using it on.

  39. daniel says:

    very handy script indeed.

  40. Teacher says:

    Amazingly simple script. I fought with another one for hours and gave up. I got this one to work perfectly in 5 minutes. Everything should be this easy.

  41. dave e says:

    just what i was looking for, thanks

  42. fluffy says:

    perfect. exactly the script i was looking for. worked flawlessly the first time, everyone’s excited to start using it.

  43. Thank you very much, this script is very simple but effective and exactly what I have been looking for.

    Stam

  44. Amir Aziz says:

    Fantastic script and its quality, Easy to use.

  45. Ooh! says:

    Thank you very much for this script and it’s very easy to config too.

  46. Hi.

    Excelent script, i made some changes because the size prameter for square images didn’t work, so if you want the new version let me know.

    Ricardo from Dominican Republic.

  47. Daniel says:

    why is tif not supported?

  48. Daniel says:

    why do you not support .tif files?

  49. suthann says:

    Coool script..thanks alot…it helped meh alot..thanks soo much..keep up the good work..

  50. MRT says:

    I can Give width ..
    it is now working thumb.php?file=./uploads/cc_articles/59&size=200&quality=100&nocache=NOCACHE

  51. chris says:

    Great script! I made a small modification to send back a 304 Not Modified header to make use of client-side caching. It definitely sped things up for me! Thanks! chris

  52. Many many thnx for this amazing script. Very easy to use, very good quality script

  53. MM says:

    Thank you very much for the script

  54. satish says:

    nice script very helpfull. thanks a lot

Leave a Reply

Isometric Projection with JavaScript in HTML5 vs Flash and Actionscript

Recently I decided to build part of a game that used an isometric projection for displaying the game board using HTML5.
Dirty plagiarist that I am, I found an excellent (albeit very old) tutorial by Danko Kozar for doing this. However it was written in Actionscript. No bother – I just modified the code to work in HTML5. This meant I could more or less ignore the theory of how isometric projection works and just get down to the business of doing it. Porting the code was easy. As it says in the tutorial:

These tutorials can be used for programming an isometric game in many programming languages not just in Flash. They are more about game programming than about Flash programming.

Here’s the swf from Danko’s tutorial:
Details »

Dynamic Leaderboard

So you want to create a dynamic leaderboard that changes on the fly using jQuery? Well do ya?

Here’s the scenario: you have some cool (to you at least) data that you want to put up on a webpage/kiosk/touchscreen/smartphone that animates beautifully in real time to update the results.

Details »

Handy Linux Shortcuts and How To’s

Here’s some linux tricks I use in my line of work. I find them useful, you may not. You decide.
Details »

Friend Photomosaics


Turn your Facebook profile picture into a photomosaic made up of your friends profile pictures.

Details »

PHP Chart Class

Create line or bar charts and graphs within php. They can be output directly or image files created.

Details »

Drawing Gallery

Create your own drawing

Details »

Create Flash Drawing

A collection of many spontaneous drawings generated with Flash

Over a thousand random drawings created since 2003.

Details »

Font List


Details »

PHP Thumbnail Script

A script for converting images to different sizes inline, with support for server caching.

Details »

Flash Chess

Chess program designed to learn from player interaction. Made with flash and PHP server technology, the finished version will (in theory) be able to beat all players that aren’t as good as players that have played the system often.

Details »