Create Image Thumbnails Using PHP
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 */ imagecopyresampled($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!
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 :)
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!
I prefer ImageMagick or GraphicsMagick over GD.
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.
As far as I have seen, imagecopyresampled gives far better results than imagecopyresized, altough it takes considerately more processing time.
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.
Yes, but generating them once is the better choice.
@Lewis: I agree with Fabian.
a step further to have use of it as asked David, how do you see the way to create a slideshow made with thumbnails from sites such as we can see on Chrome?
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.
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?
Thank you man! Nice piece of code! :)
I have seen “convert” used from ImageMagick with some success. The resulting image is pretty clean..
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
@Fabian Beiner: But The ImageMagick has problem with Safe_mode.It requiress this option to be off which is not secure.
@Fabian Beiner: But The ImageMagick has problem with Safe_mode.It requiress this option to be off which is not secure.
@Fabian Beiner:
If you want to create thumbnail images from a directory of folders, to a thumb folder, use this thumbnail generator script:
http://bgallz.org/270/php-create-thumbnail-images/
This one works well too, doesn’t overwrite.
And if you want to create thumbnails from a collection of urls ?
a solution to improve would be to either give a 4th param for the image type or auto-detect like gif,jpg,png
Hello use this
its so simple,if u have any queries mail at karthid@in.com
$ffmpeg = “ffmpeg Installed path”
$flvfile = “source video file with root path”
$png_path ” “Destination video file with root path and file type”
exec(“$ffmpeg -y -i $flvfile -vframes 1 -ss 00:01:60 -an -vcodec png -f rawvideo -s 110×90 $png_path”);
all the best….
Do you think it is possible to create a thumbnail of a web page with PHP ?
(for your info, actually I use a kind of C++ langage and ActiveX to create thumbnails of web pages, then send the thumbnails by FTP on the server and finally read them on the server with mootools/PHP … nothing very brilliant today)
Where do you specify quality in this code?
Hi guys!
No matter what I do, the image is garbled and I can’t decode it either. Any ideas?
But this code is only for jpeg. How to handle other image types ?
drthyutjuygfjityf
I dont think we can create thumbnail with php.
I suggest you add the following code after :
and the code is :
otherwise if the dimensions are not logically proportional you will get the error saying :
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in ….
Here is a more improved version of the function.
Terrific, just a little correction, this:
should be:
One very large important change to this script would be to change the function imagecopyresized to imagecopyresampled. The pixels will be resampled properly so you don’t get that garbled image. It works really quite well :D
Updated Chris, thank you!
working on this task
would like to remind about images with wrong extension,
found 1 way to check
http://www.php.net/manual/en/function.exif-imagetype.php
want to add this check to Agim Allkanjari script
dont promise to share :)
Thanks man!!! just what i wanted!! wasted much time creating thumbnails with gimp and photoshop, my work has now reduced greatly..will use it to create thumbnails for wallpapers for my website
http://www.joseblog.netau.net/wallpapers
ummm…sorry about the formatting of my post (forgot the code closing bracket, can’t edit or delete it:
Just curious where to stick the 100 quality?
Would it be here?
I also combined this with another script I found, which allows me to post all the images and thumbnails using a while loop so all I need to do is upload the images in the directory & it automatically creates the gallery using just the one echo:
i need the example code for creating thumbnail while uploading image using php. the image was not good to look. help
For all looking at how to set the quality, you do it like this
w00t, awesome addition. Thanks Jason!
Dear David,
I want to create thumbs of images but with specific image name as well. I have downloaded a script from this site: http://ashishrevar.com/2012/10/create-thumbnails-using-php-script/
I am using wordpress plugin next-gen gallery so, I want to resize images with predefined dimensions and filename(a pattern given by plugin).
Can you please help me for the same.
Here is the code that I have copied from the aforesaid site.
Create thumbnail image by php
When we upload large size images on server. Uploaded large images take more time to load on webpage, so we need to show small size images on our webpage. Image thumbnail is a solution to generate uploaded image’s thumbnail to show required size images on our website.
http://codelibrary.googleplus.co.in/create-thumbnail-image-by-php/
Hi,
i created a php class for thumbnail. It’s very easy.
you can download from this link : https://github.com/selahattinunlu/createThumbnail.class
Using Manual:
Control: upload
Result:
thanks lot
i have some error like this,
Message: imagejpeg(http://localhost/ots-tc/trunk/code/hive_tm/attachments/thumb/): failed to open stream: HTTP wrapper does not support writeable connections
kindly help me..
I am using this on IIS Server but its not working. I have bundled (2.0 compatible) enabled. Its giving blank screen. so can you help me please?
yes this is a simple way of creating thumbnails using GD2 library functionality.
This works for me for gif,jpg,png
Hi
I use this function, its very good thank you, But I have a problem , When images number is near 1000 it does not work and break, log on apache:
[Tue Nov 26 18:48:39.271267 2019] [php7:error] [pid 25563] [client 213.233.171.85:38342] PHP Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/html/config.php on line 162, referer: http://37.191.64.67:1894/report.php
line 162 i imagecopyresampled