Advertisement
Guest User

resize crop png jpg jpeg gif php

a guest
Dec 19th, 2011
1,562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2.  
  3. class SimpleImage {
  4.                
  5.    var $image;
  6.    var $image_type;
  7.  
  8.    function load($filename) {
  9.       $image_info = getimagesize($filename);
  10.       $this->image_type = $image_info[2];
  11.       if( $this->image_type == IMAGETYPE_JPEG ) {
  12.          $this->image = imagecreatefromjpeg($filename);
  13.       } elseif( $this->image_type == IMAGETYPE_GIF ) {
  14.          $this->image = imagecreatefromgif($filename);
  15.       } elseif( $this->image_type == IMAGETYPE_PNG ) {
  16.          $this->image = imagecreatefrompng($filename);
  17.       }
  18.    }
  19.    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $permissions=null) {
  20.       if( $image_type == IMAGETYPE_JPEG ) {
  21.          imagejpeg($this->image,$filename,$compression);
  22.       } elseif( $image_type == IMAGETYPE_GIF ) {
  23.          imagegif($this->image,$filename);        
  24.       } elseif( $image_type == IMAGETYPE_PNG ) {
  25.          imagepng($this->image,$filename);
  26.       }  
  27.       if( $permissions != null) {
  28.          chmod($filename,$permissions);
  29.       }
  30.    }
  31.    function output($image_type=IMAGETYPE_JPEG) {
  32.       if( $image_type == IMAGETYPE_JPEG ) {
  33.          imagejpeg($this->image);
  34.       } elseif( $image_type == IMAGETYPE_GIF ) {
  35.          imagegif($this->image);        
  36.       } elseif( $image_type == IMAGETYPE_PNG ) {
  37.          imagepng($this->image);
  38.       }  
  39.    }
  40.    function getWidth() {
  41.       return imagesx($this->image);
  42.    }
  43.    function getHeight() {
  44.       return imagesy($this->image);
  45.    }
  46.    function resizeToHeight($height) {
  47.       $ratio = $height / $this->getHeight();
  48.       $width = $this->getWidth() * $ratio;
  49.       $this->resize($width,$height);
  50.    }
  51.    function resizeToWidth($width) {
  52.       $ratio = $width / $this->getWidth();
  53.       $height = $this->getHeight() * $ratio;
  54.       $this->resize($width,$height);
  55.    }
  56.    function scale($scale) {
  57.       $width = $this->getWidth() * $scale/100;
  58.       $height = $this->getheight() * $scale/100;
  59.       $this->resize($width,$height);
  60.    }
  61.    function resize($width,$height) {
  62.         $new_image = imagecreatetruecolor($width, $height);
  63.         $bg = imagecolorallocate($new_image,255,255,255);
  64.         imagefill($new_image,0,0,$bg);
  65.         imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  66.         $this->image = $new_image;  
  67.    }  
  68.     function crop($filename,$width,$height)
  69.     {
  70.         list($w, $h, $type, $attr) = getimagesize($filename);
  71.  
  72.         if( $this->image_type == IMAGETYPE_JPEG ) {
  73.             $src_im = imagecreatefromjpeg($filename);
  74.         } elseif( $this->image_type == IMAGETYPE_GIF ) {
  75.             $src_im = imagecreatefromgif($filename);        
  76.         } elseif( $this->image_type == IMAGETYPE_PNG ) {
  77.             $src_im = imagecreatefrompng($filename);
  78.         }  
  79.        
  80.        
  81.         $src_x = '0';   // begin x
  82.         $src_y = '0';   // begin y
  83.         $src_w = $width; // width
  84.         $src_h = $height; // height
  85.         $dst_x = '0';   // destination x
  86.         $dst_y = '0';   // destination y
  87.  
  88.         $new_image = imagecreatetruecolor($src_w, $src_h);
  89.         $white = imagecolorallocate($new_image, 255, 255, 255);
  90.         imagefill($new_image, 0, 0, $white);
  91.  
  92.         imagecopy($new_image, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  93.         $this->image = $new_image;
  94.     }
  95. }
  96.  
  97. $filename= "test.png";
  98.  
  99. $thumb = new SimpleImage();
  100. $thumb->load($filename);
  101. $thumb->resizeToWidth(120);
  102. $thumb->crop($filename, 120, 120);
  103. $thumb->save("test".$filename);
  104.  
  105.  
  106.  
  107.  
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement