Php Resize Image Using OOP

06,03,2010 at 21:29 2 yorum

<![CDATA[You should know Simon Jarvis's Php resizer. I edited his code and added centerer. It means when you resize your image, it will not be deformed, the script will crop the image from center. Just try it and see.

class resizeThis{

	var $sourceImage;
	var $ext;

	function load($source){
		$this->ext = strtolower(pathinfo($source, PATHINFO_EXTENSION));
		if($this->ext == "jpg"){
			$this->sourceImage = imagecreatefromjpeg($source);
		}elseif($this->ext == "gif"){
			$this->sourceImage = imagecreatefromgif($source);
		}elseif($this->ext == "png"){
			$this->sourceImage = imagecreatefrompng($source);
		}
	}

	function getWidth(){
		return imagesx($this->sourceImage);
	}

	function getHeight(){
		return imagesy($this->sourceImage);
	}

	function resizeToHeight($height){
		$ratio = $height / $this->getHeight();
		$width = $this->getWidth() * $ratio;
		$this->resize($width,$height);
	}

	function resizeToWidth($width){
		$ratio = $width / $this->getWidth();
		$height = $this->getheight() * $ratio;
		$this->resize($width,$height);
	}

	function scale($scale){
		$width = $this->getWidth() * $scale/100;
		$height = $this->getheight() * $scale/100;
		$this->resize($width,$height);
	}

	function resize($width,$height){
		$newImage = imagecreatetruecolor($width, $height);
		imagecopyresampled($newImage, $this->sourceImage, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
		$this->sourceImage = $newImage;
	}

	function crop($width,$height,$x_pos=0,$y_pos=0){
		$newImage = imagecreatetruecolor($width, $height);

		if($this->getWidth()/$this->getHeight() != $width/$height){
			$width_temp=$width;
			$height_temp=$height;

			if($this->getWidth()/$this->getHeight()>$width/$height){
				$width = $this->getWidth()*$height/$this->getHeight();
				$x_pos = -($width-$width_temp)/2;
				$y_pos = 0;
			}else{
				$height = $this->getHeight()*$width/$this->getWidth();
				$y_pos = -($height-$height_temp)/2;
				$x_pos = 0;
			}
		}

		imagecopyresampled($newImage, $this->sourceImage, $x_pos, $y_pos, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
		$this->sourceImage = $newImage;
	}

	function save($source, $ext="jpg", $comp=80){
		if($ext == "jpg"){
			imagejpeg($this->sourceImage, $source, $comp);
		}elseif($ext == "gif"){
			imagegif($this->sourceImage, $source);
		}elseif($ext == "png"){
			imagepng($this->sourceImage, $source);
		}
	}

	function output($ext="jpg"){
		if($ext == "jpg"){
			imagejpeg($this->sourceImage);
		}elseif($ext == "gif"){
			imagegif($this->sourceImage);
		}elseif($ext == "png"){
			imagepng($this->sourceImage);
		}
	}


}


//The following example will resize the image to 200x300px. If the new ratio differs then the original ratio, it will crop from center.
$image = new resizeThis();
$image->load('1.jpg');
$image->crop(200,300);//"$image->resizeToWidth(250);" This will keep the dimensions ratio and make the width 250px. "$image->scale(50);" will resize to 50%
$image->save('2.png');

//You can also put "header('Content-Type: image/jpeg');" before $image->load() and "$image->output();" after save. Its useful for on the fly thumbnail generation

]]>

Entry filed under: Php.

Resize & Crop Image with GD2 Lib. Any problems with front-end or back-end?

2 Yorum Add your own

Yorum bırakın

Trackback this post  |  Subscribe to the comments via RSS Feed


Takvim

Mart 2010
P S Ç P C C P
1234567
891011121314
15161718192021
22232425262728
293031  

Most Recent Posts