DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Image Resize

07.20.2005
| 53021 views |
  • submit to reddit
        This function will resize any input file restricting the width and height to be no more than the specified pixels, and output a binary stream.

<pre>function resize_jpg($inputFilename, $new_side){
	$imagedata = getimagesize($inputFilename);
	$w = $imagedata[0];
	$h = $imagedata[1];
	
	if ($h > $w) {
		$new_w = ($new_side / $h) * $w;
		$new_h = $new_side;	
	} else {
		$new_h = ($new_side / $w) * $h;
		$new_w = $new_side;
	}
	
	$im2 = ImageCreateTrueColor($new_w, $new_h);
	$image = ImageCreateFromJpeg($inputFilename);
	imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $imagedata[0], $imagedata[1]);
	return $im2;
}</pre>    

Comments

Snippets Manager replied on Thu, 2008/07/17 - 11:08am

You don't have to host CPU-intensive resize scripts on your web server anymore. There already exist services for performing image manipulation remotely. With SteadyOffload.com, you can resize an image just by using a custom HTML attribute called "xmanip". Forget about all this hassle with GD, ImageMagick or whatever else.

Snippets Manager replied on Thu, 2007/03/29 - 4:50pm

How to use this script? I have erors!