Jump to content

Image resize quality


jarvis

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/188209-image-resize-quality/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.