Advertisement
Guest User

Image Resize

a guest
Jan 5th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. <?php class ResizeImage {
  2.  
  3.    var $image;
  4.    var $image_type;
  5.  
  6.    function load($filename) {
  7.  
  8.       $image_info = getimagesize($filename);
  9.       $this->image_type = $image_info[2];
  10.      
  11.       if( $this->image_type == IMAGETYPE_JPEG ) {
  12.  
  13.          $this->image = imagecreatefromjpeg($filename);
  14.       } elseif( $this->image_type == IMAGETYPE_GIF ) {
  15.  
  16.          $this->image = imagecreatefromgif($filename);
  17.       } elseif( $this->image_type == IMAGETYPE_PNG ) {
  18.  
  19.          $this->image = imagecreatefrompng($filename);
  20.       }
  21.    }
  22.    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  23.  
  24.       if( $image_type == IMAGETYPE_JPEG ) {
  25.          imagejpeg($this->image,$filename,$compression);
  26.       } elseif( $image_type == IMAGETYPE_GIF ) {
  27.  
  28.          imagegif($this->image,$filename);
  29.       } elseif( $image_type == IMAGETYPE_PNG ) {
  30.  
  31.          imagepng($this->image,$filename);
  32.       }
  33.       if( $permissions != null) {
  34.  
  35.          chmod($filename,$permissions);
  36.       }
  37.    }
  38.    function output($image_type=IMAGETYPE_JPEG) {
  39.  
  40.       if( $image_type == IMAGETYPE_JPEG ) {
  41.          imagejpeg($this->image);
  42.       } elseif( $image_type == IMAGETYPE_GIF ) {
  43.  
  44.          imagegif($this->image);
  45.       } elseif( $image_type == IMAGETYPE_PNG ) {
  46.  
  47.          imagepng($this->image);
  48.       }
  49.    }
  50.    function getWidth() {
  51.  
  52.       return imagesx($this->image);
  53.    }
  54.    function getHeight() {
  55.  
  56.       return imagesy($this->image);
  57.    }
  58.    function resizeToHeight($height) {
  59.  
  60.       $ratio = $height / $this->getHeight();
  61.       $width = $this->getWidth() * $ratio;
  62.       $this->resize($width,$height);
  63.    }
  64.  
  65.    function resizeToWidth($width) {
  66.       $ratio = $width / $this->getWidth();
  67.       $height = $this->getheight() * $ratio;
  68.       $this->resize($width,$height);
  69.    }
  70.  
  71.    function scale($scale) {
  72.       $width = $this->getWidth() * $scale/100;
  73.       $height = $this->getheight() * $scale/100;
  74.       $this->resize($width,$height);
  75.    }
  76.  
  77.    function resize($width,$height) {
  78.       $new_image = imagecreatetruecolor($width, $height);
  79.       if ($this->image_type == IMAGETYPE_PNG){
  80.         imagealphablending($new_image, false);
  81.         imagesavealpha($new_image, true);
  82.         $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
  83.         imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
  84.       }
  85.       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  86.       $this->image = $new_image;
  87.    }      
  88.  
  89. }
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement