Here's the code I use to create thumbnails
Code:
function resizeImage($img, $imgPath, $quality,$medPath,$thumbPath) {
// Open the original image.
list($width, $height, $type, $attr) = getimagesize($imgPath.'/'.$img);
//create new file name
$baseName = explode("_",$img);
$newNameE = explode(".", $img);
// thumbnail
$newName = $baseName[0] . '.'. $newNameE[1] ;
//check for existence
if (!file_exists($thumbPath.$newName)) {
if (!$original) {
$original = createimage($imgPath.$img);
}
// Determine new width and height for thumbnail image
if ($width>$height) {
$newWidth=72;
$newHeight=72*$height/$width;
} else {
$newHeight=72;
$newWidth=72*$width/$height;
}
// Resample the image.
$tempImg = imagecreatetruecolor($newWidth, $newHeight);
if (!$tempImg) {
return false;
} else {
if (!imagecopyresampled($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height)) {
return false;
} else {
// Save the image.
if(!imagejpeg($tempImg, $thumbPath.$newName, $quality)) {
return false;
} else {
//Clean up
imagedestroy($original);
imagedestroy($tempImg);
}
}
}
}
return true;
}
function createimage($pathplusimg) {
$foriginal = imagecreatefromjpeg($pathplusimg);
if (!$foriginal) {
$foriginal=imagecreatefromgif($pathplusimg);
}
if (!$foriginal) {
$foriginal=imagecreatefrompng($pathplusimg);
}
return $foriginal;
}
key point is probably using
Code:
$tempImg = imagecreatetruecolor($newWidth, $newHeight);
to create a blank image of the new size and then using
Code:
imagecopyresampled($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height)
to resize and copy the image