One of the great things about php is its built in support for the gd image library which makes creating thumbnails from images quite trivial, although you still need to calculate the correct dimensions if you want your thumbs to be resized proportionally.
Here’s a function that takes a gd image, a maximum width and / or maximum height and returns a new thumbnail image that fits into these sizes. The function can either scale the image proportionally so the whole image fits completely into the new size, or scale and crop the original so that the thumbnail is exactly the maximum width and height.
If either the maximum width or maximum height is 0 or null then the function calculates these proportionally which is useful if you want all thumbnails a certain width and height and don’t care about the other dimension.
You can also specify an rgb background colour which will force the thumbnail to be exactly $max_width x $max_height with any space at the left and right (or top and bottom depending on whether the source image is portrait or landscape) filled in with the background colour.
I’ve written a couple of scripts to show usage of this function below – one creates thumbnails for every image in the same directory as the script, then generates html to show these. The other extracts images from a zip archive through a standard form upload and creates and returns a zip archive containing generated thumbnails.
You can copy and paste the code, or download the zip file containing all three scripts.
paGdThumbnail.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<?php function paGdThumbnail($image, $max_width, $max_height, $method = 'scale', $bgColour = null) { // get the current dimensions of the image $src_width = imagesx($image); $src_height = imagesy($image); // if either max_width or max_height are 0 or null then calculate it proportionally if( !$max_width ){ $max_width = $src_width / ($src_height / $max_height); } elseif( !$max_height ){ $max_height = $src_height / ($src_width / $max_width); } // initialize some variables $thumb_x = $thumb_y = 0; // offset into thumbination image // if scaling the image calculate the dest width and height $dx = $src_width / $max_width; $dy = $src_height / $max_height; if( $method == 'scale' ){ $d = max($dx,$dy); } // otherwise assume cropping image else{ $d = min($dx, $dy); } $new_width = $src_width / $d; $new_height = $src_height / $d; // sanity check to make sure neither is zero $new_width = max(1,$new_width); $new_height = max(1,$new_height); $thumb_width = min($max_width, $new_width); $thumb_height = min($max_height, $new_height); // if bgColour is an array of rgb values, then we will always create a thumbnail image of exactly // max_width x max_height if( is_array($bgColour) ){ $thumb_width = $max_width; $thumb_height = $max_height; $thumb_x = ($thumb_width - $new_width) / 2; $thumb_y = ($thumb_height - $new_height) / 2; } else{ $thumb_x = ($thumb_width - $new_width) / 2; $thumb_y = ($thumb_height - $new_height) / 2; } // create a new image to hold the thumbnail $thumb = imagecreatetruecolor($thumb_width, $thumb_height); if( is_array($bgColour) ){ $bg = imagecolorallocate($thumb, $bgColour[0], $bgColour[1], $bgColour[2]); imagefill($thumb,0,0,$bg); } // copy from the source to the thumbnail imagecopyresampled($thumb, $image, $thumb_x, $thumb_y, 0, 0, $new_width, $new_height, $src_width, $src_height); return $thumb; } |
You must log in to post a comment.