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 */
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!
Comments
Be Heard!
Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!
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