maxhugen
Posts: 272
|
| Posted: 02/04/2009, 4:17 PM |
|
I created to following to create a thumbnail of 100x100, but without distorting the aspect ratio (it crops part of the image as required.) Maybe the script will help you? You can see the results at www.gardenloco/res/.
function gl_ImageThumbnail($filename, $newfilename="", $max_width=100, $max_height=100, $withSampling=true) {
/* Create and save a thumbnail of the image, 100x100, cropped as necessary
so that the original aspect ratio is preserved */
$width = 0;
$height = 0;
$newwidth = 0;
$newheight = 0;
$offset_x = 0;
$offset_y = 0;
// check that that the file is in a format we can deal with
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
switch ($ext) {
case 'jpg':
case 'jpeg':
case 'gif':
case 'png':
break;
default:
return false;
}
// If no new filename was specified, set the default
if(!$newfilename){
$path = pathinfo($filename, PATHINFO_DIRNAME);
$name = pathinfo($filename, PATHINFO_BASENAME);
$newfilename = $path."/tn/"."tn.".$name;
}
// Get original sizes
list($width, $height) = getimagesize($filename);
// Check if we need to resize for thumbnail
if ($width > $max_width || $height > $max_height)
$resize=true;
if ($resize) {
if ($width > $height) {
// We're dealing with max_width, so resize to suit
// height, then determine offset to crop width.
$newheight = $max_height;
$newwidth = ceil($width * ($max_height / $height));
$offset_x = ceil(($newwidth-$max_width)/2);
} else {
// We're dealing with max_height, so resize to suit
// width, then determine offset to crop height.
$newwidth = $max_width;
$newheight = ceil($height * ($max_width / $width));
$offset_y = ceil(($newheight-$max_height)/2);
}
/*
fb("newheight: ".$newheight);
fb("newwidth: ".$newwidth);
fb("offset_x: ".$offset_x);
fb("offset_y: ".$offset_y);
*/
// Create new image objects
$temp = imagecreatetruecolor($newwidth, $newheight);
$thumb = imagecreatetruecolor($max_width, $max_height);
// Load the original based on it's extension
if ($ext=='jpg' || $ext=='jpeg')
$source = imagecreatefromjpeg($filename);
else if ($ext=='gif')
$source = imagecreatefromgif($filename);
else if ($ext=='png')
$source = imagecreatefrompng($filename);
// Resize the image with sampling if specified
if ($withSampling) {
imagecopyresampled($temp, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
} else
imagecopyresized( $temp, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Crop the resized image
imagecopy($thumb, $temp , 0, 0, $offset_x, $offset_y, $max_width, $max_height);
imagedestroy($temp);
} else {
// Create the thumbnail from original, based on it's extension
if ($ext=='jpg' || $ext=='jpeg')
$thumb = imagecreatefromjpeg($filename);
else if ($ext=='gif')
$thumb = imagecreatefromgif($filename);
else if ($ext=='png')
$thumb = imagecreatefrompng($filename);
}
// Save the new image
if ($ext=='jpg' || $ext=='jpeg')
return imagejpeg($thumb, $newfilename);
else if ($ext=='gif')
return imagegif($thumb, $newfilename);
else if ($ext=='png')
return imagepng($thumb, $newfilename);
}
HTH
_________________
Max
www.gardenloco.com | www.eipdna.com | www.chrisarminson.com |