Forum Moderators: coopster
I'm trying my hand at php's image resizing functions and all is well except for the output of the thumnails. They look "ok" - very sharp and clear BUT all edges around the people (heads, shoulders, collars, etc) are jagged - like they've been aliased or something during the resizing process. The original images that these thumbs are being made from are all beautiful and smooth, so I don't know what I'm doing wrong except that maybe I'm missing (or haven't properly set) a parameter in one of the image functions.
BTW: the images I'm resizing are jpegs saved at maximum (100%) quality.
My code block for creating and saving thumbs is shown below.
Any guidance is greatly appreciated!
Neophyte
****************************
//Create and thumbnail image
function createThumbNail($fileName, $fileType, $thumbPath, $thumbWidth, $keepRatio)
{
$imageTypes = array('.gif','.jpg','.jpeg','.png','.bmp');
$fileExt = getFileExt($fileName);
if(!in_array($fileExt, $imageTypes))
{
echo 'Cannot Make Thumbnail from file type: ' , $fileExt;
}
else
{
switch($fileType)
{
case 'image/pjpeg': case 'image/jpeg':
$newImage = imagecreatefromjpeg($thumbPath);
break;
case 'image/x-png': case 'image/png':
$newImage = imagecreatefrompng($thumbPath);
break;
case 'image/gif':
$newImage = imagecreatefromgif($thumbPath);
break;
}
//list the width and height and keep the height ratio.
list($width, $height) = getimagesize($thumbPath);
//calculate the image ratio
$imageRatio = $width / $height;
if($keepRatio)
{
if($imageRatio > 1)
{
$newWidth = $thumbWidth;
$newHeight = $thumbWidth / $imageRatio;
}
else
{
$newHeight = $thumbWidth;
$newWidth = $thumbWidth * $imageRatio;
}
}
else
{
$newWidth = $thumbWidth;
$newHeight = $thumbWidth / $imageRatio;
}
$newWidth = round($newWidth);
$newHeight = round($newHeight);
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
//Resize Image
imagecopyresized($resizedImage, $newImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
//Save Resized Image
imagejpeg($resizedImage, $thumbPath, 100);
imagedestroy($resizedImage);
imagedestroy($newImage);
}
}