Imagecopyresampled: Help! Poor Image Quality!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slevytam
Forum Newbie
Posts: 3
Joined: Thu Jan 13, 2005 10:50 pm

Imagecopyresampled: Help! Poor Image Quality!

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
slevytam
Forum Newbie
Posts: 3
Joined: Thu Jan 13, 2005 10:50 pm

Post by slevytam »

Excellent!

Thank you very much!

SL

PS. Where do I specify the quality setting of 100?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Check the PHP manual for imagecreatejpeg. It's the 3rd argument and it's optional.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
slevytam
Forum Newbie
Posts: 3
Joined: Thu Jan 13, 2005 10:50 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

he said imagecreatejpeg().. also imagejpeg() supports it.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

imagejpeg() - that's what I meant. Sorry about the mix up
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply