Advertisement
aznim

SimpleImage with Thumbnail generation

Nov 29th, 2011
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.70 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * File: SimpleImage.php
  5. * Author: Simon Jarvis
  6. * Copyright: 2006 Simon Jarvis
  7. * Date: 08/11/06
  8. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details:
  19. * http://www.gnu.org/licenses/gpl.html
  20. *
  21. *
  22. * function CroppedThumbnail was added by Galicz Miklรณs
  23. * Date: 29/11/11
  24. * Link: http://blackworks.org
  25. *
  26. */
  27.  
  28. class SimpleImage {
  29.  
  30.    var $image;
  31.    var $image_type;
  32.  
  33.    function load($filename) {
  34.  
  35.       $image_info = getimagesize($filename);
  36.       $this->image_type = $image_info[2];
  37.       if( $this->image_type == IMAGETYPE_JPEG ) {
  38.  
  39.          $this->image = imagecreatefromjpeg($filename);
  40.       } elseif( $this->image_type == IMAGETYPE_GIF ) {
  41.  
  42.          $this->image = imagecreatefromgif($filename);
  43.       } elseif( $this->image_type == 3 ) {
  44.  
  45.          $this->image = imagecreatefrompng($filename);
  46.       }
  47.    }
  48.    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  49.  
  50.       if( $image_type == IMAGETYPE_JPEG ) {
  51.          imagejpeg($this->image,$filename,$compression);
  52.       } elseif( $image_type == IMAGETYPE_GIF ) {
  53.  
  54.          imagegif($this->image,$filename);
  55.       } elseif( $image_type == IMAGETYPE_PNG ) {
  56.  
  57.          imagepng($this->image,$filename);
  58.       }
  59.       if( $permissions != null) {
  60.  
  61.          chmod($filename,$permissions);
  62.       }
  63.    }
  64.    function output($image_type=IMAGETYPE_JPEG) {
  65.  
  66.       if( $image_type == IMAGETYPE_JPEG ) {
  67.          imagejpeg($this->image,null,100);
  68.       } elseif( $image_type == IMAGETYPE_GIF ) {
  69.  
  70.          imagegif($this->image);
  71.       } elseif( $image_type == IMAGETYPE_PNG ) {
  72.  
  73.          imagepng($this->image);
  74.       }
  75.    }
  76.    function getWidth() {
  77.  
  78.       return imagesx($this->image);
  79.    }
  80.    function getHeight() {
  81.  
  82.       return imagesy($this->image);
  83.    }
  84.    function resizeToHeight($height) {
  85.  
  86.       $ratio = $height / $this->getHeight();
  87.       $width = $this->getWidth() * $ratio;
  88.       $this->resize($width,$height);
  89.    }
  90.  
  91.    function resizeToWidth($width) {
  92.       $ratio = $width / $this->getWidth();
  93.       $height = $this->getheight() * $ratio;
  94.       $this->resize($width,$height);
  95.    }
  96.  
  97.    function scale($scale) {
  98.       $width = $this->getWidth() * $scale/100;
  99.       $height = $this->getheight() * $scale/100;
  100.       $this->resize($width,$height);
  101.    }
  102.  
  103.    function resize($width,$height) {
  104.       $new_image = imagecreatetruecolor($width, $height);
  105.       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  106.       $this->image = $new_image;
  107.    }
  108.    
  109.    function CroppedThumbnail($thumbnail_width,$thumbnail_height) {
  110.  
  111.     $this->resizeToWidth($thumbnail_width);
  112.    
  113.     if ($this->getHeight()>$thumbnail_height) {
  114.         $this->resizeToHeight($thumbnail_height);
  115.     }
  116.    
  117.     $imgMidX = $this->getWidth()/2;
  118.     $imgMidY = $this->getHeight()/2;
  119.    
  120.    
  121.     $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
  122.     imagecopyresized($thumb, $this->image, 0, 0, ($imgMidX-($thumbnail_width/2)), ($imgMidY-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
  123.  
  124.     $this->image = $thumb;
  125. }      
  126.  
  127. }
  128. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement