Return to Snippet

Revision: 33446
at October 9, 2010 21:50 by stz184


Updated Code
function image_resize($input_file, $output_file, $new_size=75, $crop=true) {
	if(!file_exists($input_file)) {
		return false;
	}
	if (function_exists('ini_set')) {
		@ini_set('memory_limit', -1);
		ini_set( 'arg_separator.output' , '&' );
	}
	if(eregi('\.jpg$|\.jpeg$', $input_file)) {
		$original = @imagecreatefromjpeg($input_file);
	} 
	elseif (eregi('\.gif$', $input_file)) {
		$original = @imagecreatefromgif($input_file);
	} 
	elseif (eregi('\.png$', $input_file)) {
		$original = @imagecreatefrompng($input_file);
	}
	else {
		return false;
	}
	if ($original) {
		if (function_exists('getimagesize')) {
			list($width, $height, $type, $attr) = getimagesize($input_file);
		} else {
			return false;
		}
		$ratio['x'] = round($width/$new_size, 2);
		$ratio['y'] = round($height/$new_size, 2);
		if($crop == true) {
			$ratio = min($ratio['x'], $ratio['y']);
		}
		else {
			$ratio = max($ratio['x'], $ratio['y']);
		}
		$smallheight = floor($height / $ratio);
		$smallwidth = floor($width / $ratio);
		if($crop == true) {
			$ofx = floor(($new_size - $smallwidth) / 2);
			$ofy = floor(($new_size - $smallheight) / 2);
		}
		else {
			$ofx = $ofy = 0;
		}
				
		if (function_exists('imagecreatetruecolor')) {
			if($crop == true) {
				$small = imagecreatetruecolor($new_size, $new_size);
			} 
			else {
				$small = imagecreatetruecolor($smallwidth, $smallheight);
			}
		} else {
			if($crop == true) {
				$small = imagecreate($new_size, $new_size);
			}
			else {
				$small = imagecreate($smallwidth, $smallheight);
			}
		}
		if (function_exists('imagecopyresampled')) {
			imagecopyresampled($small, $original, $ofx, $ofy, 0, 0, $smallwidth, $smallheight, $width, $height);
		} else {
			imagecopyresized($small, $original, $ofx, $ofy, 0, 0, $smallwidth, $smallheight, $width, $height);
		}
		if(imagejpeg($small, $output_file)) {
			if(file_exists($output_file)) {
				return true;
			}
		}
		else {
			return false;
		}
	}	
}

Revision: 33445
at October 8, 2010 21:50 by stz184


Initial Code
function image_resize($input_file, $output_file, $new_size=75, $crop=true) {
	if(!file_exists($input_file)) {
		return false;
	}
	if (function_exists('ini_set')) {
		@ini_set('memory_limit', -1);
		ini_set( 'arg_separator.output' , '&' );
	}
	if(eregi('\.jpg$|\.jpeg$', $input_file) || getmimetype($input_file, 'image/jpeg')) {
		$original = @imagecreatefromjpeg($input_file);
	} 
	elseif (eregi('\.gif$', $input_file) || getmimetype($input_file, 'image/gif')) {
		$original = @imagecreatefromgif($input_file);
	} 
	elseif (eregi('\.png$', $input_file) || getmimetype($input_file, 'image/png')) {
		$original = @imagecreatefrompng($input_file);
	}
	else {
		return false;
	}
	if ($original) {
		if (function_exists('getimagesize')) {
			list($width, $height, $type, $attr) = getimagesize($input_file);
		} else {
			return false;
		}
		$ratio['x'] = round($width/$new_size, 2);
		$ratio['y'] = round($height/$new_size, 2);
		if($crop == true) {
			$ratio = min($ratio['x'], $ratio['y']);
		}
		else {
			$ratio = max($ratio['x'], $ratio['y']);
		}
		$smallheight = floor($height / $ratio);
		$smallwidth = floor($width / $ratio);
		if($crop == true) {
			$ofx = floor(($new_size - $smallwidth) / 2);
			$ofy = floor(($new_size - $smallheight) / 2);
		}
		else {
			$ofx = $ofy = 0;
		}
				
		if (function_exists('imagecreatetruecolor')) {
			if($crop == true) {
				$small = imagecreatetruecolor($new_size, $new_size);
			} 
			else {
				$small = imagecreatetruecolor($smallwidth, $smallheight);
			}
		} else {
			if($crop == true) {
				$small = imagecreate($new_size, $new_size);
			}
			else {
				$small = imagecreate($smallwidth, $smallheight);
			}
		}
		if (function_exists('imagecopyresampled')) {
			imagecopyresampled($small, $original, $ofx, $ofy, 0, 0, $smallwidth, $smallheight, $width, $height);
		} else {
			imagecopyresized($small, $original, $ofx, $ofy, 0, 0, $smallwidth, $smallheight, $width, $height);
		}
		if(imagejpeg($small, $output_file)) {
			if(file_exists($output_file)) {
				return true;
			}
		}
		else {
			return false;
		}
	}	
}

Initial URL

                                

Initial Description
If the optional parameter $crop is set to TRUE the image will be zoomed and cropped to specific size ($new_size). Else, the image will be resized to size, keeping aspect ratio and using $new_size as max size.

Initial Title
Image resizer (thumbnail generator)

Initial Tags
resize, image

Initial Language
PHP