Resize image with php



User Rating:  / 17
PoorBest 
Details

The following php class resizes the image to a specific size.

View source
<?php
 
/*
 * www.developerpages.gr
 * 
 * Resize image with php
 * 
 */
class ResizeImage {
    var $image;
    var $image_type;
 
    function resize($filename$width$height$newfilename$compression = 75) {
        $image_info = getimagesize($filename);
        $this->image_type = $image_info[2];
        switch ($this->image_type) {
            case IMAGETYPE_JPEG:
                $this->image = imagecreatefromjpeg($filename);
                break;
            case IMAGETYPE_GIF:
                $this->image = imagecreatefromgif($filename);
                break;
            case IMAGETYPE_PNG:
                $this->image = imagecreatefrompng($filename);
                break;
        }
 
        $new_image = imagecreatetruecolor($width$height);
        imagecopyresampled($new_image$this->image0000$width$height, imagesx($this->image), imagesy($this->image));
        $this->image = $new_image;
 
        switch ($this->image_type) {
            case IMAGETYPE_JPEG:
                imagejpeg($this->image$newfilename$compression);
                break;
            case IMAGETYPE_GIF:
                imagegif($this->image$newfilename);
                break;
            case IMAGETYPE_PNG:
                imagepng($this->image$newfilename);
                break;
        }
    }
 
}
 
?>
 

call php code for resizing image:

View source
<?php
   include('ResizeImage.php');
   $image = new ResizeImage();
   $image->resize('image1.jpg'800,600'image2.jpg');
?>
 


You have no rights to post comments

   

Login  

   
   

     

© Developerpages