Page 1 of 1

Imagecopyresampled: Help! Poor Image Quality!

Posted: Thu Jan 13, 2005 10:51 pm
by slevytam
Hello,

I am using imagecopyresampled to create thumbnails from an image; however, the quality I am getting is very poor. Can anyone help me fix this?

Thanks,

SL

----------------

$twidth = "125"; // Maximum Width For Thumbnail Images
$theight = "100"; // Maximum Height For Thumbnail Images
$simg = imagecreatefromjpeg("$idir".$url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height

if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $mwidth / $currheight; // Length Ratio For Width
$newheight = $mheight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
}
else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $mwidth / $currwidth; // Length Ratio For Height
$newwidth = $mwidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$mdir".$url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image

------------

Here are links to the created images from this script

original
http://www.desirethis.com/images/news/8.jpg

medium
http://www.desirethis.com/images/news/meds/8.jpg

thumbnail
http://www.desirethis.com/images/news/thumbs/8.jpg

Posted: Thu Jan 13, 2005 11:10 pm
by feyd
I think you are supposed to use imagecreatetruecolor() when working with resampled.. Since your thumbnail is still a JPEG, I don't see a point in creating a palettized file.

Posted: Fri Jan 14, 2005 9:56 am
by pickle
Yep, you should use imagecreatetruecolor. Also, specifying a quality of 100 as opposed to leaving it as the default (~75), should also increase the quality a bit.

Posted: Fri Jan 14, 2005 11:01 am
by slevytam
Excellent!

Thank you very much!

SL

PS. Where do I specify the quality setting of 100?

Posted: Fri Jan 14, 2005 1:42 pm
by pickle
Check the PHP manual for imagecreatejpeg. It's the 3rd argument and it's optional.

Posted: Fri Jan 14, 2005 7:18 pm
by slevytam
Hi,

Thanks for your reply... Im sorry but I can only see one parameter for that function in the php manual...

http://ca.php.net/manual/en/function.im ... omjpeg.php

Thanks again,

SL

Posted: Fri Jan 14, 2005 8:07 pm
by feyd
he said imagecreatejpeg().. also imagejpeg() supports it.

Posted: Mon Jan 17, 2005 9:57 am
by pickle
imagejpeg() - that's what I meant. Sorry about the mix up