jarvis Posted January 12, 2010 Share Posted January 12, 2010 Hi All, I've got the following script which resizes my images. It then produces: 1) A cropped thumbnail 2) A scaled down image Is there anything I can do to improve the images quality of the resized file? The client using it is uploading very high res images you see. Thanks <?php function saveThumb($gal,$fn) { define("MAX_XY_SMALL", 150); // maximum width or height of thumbnail image define("MAX_XY_LARGE", 550); // maximum width or height of thumbnail image $fpath = "categories/$gal/$fn"; //print $fpath."\n"; /* GetImageSize returns an array: * $info[0] - horizontal size of image in pixels * $info[1] - vertical size of image in pixels * $info[2] - type of image (1=GIF, 2=JPEG, 3=PNG) */ $info = GetImageSize("$fpath"); //print_r($info); // do we need to resize image? if ($info[0] > MAX_XY_SMALL || $info[1] > MAX_XY_SMALL) { // is image landscape? if ($info[0] >= $info[1]) { $width = MAX_XY_SMALL; $height = MAX_XY_SMALL; // or portrait? } else { $height = MAX_XY_SMALL; $width = MAX_XY_SMALL; } } else { // use original dimensions $width = $info[0]; $height = $info[1]; } if ($info[0] > MAX_XY_LARGE || $info[1] > MAX_XY_LARGE) { // is image landscape? if ($info[0] >= $info[1]) { $width_large = MAX_XY_LARGE; $height_large = $info[1]*MAX_XY_LARGE/$info[0]; // or portrait? } else { $height_large = MAX_XY_LARGE; $width_large = $info[0]*MAX_XY_LARGE/$info[1]; } } else { // use original dimensions $width_large = $info[0]; $height_large = $info[1]; } // create new thumbnail image //echo "<br>$width - $height<br>"; $im = ImageCreateTrueColor($width, $height); $im_large = ImageCreateTrueColor($width_large, $height_large); /* determine image type and load original image. Notice new tests for image * types. The GD extension has changed a lot over the years, dropping GIFs * and adding PNG support. By testing, we avoid trying to process an image * PHP can't deal with */ $im_big = ""; // default is no image switch ($info[2]) { case 1 : if (ImageTypes() & IMG_GIF) // test for GIF support $im_big = ImageCreateFromGIF("$fpath"); break; case 2 : if (ImageTypes() & IMG_JPG) // test for JPEG support $im_big = ImageCreateFromJPEG("$fpath"); break; case 3 : if (ImageTypes() & IMG_PNG) // test for PNG support $im_big = ImageCreateFromPNG("$fpath"); break; case 4 : if (ImageTypes() & IMG_BMP) // test for BMP support $im_big = ImageCreateFromBMP("$fpath"); break; } if ($im_big) { /* resize original image into thumbnail - see PHP Manual for details on * the arguements ImageCopyResized takes */ #ImageCopyResized($im,$im_big,0,0,0,0,$width,$height,MAX_XY_SMALL,MAX_XY_SMALL); #ImageCopyResized($im,$im_big,0,0,150,150,$width,$height,MAX_XY_SMALL,MAX_XY_SMALL); ImageCopyResized($im,$im_big,0,0,300,230,$width,$height,MAX_XY_SMALL,MAX_XY_SMALL); ImageCopyResized($im_large,$im_big,0,0,0,0,$width_large,$height_large,$info[0],$info[1]); } else { // couldn't load original image, so generate 'no thumbnail' image instead $width = 150; $height = 150; $im = ImageCreate($width, $height); // create a blank image $bgc = ImageColorAllocate($im, 0, 0, 0); // background color = black $tc = ImageColorAllocate($im, 255, 255, 255); // text color = white ImageFilledRectangle($im, 0, 0, $width, $height, $bgc); // draw rectangle ImageString($im, 3, 9, 36, "No thumbnail", $tc); // add text ImageString($im, 3, 17, 48, "available", $tc); } /* save our image in thumb directory - if the second argument is left out, * the image gets sent to the browser, as in thumbnail.php */ ImageJPEG($im, "categories/".$gal."/t_".$fn, 100); ImageJPEG($im_large, "categories/".$gal."/".$fn, 100); // clean up our working images ImageDestroy($im_big); ImageDestroy($im); ImageDestroy($im_large); } ?> Thanks in advanced Quote Link to comment https://forums.phpfreaks.com/topic/188209-image-resize-quality/ Share on other sites More sharing options...
fooDigi Posted January 12, 2010 Share Posted January 12, 2010 look into imagecopyresampled(). it may make the images more clear. Quote Link to comment https://forums.phpfreaks.com/topic/188209-image-resize-quality/#findComment-993646 Share on other sites More sharing options...
jarvis Posted January 12, 2010 Author Share Posted January 12, 2010 Thanks fooDiggi, Do I simply replace: ImageCopyResized($im,$im_big,0,0,300,230,$width,$height,MAX_XY_SMALL,MAX_XY_SMALL); With imagecopyresampled($im,$im_big,0,0,300,230,$width,$height,MAX_XY_SMALL,MAX_XY_SMALL); Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188209-image-resize-quality/#findComment-993694 Share on other sites More sharing options...
fooDigi Posted January 12, 2010 Share Posted January 12, 2010 yes, i believe so. it has been a while since i have worked with my gd scripts... should resample it making them smoother. Quote Link to comment https://forums.phpfreaks.com/topic/188209-image-resize-quality/#findComment-993700 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.