Php is a powerful language. If you are thinking whether Php supports image processing like the other language Java, C# etc. than i must tell you, YES! Php have a powerful image processing library called as Php GD Library which contains many different useful functions that can help you in processing images.
Here i am going to introduce you a simple function through which you can resize any image easily. All the php functions used in this function are from php gd library. Its a really small php function with complete explanation. Just call the function with few parameters ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
function image_resize($imageName,$newName,$newWidth,$newHeight)
{
      $imginfo = getimagesize($imageName); //get information about image
      $type = $imginfo[2]; //third element of array is image type
      if( $type == IMAGETYPE_JPEG ) {  //if image is jpeg type
         $image = imagecreatefromjpeg($imageName);
      } elseif( $type == IMAGETYPE_GIF ) {  // if image is gif type
         $image = imagecreatefromgif($imageName);
      } elseif( $type == IMAGETYPE_PNG ) {  //if image is png type
         $image = imagecreatefrompng($imageName);
      }
      $new_img = imagecreatetruecolor($newWidth, $newHeight); //create a new image
      imagecopyresampled($new_img, $image, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($image), imagesy($image)); 
      imagejpeg($new_img,$newName,75); //save the image as jpeg
}
image_resize("original.jpg","output.jpg","100","150"); //how to use
?>
<img src="original.jpg" />
<img src="output.jpg" />

One Response so far.

  1. Interesting post.You can follow this too.
    ?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) {
    $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;
    }

    }
    ?>