troutnut Posted March 20, 2005 Share Posted March 20, 2005 I'm using PHP to write a new content management system for my website,which includes thousands of images. I need to automatically generatethumbnails in at least three sizes from the original image. I knowthe basic way to do it with PHP and I've got it working. My questionis, are there any better ways to do it to produce higher-qualityphotos? Would I be better off writing a custom resizing functionbased on an algorithm I can find somewhere? I see so much detailabout various ways to resize images for the best quality resampling inPhotoshop... so surely there's a way to do better than PHP's defaultalgorithm. Link to comment Share on other sites More sharing options...
ky2 Posted March 20, 2005 Share Posted March 20, 2005 Photoshop's algorithms aren't the best eiter. You could import an external library and reuse it's functionality to do this. These range in $20-$30 in price and are much more powerful. They don't support all OS/platforms thought. Link to comment Share on other sites More sharing options...
peter_witkop Posted March 20, 2005 Share Posted March 20, 2005 Making thumbnails you're downsizing an image, which is much easier to do well than up-sizing, easier to throw away information than create it. Esspecially for web sized images, the php standard functions should work just fine, just make sure you're using true color images, which isn't the default, you'll be fine. If you were automating preping files for printing, you might want something with better options than php offers, but for what you're doing, php's image handling is probably the best way to go. Peter Link to comment Share on other sites More sharing options...
rene_nederhand Posted March 20, 2005 Share Posted March 20, 2005 Why don't you use ImageMagick for this. It's fast, it's command line, it's free and it's high quality. Photoshop will make the best thumbnails though. So you could make an PS action for it. Link to comment Share on other sites More sharing options...
perfect_exposure Posted March 20, 2005 Share Posted March 20, 2005 The answer really depends on who has control of software installed on your webserver. I,m not sure what you mean by PHP's basic way. You will likely already have either GD1, GD2 or ImageMagick installed on your webserver. GD1 image functions are not very good and thumbnails produced are bad. GD2 is much better and will produce reasonable thumbnails. Imagemagick is reputed to have very good algorithms and especially the resizing algorithms which are better than photoshop. However you have to work out how to use them. phpinfo() should show you which of the above are available. If its GD1 or GD2 then search the php manual for "image" which gives a big list of functions you can use to manipulate images. If its only GD1 then your're unlucky and should try to either update to GD2 or ImageMagick. Link to comment Share on other sites More sharing options...
perfect_exposure Posted March 20, 2005 Share Posted March 20, 2005 p.s. I'm assuming your are using php on apache. Link to comment Share on other sites More sharing options...
perfect_exposure Posted March 21, 2005 Share Posted March 21, 2005 The following php code will do what you want but it requires GD2 library to be available. You only need to change the first 2 lines unless you want to base thumbnail size on standard width instead of standard height. Note that using this to dynamically do it at user run time would seriously slow down your website so do it when you upload images and create static links to thumbnails.<br> <br> $my_input_file = 'path/to/source.jpg';<br> $my_output_file = 'path/to/destination.jpg';<br> <br> // set output jpeg quality<br> $jpeg_quality = 80;<br> <br> // set thumbnail height to use<br> $thumb_height = 100;<br> <br> $size = getimagesize($my_input_file);<br> // $size[0] = the width of input image : $size[1] = the height of input image<br> <br> // calculate thumbnail width based on ratio of width / height of input image<br> $thumb_width = ($size[0] / $size[1]) * $thumb_height; <br> <br> // create image in memory<br> $src_img = imagecreatefromjpeg($my_input_file);<br> $dst_img = imagecreatetruecolor($thumb_width,$thumb_height);<br> imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1]);<br> <br> // write output image<br> imagejpeg($dst_img, $my_output_file, $jpeg_quality);<br> <br> // free memory used<br> imagedestroy($src_img);<br> imagedestroy($dst_img);<br> Link to comment Share on other sites More sharing options...
bill_tuthill Posted March 21, 2005 Share Posted March 21, 2005 Do you know what algorithm(s) GD2 uses? I know nothing about it. <A HREF="http://www.photo.net/bboard/q-and-a-fetch-msg?msg_id=00BLh5&unified_p=1">Here is a thread discussing downsampling</A> with ImageMagick and other methods. Bart van der Wolf studied this and determined that you can't do much better than Lanczos filter with post-sharpening. Link to comment Share on other sites More sharing options...
perfect_exposure Posted March 21, 2005 Share Posted March 21, 2005 I don't know and I couldn't find a USM function either but the code as posted produces acceptable thumbnails but not very sharp. Link to comment Share on other sites More sharing options...
jeff_helm Posted September 5, 2006 Share Posted September 5, 2006 The code as posted? Hahahah of course they're not sharp. Up the $jpeg_quality to 100 if quality is your goal. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now