[ Index ]
 

Code source de Plume CMS 1.2.2

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/manager/inc/ -> class.thumbnail.php (source)

   1  <?php
   2  /*
   3  # ***** BEGIN LICENSE BLOCK *****
   4  # Version: MPL 1.1/GPL 2.0/LGPL 2.1
   5  #
   6  # The contents of this file are subject to the Mozilla Public License Version
   7  # 1.1 (the "License"); you may not use this file except in compliance with
   8  # the License. You may obtain a copy of the License at
   9  # http://www.mozilla.org/MPL/
  10  #
  11  # Software distributed under the License is distributed on an "AS IS" basis,
  12  # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13  # for the specific language governing rights and limitations under the
  14  # License.
  15  #
  16  # The Original Code is Plume CMS.
  17  #
  18  # The Initial Developer of the Original Code is
  19  # Loic d'Anterroches
  20  # 
  21  # Portions created by the Initial Developer are Copyright (C) 2004
  22  # the Initial Developer. All Rights Reserved.
  23  #
  24  # Contributor(s):
  25  # Olivier Meunier.
  26  #
  27  # Alternatively, the contents of this file may be used under the terms of
  28  # either the GNU General Public License Version 2 or later (the "GPL"), or
  29  # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30  # in which case the provisions of the GPL or the LGPL are applicable instead
  31  # of those above. If you wish to allow use of your version of this file only
  32  # under the terms of either the GPL or the LGPL, and not to allow others to
  33  # use your version of this file under the terms of the MPL, indicate your
  34  # decision by deleting the provisions above and replace them with the notice
  35  # and other provisions required by the GPL or the LGPL. If you do not delete
  36  # the provisions above, a recipient may use your version of this file under
  37  # the terms of any one of the MPL, the GPL or the LGPL.
  38  #
  39  # ***** END LICENSE BLOCK ***** */
  40  
  41  /**
  42   * Class to manage the thumbnails
  43   * It provides also a static method to get the gd library version. 
  44   */
  45  class thumbnail 
  46  {
  47      var $_thumbdir = ''; /**< Path to the thumbnail folder. */
  48      var $_thumbfile = ''; /**< Path to the last created thumbnail. */
  49  
  50      /**
  51       * Init the thumbnail class. 
  52       */
  53      function thumbnail($thumbdir = '')
  54      {
  55      $this->_thumbdir = $thumbdir;
  56      }
  57  
  58      /**
  59       * Get the name of a thumbnail from the image name, the image
  60       * name can include a path this is just a md5() operation.
  61       * If the height and the width are given, the info is used to
  62       * generate the name, else not.
  63       */
  64      function getName($img, $w=0, $h=0)
  65      {
  66      $name = md5($img);
  67      if ($w>0 && $h>0) {
  68          $name .= '-'.$w.'x'.$h;
  69      }
  70      $name .= '.jpg';
  71      return $name;
  72      }
  73   
  74      /**
  75       * Get the path to the thumbnail.
  76       */
  77      function getPath($img, $w=0, $h=0)
  78      {
  79      return $this->_thumbdir.'/'.$this->getName($img, $w, $h);
  80      }
  81  
  82      /**
  83       * Get the size of the last created thumbnail
  84       * return the same results as the builtin getimagesize PHP function.
  85       */
  86      function getSize() 
  87      {
  88      if (!empty($this->_thumbfile)) {
  89          return getimagesize($this->_thumbfile);
  90      } else {
  91          return false;
  92      }
  93      }
  94  
  95      /**
  96       * Create thumbnail of an image, proportions are kept.
  97       *
  98       * @param string Path to source image
  99       * @param string Path to the thumbnail image
 100       * @param int Max width of the thumbnail
 101       * @param int Max height of the thumbnail
 102       * @return bool Success or not
 103       */
 104      function create($src, $dst, $w=120, $h=200)
 105      {
 106      if (!file_exists($src)) {
 107          return false;
 108      }
 109      
 110      if (($size = @getimagesize($src)) === false) {
 111          return false;
 112      }
 113      
 114      $type = $size[2];
 115      $H = $size[1];
 116      $W = $size[0];
 117      
 118      if ($type == '1') {
 119          $function = 'imagecreatefromgif';
 120      } elseif ($type == '2') {
 121          $function = 'imagecreatefromjpeg';
 122      } elseif ($type == '3') {
 123          $function = 'imagecreatefrompng';
 124      } else {
 125          return false;
 126      }
 127      if (!function_exists($function)) {
 128          return false;
 129      }
 130  
 131      if (($img = @$function($src)) == false) {
 132          return false;
 133      }
 134  
 135      // get the zoom factors and the thumbnail height and width
 136      $rB = $H/$W;
 137      $rS = $h/$w;
 138      if (($H > $h) && ($W > $w)) {
 139          if ($rB > $rS) {
 140          $height = $h;
 141          $width  = $height/$rB;
 142          } else {
 143          $width = $w;
 144          $height = $width*$rB; 
 145          }
 146      } elseif ($H > $h) {
 147          $height = $h;
 148          $width  = $height/$rB;
 149      } elseif ($W > $w) {
 150          $width = $w;
 151          $height = $width*$rB; 
 152      } else {
 153          $height = $H;
 154          $width  = $W;
 155      } 
 156  
 157      $zx = $W/$width;
 158      $zy = $H/$height;
 159  
 160      if (thumbnail::gd_version() >= 2) {
 161          if ( ($img2 = imagecreatetruecolor(round($width),round($height)))  === false) {
 162          return false;
 163          }
 164      } else {
 165          if ( ($img2 = ImageCreate(round($width),round($height)))  === false) {
 166          return false;
 167          }    
 168      }
 169  
 170      $this->_resampleBicubic($img2,$img,0,0,0,0,$width,$height,$zx,$zy);
 171  
 172      if (@imagejpeg($img2,$dst,80) === false) {
 173          return false;
 174      }
 175      imagedestroy($img2);
 176      if (@file_exists($dst)) {
 177          $this->_thumbfile = $dst;
 178          return true;
 179      } else {
 180          $this->_thumbfile = '';
 181          return false;
 182      }
 183      }
 184      
 185      /**
 186       * Get the current GD version. Need the output buffering functions.
 187       */
 188      function gd_version() 
 189      {
 190      static $gd_version_number = null;
 191      if ($gd_version_number === null) {
 192          // Use output buffering to get results from phpinfo()
 193          // without disturbing the page we're in.  Output
 194          // buffering is "stackable" so we don't even have to
 195          // worry about previous or encompassing buffering.
 196          ob_start();
 197          phpinfo(8);
 198          $module_info = ob_get_contents();
 199          ob_end_clean();
 200          if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i",
 201                 $module_info,$matches)) {
 202          $gd_version_number = $matches[1];
 203          } else {
 204          $gd_version_number = '0';
 205          }
 206      }
 207      return $gd_version_number;
 208  
 209      }
 210  
 211      /* ========================================================================
 212       *   Private functions, should not be called from outside of the class.
 213       * ========================================================================
 214       */
 215  
 216      /**
 217       * Resample the image 
 218       * http://www.php.net/manual/en/function.imagecopyresized.php
 219       */
 220      function _resampleBicubic(&$dst, &$src, $dstx, $dsty, $srcx, $srcy, $w, $h, $zoomX, $zoomY = '')
 221      {
 222      if (!$zoomY) {
 223          $zoomY = $zoomX;
 224      }
 225      
 226      $palsize = ImageColorsTotal($src);
 227      
 228      for ($i = 0; $i<$palsize; $i++) {
 229          $colors = ImageColorsForIndex($src, $i);
 230          ImageColorAllocate($dst, $colors['red'], $colors['green'], $colors['blue']);
 231      }
 232      
 233      $zoomX2 = (int)($zoomX/2);
 234      $zoomY2 = (int)($zoomY/2);
 235      
 236      $dstX = imagesx($dst);
 237      $dstY = imagesy($dst);
 238      $srcX = imagesx($src);
 239      $srcY = imagesy($src);
 240      
 241      for ($j = 0; $j<($h-$dsty); $j++) {
 242          $sY = (int)($j*$zoomY)+$srcy;
 243          $y13 = $sY+$zoomY2;
 244          $dY = $j+$dsty;
 245          
 246          if (($sY >= $srcY) or ($dY >= $dstY) or ($y13 >= $srcY)) {
 247          break 1;
 248          }
 249          
 250          for ($i = 0; $i<($w-$dstx); $i++) {
 251          $sX = (int)($i*$zoomX)+$srcx;
 252          $x34 = $sX+$zoomX2;
 253          $dX = $i+$dstx;
 254          
 255          if (($sX >= $srcX) or ($dX >= $dstX) or ($x34 >= $srcX)) {
 256              break 1;
 257          }
 258          
 259          $c1 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $y13));
 260          $c2 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $sY));
 261          $c3 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $y13));
 262          $c4 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $sY));
 263          
 264          $r = ($c1['red']+$c2['red']+$c3['red']+$c4['red'])/4;
 265          $g = ($c1['green']+$c2['green']+$c3['green']+$c4['green'])/4;
 266          $b = ($c1['blue']+$c2['blue']+$c3['blue']+$c4['blue'])/4;
 267          
 268          ImageSetPixel($dst, $dX, $dY, ImageColorClosest($dst, $r, $g, $b));
 269          }
 270      }
 271      }
 272  }
 273  ?>


Généré le : Mon Nov 26 11:57:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics