Forum Moderators: coopster

Message Too Old, No Replies

Thumb Image Jagged when resized

some setting I'm missing?

         

neophyte

2:33 am on Jun 6, 2008 (gmt 0)

10+ Year Member



Hello All -

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);
}
}

cameraman

7:18 am on Jun 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just for grins try saving the image as a PNG. I've been playing with imagemagick lately and noticed that PNGs generally wind up looking a whole lot better - unfortunately the same image is typically about 10 times the filesize. You also might try fiddling with the thumbnail size - a little bigger or a little smaller may make a noticeable difference.

deMorte

11:27 am on Jun 6, 2008 (gmt 0)

10+ Year Member



Try using imagecopyresampled instead of imagecopyresized.
I'm not sure if this has any effect on the quality, but I remember reading somewhere that resampling would produce better images.

neophyte

11:58 pm on Jun 6, 2008 (gmt 0)

10+ Year Member



Cameraman, DeMorte -

Thanks to you both for your suggestions. imagecopyresampled won the day - tried it as DeMorte suggested (in the place of imagecopyresized) and no more jaggies! Perfect!