Resize your image with PHP (Output as JPG or Save to a new file)

Comment: 2 Comments | Hit: 3349 | Date: 22 Jul 2010 19:07 | Category: PHP and MySQL

To resize your images with ratio size and reduce file's size is very easy now. There are two options for this. First output to browser as an image and second save your new image to a file by filtering file's type. I hope that this will give an advantage to fulfil your need.

Sample

Output to browsers

Now you can resize an image very easy.

To resize an image to a max of 400px along the longest side

To resize an image to a height of 400px (width will be kept to the right aspect ratio)


To resize an image to a width of 400px (height will be kept to the right aspect ratio)

if (!isset($_GET['file']) || !isset($_GET['size'])) {
	echo "Image variables not specified correctly";
	exit();
}

$file = $_GET['file'];
$size = $_GET['size'];

/* Get image dimensions and ratio */
list($width, $height) = getimagesize($file);
$ratio = $width / $height;

/* Decide how we should resize image - fixed width or fixed height */
if (substr($size, 0, 1) == 'h') {
	$type = 'fixedheight';
} elseif (substr($size, 0, 1) == 'w') {
	$type = 'fixedwidth';
} elseif ($height > $width) {
	$type = 'fixedheight';
} else {
	$type = 'fixedwidth';
}

/* Calculate new dimensions */
if ($type == 'fixedheight') {
	$new_width = floor(str_replace('h','',$size) * $ratio);
	$new_height = str_replace('h','',$size);
} else {
	$new_width = str_replace('w','',$size);
	$new_height = floor(str_replace('w','',$size) / $ratio);
}

/* Resample */
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_image = imagecreatefromjpeg($file);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

/* Output */
header('Content-type: image/jpeg');
imagejpeg($new_image, null, 100);
exit();

I am thankful Harvey Kane, harvey[ @ ]harveykane.com, who is the author of image.php

Save to a new image

Based on Harvey Kane, I created a new function, img_to_file, that can output your new image to a file by filtering file type JPG, PNG, and GIF.

First, I created two new functions to use in img_to_file function

function get_image_extension($file){
	$file = strtolower($file);
	$dot_pos = strrpos($file, ".") + 1;
	return substr($file,$dot_pos,strlen($file));
}
function allow_image_extension($file_name, $allow_type = "gif|jpg|png"){
	return (ereg($allow_type, get_file_extension($file_name))) ? true : false;
}

Second, I recoded Harvey Kane's script to a function with 3 arguments.

  • $file: your image's path and image's name
  • $size: your input size (leading by w or h or none)
  • $dpi: output quality
function img_to_file($file, $size, $filename, $dpi = 75){
	$file_extension = get_image_extension($file);
	if (!isset($file) || !isset($size)) {
		echo "Image variables not specified correctly";
		exit();
	}
	if(allow_image_extension($file_extension)){
		echo "Invalid file format. GIF, JPG and PNG are allowed.";											 
	}
	/* Get image dimensions and ratio */
	list($width, $height) = getimagesize($file);
	$ratio = $width / $height;
	
	/* Decide how we should resize image - fixed width or fixed height */
	if (substr($size, 0, 1) == 'h') {
		$type = 'fixedheight';
	} elseif (substr($size, 0, 1) == 'w') {
		$type = 'fixedwidth';
	} elseif ($height > $width) {
		$type = 'fixedheight';
	} else {
		$type = 'fixedwidth';
	}
 
	/* Calculate new dimensions */
	if ($type == 'fixedheight') {
		$new_width = floor(str_replace('h','',$size) * $ratio);
		$new_height = str_replace('h','',$size);
	} else {
		$new_width = str_replace('w','',$size);
		$new_height = floor(str_replace('w','',$size) / $ratio);
	}

	switch($file_extension){
		case "jpg":
			$new_image = imagecreatetruecolor($new_width, $new_height);
			$old_image = imagecreatefromjpeg($file);
			imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			imagejpeg($new_image, $filename, $dpi);
			break;
		case "gif"
			$new_image = imagecreatetruecolor($new_width, $new_height);
			$old_image = imagecreatefromgif($file);
			imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			imagegif($new_image, $filename);
			break;
		case "png":
			$new_image = imagecreatetruecolor($new_width, $new_height);
			$white = imagecolorallocate($new_image, 255, 255, 255);
			imagefill($new_image, 0, 0, $white);
			$old_image = imagecreatefrompng($file);
			imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
			imagepng($new_image, $filename);
			break;
	}

}

Finally, test and enjoy.

Source:

nVitou.com

http://digg.com/submit?phase=2&url=http://www.nvitou.com/en/article/resize-your-image-with-php-output-as-jpg-or-save-to-a-new-file/&title=Resize your image with PHP (Output as JPG or Save to a new file)http://delicious.com/post?url=http://www.nvitou.com/en/article/resize-your-image-with-php-output-as-jpg-or-save-to-a-new-file/&title=Resize your image with PHP (Output as JPG or Save to a new file)http://www.facebook.com/sharer.php?u=http://www.nvitou.com/en/article/resize-your-image-with-php-output-as-jpg-or-save-to-a-new-file/&t=Resize your image with PHP (Output as JPG or Save to a new file)http://twitter.com/home?status=Resize your image with PHP (Output as JPG or Save to a new file) via @nvitou http://bit.ly/bdDuEBhttp://www.google.com/bookmarks/mark?op=edit&bkmk=http://www.nvitou.com/en/article/resize-your-image-with-php-output-as-jpg-or-save-to-a-new-file/&title=Resize your image with PHP (Output as JPG or Save to a new file)http://www.stumbleupon.com/submit?url=http://www.nvitou.com/en/article/resize-your-image-with-php-output-as-jpg-or-save-to-a-new-file/&title=Resize your image with PHP (Output as JPG or Save to a new file)http://www.myspace.com/Modules/PostTo/Pages/?u=http://www.nvitou.com/en/article/resize-your-image-with-php-output-as-jpg-or-save-to-a-new-file/&t=Resize your image with PHP (Output as JPG or Save to a new file)http://www.squidoo.com/lensmaster/bookmark?http://www.nvitou.com/en/article/resize-your-image-with-php-output-as-jpg-or-save-to-a-new-file/&title=Resize your image with PHP (Output as JPG or Save to a new file)

Comments

evannak

On July 23, 2010 at 05:46:54

Hi man, why there is no Demo?

nVitou

On July 23, 2010 at 20:54:20

Hi Vannak, This just wants you to test to take more experience.

Name:

Email/Website:

Your comment:

nVitou.com in brief

nVitou.com was founded in July 2010 by a Cambodian, NOUV Vithou, located in Phnom Penh. The mission is to exchange knowledge and experiences on code and design.