Resizing images with PHP

To resize image or to create thumbnail of image at run time we can use following code.
Step 1 :
create a file named resizeimageclass.php and write following code in it.

1 class ResizeImage {
2
3    var $image;
4    var $image_type;
5
6    function load($filename) {
7       $image_info = getimagesize($filename);
8       $this->image_type = $image_info[2];
9       if( $this->image_type == IMAGETYPE_JPEG ) {
10          $this->image = imagecreatefromjpeg($filename);
11       } elseif( $this->image_type == IMAGETYPE_GIF ) {
12          $this->image = imagecreatefromgif($filename);
13       } elseif( $this->image_type == IMAGETYPE_PNG ) {
14          $this->image = imagecreatefrompng($filename);
15       }
16    }
17    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
18       if( $image_type == IMAGETYPE_JPEG ) {
19          imagejpeg($this->image,$filename,$compression);
20       } elseif( $image_type == IMAGETYPE_GIF ) {
21          imagegif($this->image,$filename);
22       } elseif( $image_type == IMAGETYPE_PNG ) {
23          imagepng($this->image,$filename);
24       }
25       if( $permissions != null) {
26          chmod($filename,$permissions);
27       }
28    }
29    function output($image_type=IMAGETYPE_JPEG) {
30       if( $image_type == IMAGETYPE_JPEG ) {
31          imagejpeg($this->image);
32       } elseif( $image_type == IMAGETYPE_GIF ) {
33          imagegif($this->image);
34       } elseif( $image_type == IMAGETYPE_PNG ) {
35          imagepng($this->image);
36       }
37    }
38    function getWidth() {
39       return imagesx($this->image);
40    }
41    function getHeight() {
42       return imagesy($this->image);
43    }
44    function resizeToHeight($height) {
45       $ratio = $height / $this->getHeight();
46       $width = $this->getWidth() * $ratio;
47       $this->resize($width,$height);
48    }
49    function resizeToWidth($width) {
50       $ratio = $width / $this->getWidth();
51       $height = $this->getheight() * $ratio;
52       $this->resize($width,$height);
53    }
54    function scale($scale) {
55       $width = $this->getWidth() * $scale/100;
56       $height = $this->getheight() * $scale/100;
57       $this->resize($width,$height);
58    }
59    function resize($width,$height) {
60       $new_image = imagecreatetruecolor($width, $height);
61       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
62       $this->image = $new_image;
63    }
64 }

Step 2 :
Create a file named createthumb.php and write following code in it.

1 if(isset($_REQUEST['width']) && $_REQUEST['width'] != ''){
2         $width = $_REQUEST['width'];
3     }else {
4         $width = '170';
5     }
6
7     if(isset($_REQUEST['height']) && $_REQUEST['height'] != ''){
8         $height = $_REQUEST['height'];
9     }else {
10         $height = '';
11     }
12
13     if($height == ''){
14         if(isset($_REQUEST['path']) && $_REQUEST['path'] != ''){
15             header('Content-Type: image/jpeg');
16             include(resizeimageclass.php');
17             $image = new ResizeImage ();
18             $image->load($_REQUEST['path']);
19             $image->resizeToWidth($width);
20             $image->output();
21         }
22     }else if($height != ''){
23         if(isset($_REQUEST['path']) && $_REQUEST['path'] != ''){
24             header('Content-Type: image/jpeg');
25             include(resizeimageclass.php');
26             $image = new ResizeImage ();
27             $image->load($_REQUEST['path']);
28             $image->resize($width, $height);
29             $image->output();
30         }
31     }

Step 3 :
To display thumbnail write the following code in the file where you want to display image.
File name index.php

1 <img src="createthumb.php?width=80&height=80&path=images/imagefilename.jpg"  />

This will resize the image to 80 x 80,you can replace this with any width and height you want,imagefilename.jpg is the name of image which we want to resize.

Advertisements
By Rz Rasel Posted in Php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s