Jump to content

Generating quality thumbnails with PHP


Recommended Posts

I'm using PHP to write a new content management system for my website,

which includes thousands of images. I need to automatically generate

thumbnails in at least three sizes from the original image. I know

the basic way to do it with PHP and I've got it working. My question

is, are there any better ways to do it to produce higher-quality

photos? Would I be better off writing a custom resizing function

based on an algorithm I can find somewhere? I see so much detail

about various ways to resize images for the best quality resampling in

Photoshop... so surely there's a way to do better than PHP's default

algorithm.

Link to comment
Share on other sites

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

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

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

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

  • 1 year later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...