Php Resize Image Using OOP
<![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.
1. Anonim | 18,03,2010, 17:25
2. Umit Unal | 18,03,2010, 17:25
Merhaba,
http://phpthumb.gxdlabs.com/ bunu da inceleyin derim.Oldukça ba?ar?l?d?r.
3. Anonim | 18,03,2010, 17:25