Return to Snippet

Revision: 50991
at September 13, 2011 04:17 by TimoZachi


Updated Code
<?php
/**
 * This function resizes an image to $fitInWidth and $fitInHeight, but doesn't distort the image proportions
 * 
 * $path: The full path to the image. Ex.: "/images/my_image.jpg"
 * 
 * $newName: If you don't want to override the original image, you can specify a new name.
 * 
 * $fitInWidth:The height in pixels that you want to fit the image in
 * 
 * $fitInHeight: The height in pixels that you want to fit the image in
 * 
 * $jpegQuality: If the image is a jpeg, the function will save 
 * the resized jpeg with the specified quality from 1 to 100
 */
function resize_image($path, $fitInWidth, $fitInHeight, $newName = '', $jpegQuality = 100)
{
	list($width, $height, $type) = getimagesize($path);//Getting image information
	
	$scaleW = $fitInWidth/$width;
	$scaleH = $fitInHeight/$height;
	if($scaleH > $scaleW)
	{
		$new_width = $fitInWidth;
		$new_height = floor($height * $scaleW);
	}
	else
	{
		$new_height = $fitInHeight;			
		$new_width = floor($width * $scaleH);
	}
	$new_path = $newName == '' ? $path : dirname($path) . '/' . $newName;
	
	if($type == IMAGETYPE_JPEG)//If image is jpeg
	{
		$image_now = imagecreatefromjpeg($path);//Get image from path
		$image_new = imagecreatetruecolor($new_width, $new_height);//Create new image from scratch
		//Copy image from path into new image ($image_new) with new sizes
		imagecopyresampled($image_new, $image_now, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		imagejpeg($image_new, $new_path, $jpegQuality);
	}
	else if($type == IMAGETYPE_GIF)//If image is gif
	{
		$image_now = imagecreatefromgif($path);
		$image_new = imagecreatetruecolor($new_width, $new_height);
		imagecopyresampled($image_new, $image_now, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		imagegif($image_new, $new_path);
	}
	else if($type == IMAGETYPE_PNG)//If image is png
	{
	    $image_now = imagecreatefrompng($path);
	    $image_new = imagecreatetruecolor($new_width, $new_height);
	    //Setting black color as transparent because image is png
	    imagecolortransparent($image_new, imagecolorallocate($image_new, 0, 0, 0));
		imagecopyresampled($image_new, $image_now, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
	    imagepng($image_new, $new_path);
	}
	else
	{
		//Image type is not jpeg, gif or png.
	}
	imagedestroy($image_now);
	imagedestroy($image_new);
}

//How to use
$img_path = 'images/my_image.jpg';//The path to the image you want to resize
//Here i'm giving a new name to the resized image so that it doesn't override the original  image. If you want to override the original image change: $new_name = '';
$new_name = 'resized_image.jpg';
resize_image($img_path, 200, 200, $new_name);
?>

Revision: 50990
at September 10, 2011 08:38 by TimoZachi


Initial Code
<?php
/**
 * This function resizes an image to $fitInWidth and $fitInHeight, but doesn't distort the image proportions
 * 
 * $path: The full path to the image. Ex.: "/images/my_image.jpg"
 * 
 * $newName: If you don't want to override the original image, you can specify a new name.
 * 
 * $fitInWidth:The height in pixels that you want to fit the image in
 * 
 * $fitInHeight: The height in pixels that you want to fit the image in
 * 
 * $jpegQuality: If the image is a jpeg, the function will save 
 * the resized jpeg with the specified quality from 1 to 100
 */
function resize_image($path, $fitInWidth, $fitInHeight, $newName = '', $jpegQuality = 100)
{
	list($width, $height, $type) = getimagesize($path);//Getting image information
	
	$scaleW = $fitInWidth/$width;
	$scaleH = $fitInHeight/$height;
	if($scaleH > $scaleW)
	{
		$new_width = $fitInWidth;
		$new_height = floor($height * $scaleW);
	}
	else
	{
		$new_height = $fitInHeight;			
		$new_width = floor($width * $scaleH);
	}
	$new_path = $newName == '' ? $path : dirname($path) . '/' . $newName;
	
	if($type == IMAGETYPE_JPEG)//If image is jpeg
	{
		$image_now = imagecreatefromjpeg($path);//Get image from path
		$image_new = imagecreatetruecolor($new_width, $new_height);//Create new image from scratch
		//Copy image from path into new image ($image_new) with new sizes
		imagecopyresampled($image_new, $image_now, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		imagejpeg($image_new, $new_path, $jpegQuality);
	}
	else if($type == IMAGETYPE_GIF)//If image is gif
	{
		$image_now = imagecreatefromgif($path);
		$image_new = imagecreatetruecolor($new_width, $new_height);
		imagecopyresampled($image_new, $image_now, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		imagegif($image_new, $new_path);
	}
	else if($type == IMAGETYPE_PNG)//If image is png
	{
	    $image_now = imagecreatefrompng($path);
	    $image_new = imagecreatetruecolor($new_width, $new_height);
	    //Setting black color as transparent because image is png
	    imagecolortransparent($image_new, imagecolorallocate($image_new, 0, 0, 0));
		imagecopyresampled($image_new, $image_now, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
	    imagepng($image_new, $new_path);
	}
	else
	{
		//Image type is not jpeg, gif or png.
	}
}

//How to use
$img_path = 'images/my_image.jpg';//The path to the image you want to resize
$new_name = 'resized_image.jpg';//Here i'm giving a new name to the resized image so that it doesn't override the original image
resize_image($img_path, 200, 200, $new_name);
?>

Initial URL


Initial Description
A function that resizes an image to desired width and height, but does not distort the image proportions. Works for JPEG, GIF and PNG.

Initial Title
Resize an image without distorting proportions (JPEG, GIF or PNG)

Initial Tags
resize, image

Initial Language
PHP