aebstract Posted May 12, 2010 Share Posted May 12, 2010 Is there a way to make the image quality better after resize? Here is the script I am using: $idir = "gallery/$_POST[category]/"; // Path To Images Directory $tdir = "gallery/$_POST[category]/thumbs/"; // Path To Thumbnails Directory $twidth = "200"; // Maximum Width For Thumbnail Images $theight = "900"; // Maximum Height For Thumbnail Images // Uploading/Resizing Script $url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image $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 = $twidth / $currwidth; // Length Ratio For Height $newwidth = $twidth; // Width Is Equal To Max Width $newheight = $currheight * $zoom; // Creates The New Height } else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) $zoom = $twidth / $currwidth; // Length Ratio For Height $newwidth = $twidth; // 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 } imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "$tdir" . $url); // Saving The Image imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image print 'Image thumbnail created successfully.'; // Resize successful } else { print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed } } else { print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong print $file_ext; // Show The Invalid File's Extention print '.</font>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/ Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 Here's what I use: <?php $tn = imagecreatetruecolor($w, $h); $img = imagecreatefromjpeg($fn); imagecopyresampled($tn, $img, 0, 0, 0, 0, $w, $h, $ow, $oh); ?> $ow is the original width of image $oh is the original height $fn is the file Ken Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057317 Share on other sites More sharing options...
aebstract Posted May 12, 2010 Author Share Posted May 12, 2010 Where are you setting new heights? $w and $h? Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057319 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 <?php list($ow, $oh, $type, $attr) = getimagesize($fn); if ($oh > $ow) { $th = 160; $tw = ($th / $oh) * $ow; } if ($ow >= $oh) { $tw = 160; $th = ($tw / $ow) * $oh; } $w = round($tw); $h = round($th); ?> My thumbnails have a max height or width of 160 px. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057322 Share on other sites More sharing options...
aebstract Posted May 12, 2010 Author Share Posted May 12, 2010 My script looks just about the same, and my max is 200 width, no max height. So I am resizing dependent on the width. Here is an update: $idir = "gallery/$_POST[category]/"; // Path To Images Directory $tdir = "gallery/$_POST[category]/thumbs/"; // Path To Thumbnails Directory $twidth = "200"; // Maximum Width For Thumbnail Images $theight = "900"; // Maximum Height For Thumbnail Images // Uploading/Resizing Script $url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image $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 $newwidth = 200; $deci = 200 / $currwidth; $newheight = $currheight * $deci; $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, "$tdir" . $url); // Saving The Image imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image print 'Image thumbnail created successfully.'; // Resize successful } else { print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed } } else { print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong print $file_ext; // Show The Invalid File's Extention print '.</font>'; } } I changed the imagecopyresized to imagecopyresampled and it didn't affect anything. Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057326 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 If you can send me the original, I will process through my script and see what it looks like. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057330 Share on other sites More sharing options...
aebstract Posted May 12, 2010 Author Share Posted May 12, 2010 Sure, you can try this one: http://outlawracing.com/gallery/Flyers/huntsvillecard.jpg be cool if you got it to do 200 width just for testing. Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057331 Share on other sites More sharing options...
litebearer Posted May 12, 2010 Share Posted May 12, 2010 You need to add the quality variable in when you save (use 100) http://php.net/manual/en/function.imagejpeg.php Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057332 Share on other sites More sharing options...
aebstract Posted May 12, 2010 Author Share Posted May 12, 2010 You need to add the quality variable in when you save (use 100) http://php.net/manual/en/function.imagejpeg.php imagejpeg($dimg, "$tdir" . $url, 100); // Saving The Image this seems to actually look a bit better as far as save quality, maybe now it's a resizing (size) issue? Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057337 Share on other sites More sharing options...
947740 Posted May 12, 2010 Share Posted May 12, 2010 It might be better if you use the same image for a true comparison. Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057340 Share on other sites More sharing options...
aebstract Posted May 12, 2010 Author Share Posted May 12, 2010 <- before | after -> Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057345 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 You can test different sized thumbnails at http://rbnsn.com/phpfreaks/thumb_test.php?s=200 to change the size of the thumbnail change the value of "s". Ken Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057352 Share on other sites More sharing options...
aebstract Posted May 12, 2010 Author Share Posted May 12, 2010 s = height, right? Anyway, I played around a little and made a big thumbnail just to see save quality, did a 600 width on mine and (I guess 600 height) on yours. Check out how mine came out: http://outlawracing.com/gallery/Flyers/thumbs/huntsvillecard.jpg Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057358 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 Yes, s = height. If you do a s=900 on mine, the image is almost the same as yours. Ken Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057370 Share on other sites More sharing options...
aebstract Posted May 12, 2010 Author Share Posted May 12, 2010 For size yes, but did you look at the quality difference in the two? http://rbnsn.com/phpfreaks/thumb_test.php?s=900 http://outlawracing.com/gallery/Flyers/thumbs/huntsvillecard.jpg I'm thinking it has something to do with this: ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use When you look at it bigger like that, it's like it doesn't have enough colors to represent the image correctly? Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057374 Share on other sites More sharing options...
mattal999 Posted May 12, 2010 Share Posted May 12, 2010 Although, there is a severe quality difference between the two. I just checked, and the colours look very different. What version of PHP and imagegd are you running? If you don't know how to check, then run this script as a new file: <?php phpinfo(); ?> --- EDIT --- Try this: <?php $idir = "gallery/$_POST[category]/"; // Path To Images Directory $tdir = "gallery/$_POST[category]/thumbs/"; // Path To Thumbnails Directory $twidth = "200"; // Maximum Width For Thumbnail Images $theight = "900"; // Maximum Height For Thumbnail Images // Uploading/Resizing Script $url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") { $file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image $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 $newwidth = 200; $deci = 200 / $currwidth; $newheight = $currheight * $deci; $dimg = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail 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, "$tdir" . $url, 100); // Saving The Image imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image print 'Image thumbnail created successfully.'; // Resize successful } else { print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed } } else { print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong print $file_ext; // Show The Invalid File's Extention print '.</font>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057377 Share on other sites More sharing options...
aebstract Posted May 12, 2010 Author Share Posted May 12, 2010 GD: FreeType Version 2.2.1 PHP: Version 5.2.13 Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057381 Share on other sites More sharing options...
mattal999 Posted May 12, 2010 Share Posted May 12, 2010 Ok that sounds about right. Try the edited part of the post above, and see if it makes any difference. Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057382 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2010 Share Posted May 12, 2010 Try mine again. I changed the output quality to '100' from the default of '75'. GD Version bundled (2.0.34 compatible) PHP Version 5.2.9 Ken Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057383 Share on other sites More sharing options...
aebstract Posted May 12, 2010 Author Share Posted May 12, 2010 Ok that sounds about right. Try the edited part of the post above, and see if it makes any difference. http://outlawracing.com/gallery/Flyers/thumbs/huntsvillecard.jpg Changed 200 to 600 just to look at quality in a better size. Do you mind explaining what you did to fix this? It seems that whole color portion isn't there anymore. I think this will work! Here's another example: SO much better!! Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057387 Share on other sites More sharing options...
mattal999 Posted May 12, 2010 Share Posted May 12, 2010 Glad it's working! The solution: I actually noticed a difference in Ken's code to yours, which was the fact that he was creating a true colour image using a built in function. Your code was trying to manipulate a standard image (i'm assuming it has limited colour palettes) to become a true colour image. That was what the imageColorAllocate function was doing. All I did was get rid of the block of code that did this, and replace the imagecreate() function with imagecreatetruecolor(). Quote Link to comment https://forums.phpfreaks.com/topic/201536-resized-image-quality-sucks/#findComment-1057390 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.