Resize Image Easily using php Image Resize class

Here i will show you how to  Resize Image Easily using php Image Resize class. Let us learn how to create a php image resiz class.

Resize Image Easily using php Image Resize class

Resize Image Easily using php Image Resize class

Here let us check step by step process of Resize Image Easily using php Image Resize class.
If you are a person who is owner of a website or a web programming person,or whatever you may be. If you are dealing with a website that have so many images and taking So much time to load.this class will helpfully to you to reduce the image size and load your website fast.
I promises you can Resize Image Easily using php Image Resize class. Let us check how it can be done.
Before explaining the  php Image Resize class I will show you how to use this class to your page.
First create a page. Let’s name it as index.php and enter the code which shown below.


<!--?php require('img_resize.php'); $obj = new image_resize(); $obj--->set_img_max_width(500);
$obj->set_img_max_height(500);
$obj->image_path('pic1.jpg');
$obj->image_save_path('rpic1.jpg');
$obj->img_resize();

?>

 $obj = new image_resize(); 

this will  initialize  the  class.

 $obj->set_img_max_width(500); 

is used for specify the maximum width of your image.

 $obj->set_img_max_height(500); 

is used for specify the maximum height of your image.

 $obj->image_path('pic1.jpg');  

is used for specify the name of the image that you want to resize.

$obj->image_save_path('rpic1.jpg');  

is used for specify the name of the resized image.

 $obj->img_resize();  

this will start image resize procedure.

Next is the  php Image Resize class. For that first create a blank php file and save it as img_resize.php.

Then paste the code shown below ( which is the  php Image Resize class ) to that php file.


<!--?php class image_resize { var $set_img_max_width; var $set_img_max_height; var $img_new_width; var $img_new_height; var $mime; var $image; var $img_width; var $img_height; var $img_path; var $img_save_path; var $img; function image_path($img_path) { $this--->img_path = $img_path;
}
function image_save_path($img_save_path)
{
$this->img_save_path = $img_save_path;
}
function set_img_max_width($img_width)
{
$this->set_img_max_width = $img_width;
}
function set_img_max_height($img_height)
{
$this->set_img_max_height = $img_height;
}
function get_mime()
{
$img_data = getimagesize($this->img_path);
$this->mime = $img_data['mime'];
}
function image_create()
{
switch($this->mime)
{
case 'image/jpeg':
$this->image = imagecreatefromjpeg($this->img_path);
break;

case 'image/gif':
$this->image = imagecreatefromgif($this->img_path);
break;

case 'image/png':
$this->image = imagecreatefrompng($this->img_path);
break;
}
}
function img_resize()
{
set_time_limit(0);
$this->get_mime();
$this->image_create();
$this->img_width = imagesx($this->image);
$this->img_height = imagesy($this->image);
$this->img_set_dimension();
$resized_image = imagecreatetruecolor($this->img_new_width,$this->img_new_height);
imagecopyresampled($resized_image, $this->image, 0, 0, 0, 0, $this->img_new_width, $this->img_new_height,$this->img_width, $this->img_height);
imagejpeg($resized_image,$this->img_save_path);

}

function img_set_dimension()
{

if($this->img_width==$this->img_height)
{
$case = 'c1';
}
elseif($this->img_width > $this->img_height)
{
$case = 'c2';
}
else
{
$case = 'c3';
}

if($this->img_width>$this->set_img_max_width && $this->img_height>$this->set_img_max_height)
{
$cond = 'c1';
}
elseif($this->img_width>$this->set_img_max_width && $this->img_height<=$this->set_img_max_height)
{
$cond = 'c1';
}
else
{
$cond = 'c3';
}

switch($case)
{
case 'c1':
$this->img_new_width = $this->set_img_max_width;
$this->img_new_height = $this->set_img_max_height;
break;
case 'c2':
$img_ratio = $this->img_width/$this->img_height;
$amount = $this->img_width - $this->set_img_max_width;
$this->img_new_width = $this->img_width - $amount;
$this->img_new_height = $this->img_height - ($amount/$img_ratio);
break;
case 'c3':
$img_ratio = $this->img_height/$this->img_width;
$amount = $this->img_height - $this->set_img_max_height;
$this->img_new_height = $this->img_height - $amount;
$this->img_new_width = $this->img_width - ($amount/$img_ratio);
break;
}

}
}

?>

This class will do your rest of the job. It will create a resized version of your image. Let us check the functionality of this class briefly.


function img_resize()

The  img_resize() function is the function which is used for resizing the image. In this function,

 $this->get_mime(); 

is used for get mime header imformation of the image.

 $this->image_create(); 

is used for creating a blank imae file.

 $this->img_set_dimension(); 

is used for calculating the diminutions of the resized image.

So By following this methore you can easly Resize Image Easily using php Image Resize class.

Resize Image Easily using php Image Resize class code Download