IDIOT DEVELOPER

Image Resizing in PHP
Image Resizing in PHP
DEMO   DOWNLOAD

Images are one of the most important part of any website, so it is important is keep them within a definite boundary. If images are not in boundary they become distorted and website does feel good. So it is important to keep images in a proper manner. There are time when there are images which are not in proper size, we need to correct their size. So this can be achieved using PHP.

 

In my previous tutorial Simple Image Resizing in PHP i have developed a simple PHP script that resize image properly. But in this tutorial are going to resize the image and save them also.

 

Introduction

First of all we are going to make a rough sketch or we can say rough skeleton of the Resize Script in PHP. Please NOTE that we are going to use Object Oriented PHP here.

 


class Image{
 private $img; //image file name.
 private $height; //image height.
 private $width; //image width.
 private $type; //image type.
 private $resource_id; //resource id of the original image.
 private $new_img; //resource id new image.

  function __construct($img_file){
  }

  //checking the image orientation
  function image_orientation(){
  }

  //opening the image according to its type.
  function open(){
  }

  /**
  * Resize the image. It has only two modes
  * exact = resize image same as the dimension provided.
  * auto = resize image try to keep the original image ascept ratio.
  **/
  function resize($width, $height, $opt){
  }

  //Create the new image of the give height width.
  function create_new_image(){
  }

  //Finally save the image.
  function save(){
  }

}

First of all we are going to take the image as the constructor argument and then find the width, height, and type from it, and store the values in the class variables.

 

The PHP function getimagesize() is used for getting an entire list of image properties, including width, height and etc.

 


function __construct($img_file){
  $this->img = $img_file;
  $img_array = getimagesize($this->img);
  $this->width = $img_array[0];
  $this->height = $img_array[1];
  $this->type = $img_array['mime'];
}

 

Image Orientation

This method returns the orientation of image with the help of the dimensions of the image from the class variables.

 


function image_orientation(){
  if($this->width > $this->height){
    return 'landscape';
  }else if($this->width < $this->height){
    return 'potrait';
  }else if($this->width == $this->height){
    return 'square';
  }else{
    return 'none';
  }
}

 

Opening the Image

This method just take the image type from the class variable and then compare it with different image format, so depending on what file type is passed in, the script should determine what GD Library function it calls to open the image.

 


function open(){
$type = $this->type;

switch ($type) {
  case 'image/png':
    $img = imagecreatefrompng($this->img);
    break;
  case 'image/jpg':
  case 'image/jpeg':
    $img = imagecreatefromjpeg($this->img);
    break;
  case 'image/gif':
    $img = imagecreatefromgif($this->img);
    break;
  default:
    $img = false;
    break;
  }
  $this->resource_id = $img;
}

 

Resizing the Image

Now the next step is to resize the image according the dimension provided by the user. The resizing of the image can be done via two ways, first just make an exact copy of the image with new dimension. The demerit about the first way is that is doesn’t consider the aspect ratio of the image. The second way is the automatic way. this way try to resize the image according to the dimension and the aspect ration of the image.

 

After calculating the accurate dimension it call a new function create_new_image(). This new function takes the new dimension and create the image.

 


function resize($width, $height, $opt){
  if($opt == 'exact'){
    $this->create_new_image($width, $height);

  }else if($opt == 'auto'){
    $image_orientation = $this->image_orientation();

    if($image_orientation == 'landscape'){
      $p = ($width / $this->width);
      $new_width = $this->width * $p;
      $new_height = $this->height * $p;

    }else if($image_orientation == 'potrait'){
      $p = ($height / $this->height);
      $new_width = $this->width * $p;
      $new_height = $this->height * $p;

    }else if($image_orientation == 'square'){
      $p = ($height / $this->height);
      $new_width = $this->width * $p;
      $new_height = $this->height * $p;
    }else{
      echo 'Some Error Encountered!';
      exit();
    }

    $this->create_new_image($new_width, $new_height);
  }
}

 

Creating new Image

Now we have the dimension for the new image so, are are going to create the target image layer using the PHP imagecreatetruecolor() function. This function create a blank image and return a resource identifier which are going to use later on for the creating new image.

 

Now we are provided with a list of details about the source and the target image, that are used in image resize process.  These are – source and target layer resource id, dimensions for denoting width and height of the original image and target image layer.

 

Using the above details we will copy the portion of the original image and reassemble it on the new blank image. For that we are going to use the PHP imagecopyresampled() function.

 


//Create the new image of the give height width.
function create_new_image($width, $height){
  $new_img = imagecreatetruecolor($width, $height);
  imagealphablending($new_img, false);
  imagesavealpha($new_img, true);
  imagecopyresampled($new_img, $this->resource_id, 0,0,0,0, $width, $height, $this->width, $this->height);
  $this->new_img = $new_img;
}

 

Saving the image

Now the last thing remaining is saving the new formed image. For saving we are going to need two things – resource id of the new image, and the name of the new image.

 


//Finally save the image.
function save(){
  switch ($this->type) {
    case 'image/png':
      imagepng($this->new_img, 'new-img.png');
      break;
    case 'image/jpg':
    case 'image/jpeg':
      imagejpeg($this->new_img, 'new-img.jpg');
      break;
    case 'image/gif':
      imagegif($this->new_img, 'new-img.gif');
      break;
    default:
      break;
  }
}

 

Conclusion

I hope you all liked my tutorial. If you have any problem then comment below or contact me.

Leave a Reply

Your email address will not be published. Required fields are marked *