[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/imagekit/src/ -> lmbImageGd.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/imagekit/src/lmbImageLibrary.class.php');
  10  
  11  /**

  12   * class lmbImageGd.

  13   *

  14   * @package imagekit

  15   * @version $Id: lmbImageGd.class.php 5945 2007-06-06 08:31:43Z pachanga $

  16   */
  17  class lmbImageGd extends lmbImageLibrary
  18  {
  19    var $image;
  20    var $gd_version;
  21    var $option_re = '/(<tr.*%s.*<\/tr>)/Ui';
  22    var $create_func = '';
  23    var $resize_func = '';
  24    var $image_types = array(1 => 'GIF', 2 => 'JPEG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF(intel byte order)', 8 => 'TIFF(motorola byte order)', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF');
  25  
  26    function lmbImageGd()
  27    {
  28      if (!extension_loaded('gd'))
  29      {
  30        $this->library_installed = false;
  31        return;
  32      }
  33  
  34      $this->library_installed = true;
  35  
  36      $this->_determineGdOptions();
  37  
  38      if ($this->gd_version >= 2)
  39      {
  40        $this->create_func = 'ImageCreateTrueColor';
  41        $this->resize_func = 'ImageCopyResampled';
  42      }
  43      else
  44      {
  45        $this->create_func = 'ImageCreate';
  46        $this->resize_func = 'ImageCopyResized';
  47      }
  48    }
  49  
  50    protected function _determineGdOptions()
  51    {
  52      if (function_exists('gd_info'))
  53        $this->_determineGdOptionsThroughGdInfo();
  54      else
  55        $this->_determineGdOptionsThroughPhpInfo();
  56    }
  57  
  58    protected function _determineGdOptionsThroughGdInfo()
  59    {
  60      $info = gd_info();
  61      $this->gd_version = $this->_getNumericGdVersion($info['GD Version']);
  62  
  63      if ($info['GIF Read Support'])
  64        $this->read_types[] = 'GIF';
  65  
  66      if ($info['GIF Create Support'])
  67        $this->create_types[] = 'GIF';
  68  
  69      if ($info['JPG Support'])
  70        $this->read_types[] = $this->create_types[] = 'JPEG';
  71  
  72      if ($info['PNG Support'])
  73        $this->read_types[] = $this->create_types[] = 'PNG';
  74    }
  75  
  76    protected function _determineGdOptionsThroughPhpInfo()
  77    {
  78      ob_start();
  79      phpinfo();
  80      $phpinfo = ob_get_contents();
  81      ob_end_clean();
  82      $this->gd_version = $this->_getNumericGdVersion($this->_getGdOption($phpinfo, 'GD Version'));
  83  
  84      if (strpos($this->_getGdOption($phpinfo, 'GIF Read Support'), 'enabled') !== false)
  85        $this->read_types[] = 'GIF';
  86  
  87      if (strpos($this->_getGdOption($phpinfo, 'GIF Create Support'), 'enabled') !== false)
  88        $this->cerate_types[] = 'GIF';
  89  
  90      if (strpos($this->_getGdOption($phpinfo, 'JPG Support'), 'enabled') !== false)
  91        $this->read_types[] = $this->create_types[] = 'JPEG';
  92  
  93      if (strpos($this->_getGdOption($phpinfo, 'PNG Support'), 'enabled') !== false)
  94        $this->read_types[] = $this->create_types[] = 'PNG';
  95    }
  96  
  97    protected function _getGdOption($phpinfo, $option)
  98    {
  99      $re = sprintf($this->option_re, $option);
 100      preg_match($re, $phpinfo, $matches);
 101      return $matches[1];
 102    }
 103  
 104    protected function _getNumericGdVersion($str)
 105    {
 106      $re = "/[^\.\d]*([\.\d]+)[^\.\d]*/";
 107      preg_match($re, $str, $matches);
 108      return (float)$matches[1];
 109    }
 110  
 111    protected function _getImage()
 112    {
 113      if($this->image)
 114        return $this->image;
 115  
 116      if (!$this->input_file_type)
 117      {
 118        $info = getimagesize($this->input_file);
 119        $this->input_file_type = $this->image_types[$info[2]];
 120      }
 121  
 122      $create_func = "ImageCreateFrom{$this->input_file_type}";
 123      $this->image = $create_func($this->input_file);
 124  
 125      return $this->image;
 126    }
 127  
 128    protected function _setImage($image)
 129    {
 130      $this->image = $image;
 131    }
 132  
 133    function parseHexColor($hex)
 134    {
 135      $length = strlen($hex);
 136      $color['red'] = hexdec(substr($hex, $length - 6, 2));
 137      $color['green'] = hexdec(substr($hex, $length - 4, 2));
 138      $color['blue'] = hexdec(substr($hex, $length - 2, 2));
 139      return $color;
 140    }
 141  
 142    function reset()
 143    {
 144      $this->image = null;
 145    }
 146  
 147    function commit()
 148    {
 149      $image = $this->_getImage();
 150  
 151      $create_func = "Image{$this->output_file_type}";
 152      $create_func($image, $this->output_file);
 153  
 154      $this->reset();
 155    }
 156  
 157    function resize($params)
 158    {
 159      $image = $this->_getImage();
 160  
 161      $src_width = imagesx($image);
 162      $src_height = imagesy($image);
 163  
 164      list($dst_width, $dst_height) = $this->getDstDimensions($src_width, $src_height, $params);
 165  
 166      $create_func = $this->create_func;
 167      $resize_func = $this->resize_func;
 168  
 169      $dest_image = $create_func($dst_width, $dst_height);
 170      $resize_func($dest_image, $image, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
 171  
 172      imagedestroy($image);
 173  
 174      $this->_setImage($dest_image);
 175    }
 176  
 177    function rotate($angle, $bg_color)
 178    {
 179      $image = $this->_getImage();
 180  
 181      $color = $this->parseHexColor($bg_color);
 182      $background_color = imagecolorallocate($image, $color['red'], $color['green'], $color['blue']);
 183      $this->_setImage(imagerotate($image, $angle, $background_color));
 184    }
 185  
 186    function flip($params)
 187    {
 188      $image = $this->_getImage();
 189  
 190      $x = imagesx($image);
 191      $y = imagesy($image);
 192  
 193      $create_func = $this->create_func;
 194      $resize_func = $this->resize_func;
 195  
 196      $dest_image = $create_func($x, $y);
 197  
 198      if ($params == LIMB_IMAGE_LIBRARY_FLIP_HORIZONTAL)
 199        $resize_func($dest_image, $image, 0, 0, $x, 0, $x, $y, -$x, $y);
 200  
 201      if ($params == LIMB_IMAGE_LIBRARY_FLIP_VERTICAL)
 202        $resize_func($dest_image, $image, 0, 0, 0, $y, $x, $y, $x, -$y);
 203  
 204      imagedestroy($image);
 205      $this->_setImage($dest_image);
 206    }
 207  
 208    function cut($x, $y, $w, $h, $bg_color)
 209    {
 210      $image = $this->_getImage();
 211  
 212      $color = $this->parseHexColor($bg_color);
 213      $background_color = imagecolorallocate($image, $color['red'], $color['green'], $color['blue']);
 214      imagefill($image, 0, 0, $background_color);
 215  
 216      $create_func = $this->create_func;
 217      $resize_func = $this->resize_func;
 218  
 219      $dest_image = $create_func($w, $h);
 220      $resize_func($dest_image, $image, 0, 0, $x, $y, $w, $h, $w, $h);
 221  
 222      imagedestroy($image);
 223      $this->_setImage($dest_image);
 224    }
 225  }
 226  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7