A script for converting images to different sizes inline, with support for server caching.
![]()
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:
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

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.
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 ?
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).Very easy to implement and use.
Thanks!!!!
Great script.. works great!!! #1
Thanks!!!!!
…better than phpThumb in my opinion.
Very nice system. Thanks.
Thanks for the script very handy and lightweight. Using it for our rubber stamps website!
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!
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/
Not sure how you’re listing out your files, but if you have them as an array you could sort them with sort($listOfFiles)
thanxs man..cool script…i used it…
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!
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.
This is a very good script…I am using it
hey! ty for sharing this.
It’s 3 AM and after 5 hours of searching, I have finally found this great script. Thanks a lot!!!!!!!!!!!!!!!!!!!!
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
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.
Fantastic script and its quality, Easy to use.
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?
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?
This works perfectly when the image is saved on my server but i am having problems when it’s remote? Any tips?
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.
I am a beginner in php. This script helps me a lot. Thanks.
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.
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.
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?
this is really a brilliant script and very easy to implement, many many thanks
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…
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
script, fast and everything ok, except not it works with remote pictures
works well with remote images?
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
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?
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?
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_ROOTto this:
$site_config['path_thumbnail']=$site_config['absolute_uri'].'/cache/'; //where to cache thumbnails on the server, relative to the DOCUMENT_ROOTCool 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?
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:
This will allow the thumb.php file to create the image files within the cache/images folder.
thx a lot, perfect script !!! works great
this script is great!
I will use it on future projects
THANK YOU !!!!
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.
very handy script indeed.
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.
just what i was looking for, thanks
perfect. exactly the script i was looking for. worked flawlessly the first time, everyone’s excited to start using it.
Thank you very much, this script is very simple but effective and exactly what I have been looking for.
Stam
Fantastic script and its quality, Easy to use.
Thank you very much for this script and it’s very easy to config too.
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.
why is tif not supported?
why do you not support .tif files?
Coool script..thanks alot…it helped meh alot..thanks soo much..keep up the good work..
I can Give width ..
it is now working thumb.php?file=./uploads/cc_articles/59&size=200&quality=100&nocache=NOCACHE
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
Many many thnx for this amazing script. Very easy to use, very good quality script
Thank you very much for the script
nice script very helpfull. thanks a lot