Image Resizer

Posted by Admin on Oct 26 2008 | PHP Classes

The Simple Image Resizer class is useful when you would upload a large image file to the server. Suppose that we do not really need to upload the exactly size. And the fact is we want to upload and save those files into smaller size.


This class can be used to resize images of different formats. It can take an image file in a GIF, JPEG or PNG format and resize it to a given size. The resized image may keep the proportion between the width and height of the original image, The resulting image is saved to a file in the same format as the original image.

You can see the original image size like this one:

And the result after the image resizing by Simple Image Resizer class is:

Download Image Resizer

Code ImageResize.inc Class File:

Code:
<?php
/**
 * @author Dodit Suprianto
 * Email: d0dit@yahoo.com
 * Website: http://doditsuprianto.com
 *
 * Nama file: ImageResize.inc
 *
 * This class to resize an image from bigger to smaller size.
 * Supporting PNG, JPG, and GIF image format.
 *
 * if you chose $proportional=true, it means that width or height image is proportional, example:
 * $r = new Resize("test.jpg", 0, 500, true); height image will be 500 pixel and width will be proportional
 * $r = new Resize("test.jpg", 500, 0, true); width image will be 500 pixel and height will be proportional
 *
 * if you chose $proportional=false, it means the width and height image will be customizable, example:
 * $r = new Resize("test.jpg", 500, 500, true); it forces the image size will be 500 pixel width and 500 pixel height
 *
 */

	class Resize
	{
		private $file_source;
		private $width_resize;
		private $height_resize;
		private $proportional;

		public function __construct($file_source, $width_resize, $height_resize, $proportional)
		{
			$this->file_source = $file_source;
			$this->width_resize = $width_resize;
			$this->height_resize = $height_resize;
			$this->proportional = $proportional;
		}

		public function setProportional($proportional)
		{
			$this->proportional = $proportional;
		}

		public function setFileSource($file_source)
		{
			$this->file_source = $file_source;
		}

		public function setHeightResize($height_resize)
		{
			$this->height_resize = $height_resize;
		}

		public function setWidthResize($width_resize)
		{
			$this->width_resize = $width_resize;
		}

		private function MemoryUsage()
		{
			$imageInfo    = getimagesize($this->file_source);
			$memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);

			$memoryLimit = (int) ini_get('memory_limit')*1048576;
			if ((memory_get_usage() + $memoryNeeded) > $memoryLimit)
			ini_set('memory_limit', ceil((memory_get_usage() + $memoryNeeded + $memoryLimit)/1048576).'M');
		}

		public function ImageResize()
		{
			$this->MemoryUsage();
			if ( $this->height_resize <= 0 && $this->width_resize <= 0 ) return false;

			$info = getimagesize($this->file_source);
		    $image = '';

			$final_width = 0;
			$final_height = 0;
			list($width_old, $height_old) = $info;

			if ($this->proportional)
			{
				$proportion = $width_old / $height_old;

				if ( $this->width_resize > $this->height_resize && $this->height_resize != 0)
				{
					$final_height = $this->height_resize;
					$final_width = $final_height * $proportion;
				}
				elseif ( $this->width_resize < $this->height_resize && $this->width_resize != 0)
				{
					$final_width = $this->width_resize;
					$final_height = $final_width / $proportion;
				}
				elseif ( $this->width_resize == 0 )
				{
					$final_height = $this->height_resize;
					$final_width = $final_height * $proportion;
				}
				elseif ( $this->height_resize == 0)
		        {
					$final_width = $this->width_resize;
					$final_height = $final_width / $proportion;
				}
				else
				{
					$final_width = $this->width_resize;
					$final_height = $this->height_resize;
				}
			}
			else
			{
				$final_width = ($this->width_resize <= 0) ? $this->width_resize_old : $this->width_resize;
				$final_height = ($this->height_resize <= 0) ? $this->height_resize_old : $this->height_resize;
			}

			switch ( $info[2] )
			{
				case IMAGETYPE_GIF:
					$image = imagecreatefromgif($this->file_source);
				break;
				case IMAGETYPE_JPEG:
					$image = imagecreatefromjpeg($this->file_source);
				break;
				case IMAGETYPE_PNG:
					$image = imagecreatefrompng($this->file_source);
				break;
				default:
					return false;
			}

			$image_resized = imagecreatetruecolor( $final_width, $final_height );
			imagecolortransparent($image_resized, imagecolorallocate($image_resized, 0, 0, 0) );
			imagealphablending($image_resized, false);
			imagesavealpha($image_resized, true);

			imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);

			switch ( $info[2] )
			{
				case IMAGETYPE_GIF:
					imagegif($image_resized, time().".gif", 100);
				break;
				case IMAGETYPE_JPEG:
					imagejpeg($image_resized, time().".jpg", 100);
				break;
				case IMAGETYPE_PNG:
					imagepng($image_resized, time().".png", 100);
				break;
				default:
					return false;
			}
			return true;
		}
	}
?>

Test File Code:

Code:
<html>

<head>
<title>Image Resize</title>
</head>

<body>

<form enctype="multipart/form-data" method="POST" action="<?php echo $_SERVER['SELF'];?>">
	Photo: <input type=file name=photo size="20"><br>
	Proportional: <input type="radio" value="yes" checked name="R1"> <input type="radio" value="no" name="R1"><br>
	Width: <input type="text" name="width" size="20"><br>
	Height: <input type="text" name="height" size="20"><br>
	<input type="submit" name="submit" value="Resize">
</form>

<?php
	if ($_POST['submit'])
	{
		require_once("ImageResize.inc");

		if ($_POST['R1'] == "yes")
		{
			// proportional
			$r = new Resize($_FILES['photo']['tmp_name'], $_POST['width'], 0, true);
			// or $r = new Resize($_FILE['photo']['tmp_name'], 0, $_POST['height'], true);
		} else
		{
			// Force the image size
			$r = new Resize($_FILES[photo][tmp_name], $_POST[width], $_POST[height], false);
		}
		$r->ImageResize();
	}
?>

</body>

</html>

Tags: , , ,

1 comment for now

« Prev - Next »