Resize your Image using simpleImage class in php

If you have problem to resize your  image in php then using this simple class file and resize your image very simple.
What you need to do:
(a)-You just need to copy the entire code below and  paste it into a file name Simpleimage.php .

class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}

function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename); }
if( $permissions != null) {
chmod($filename,$permissions);
}
}

function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
}elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);

} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}

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

}

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

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

function resizeToWidth($width) {
$totW= $this->getWidth();

if($totW < $width){ $width = $totW; } $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) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}

}


(b)-Now you need to include this class file.
       (1)--If you are using core php then include like this eg-- include("Simpleimage.php");. 
       
       (2)--If you are using code igneter then simple copy this class into the path
               application/library/Simpleimage.php
               and call it into you controller like this
               $this->load->library('Simpleimage');

(c)-Now you have to be need to make a object of this class like this in both core php and codeigneter

     $upImg= new SimpleImage();

(d)-Then simple select the option of resize.If you want to resize image only on width then     
            $upImg->load($_FILES['uploadImage']['tmp_name']);
            $upImg->resizeToWidth(1024);
            $upImg->save($up_extralarge); 


   Resize Option :

       resizeToWidth(234) ----> With this function image is resize only on 234 width and height is select  appropiate ratio. 

       resizeToHeight(78) ----> With this function image is resize only on 78 height and width is select appropiate ratio. 

       resize(32,76)  ----> With this function image is resize only on 32 width and 78 height.


    
If you like this post please don't forgot to notify me to post a comment .

Happy coding guys  :)

Post a Comment

Previous Post Next Post