[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/imagekit/src/ -> lmbImageNetpbm.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  lmb_require('limb/fs/src/lmbFs.class.php');
  11  
  12  // Read utilities

  13  define('JPEGTOPNM', 'jpegtopnm');
  14  define('GIFTOPNM', 'giftopnm');
  15  define('PNGTOPNM', 'pngtopnm');
  16  define('BMPTOPNM', 'bmptopnm');
  17  define('TIFFTOPNM', 'tifftopnm');
  18  
  19  // Write utilities

  20  define('PNMTOJPEG', 'pnmtojpeg');
  21  define('PNMTOGIF', 'ppmtogif');
  22  define('PNMTOPNG', 'pnmtopng');
  23  define('PNMTOBMP', 'ppmtobmp');
  24  define('PNMTOTIFF', 'pnmtotiff');
  25  
  26  // Editor utilities

  27  define('PNMSCALE', 'pnmscale');
  28  define('PNMCUT', 'pamcut');
  29  define('PNMFLIP', 'pnmflip');
  30  define('PNMROTATE', 'pnmrotate');
  31  define('PNMQUANT', 'ppmquant');
  32  define('PNMMAKE', 'ppmmake');
  33  define('PNMCOMP', 'pnmpaste');
  34  
  35  if(!defined('NETPBM_LIB_DIR'))
  36  {
  37    if(lmbSys :: osType() == 'win32')
  38      define('NETPBM_LIB_DIR', 'c:/netpbm/');
  39    else
  40      define('NETPBM_LIB_DIR', '/usr/local/netpbm/bin/');
  41   }
  42  
  43  lmb_require('limb/imagekit/src/lmbImageLibrary.class.php');
  44  
  45  /**

  46   * class lmbImageNetpbm.

  47   *

  48   * @package imagekit

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

  50   */
  51  class lmbImageNetpbm extends lmbImageLibrary
  52  {
  53    var $lib_dir = '';
  54    var $os = '';
  55    var $ext = '';
  56    var $cmd_array = array();
  57    var $to_pnm = '';
  58    var $from_pnm = '';
  59    var $tmp_dir = '';
  60    var $current_input_file = '';
  61    var $current_input_file_type = '';
  62    var $current_output_file = '';
  63    var $current_output_file_type = '';
  64  
  65    function lmbImageNetpbm($lib_dir = NETPBM_LIB_DIR)
  66    {
  67      $this->lib_dir = $lib_dir;
  68  
  69      $this->_determineOptions();
  70    }
  71  
  72    function _determineOptions()
  73    {
  74      if(lmbSys :: osType() == "win32")
  75        $this->ext = '.exe';
  76  
  77      $this->_determineReadTypes();
  78      $this->_determineWriteTypes();
  79  
  80      if(sizeof($this->read_types) == 0)
  81        $this->library_installed = false;
  82      else
  83        $this->library_installed = true;
  84    }
  85  
  86    function _determineReadTypes()
  87    {
  88      if(file_exists($this->lib_dir . JPEGTOPNM . $this->ext))
  89        $this->read_types[] = 'JPEG';
  90  
  91      if(file_exists($this->lib_dir . GIFTOPNM . $this->ext))
  92        $this->read_types[] = 'GIF';
  93  
  94      if(file_exists($this->lib_dir . PNGTOPNM . $this->ext))
  95        $this->read_types[] = 'PNG';
  96  
  97      if(file_exists($this->lib_dir . BMPTOPNM . $this->ext))
  98        $this->read_types[] = 'BMP';
  99  
 100      if(file_exists($this->lib_dir . TIFFTOPNM . $this->ext))
 101        $this->read_types[] = 'TIFF';
 102    }
 103  
 104    function _determineWriteTypes()
 105    {
 106      if(file_exists($this->lib_dir . PNMTOJPEG . $this->ext))
 107        $this->create_types[] = 'JPEG';
 108  
 109      if(file_exists($this->lib_dir . PNMTOGIF . $this->ext))
 110        $this->create_types[] = 'GIF';
 111  
 112      if(file_exists($this->lib_dir . PNMTOPNG . $this->ext))
 113        $this->create_types[] = 'PNG';
 114  
 115      if(file_exists($this->lib_dir . PNMTOBMP . $this->ext))
 116        $this->create_types[] = 'BMP';
 117  
 118      if(file_exists($this->lib_dir . PNMTOTIFF . $this->ext))
 119        $this->create_types[] = 'TIFF';
 120    }
 121  
 122    function setInputFile($file_name, $type = '')
 123    {
 124      parent :: setInputFile($file_name, $type);
 125  
 126      $this->to_pnm = constant(strtoupper($type . 'TOPNM')) . " $file_name";
 127    }
 128  
 129    function setOutputFile($file_name, &$type)
 130    {
 131      parent :: setOutputFile($file_name, $type);
 132  
 133      $this->from_pnm = '';
 134  
 135      if(strtoupper($type) == 'GIF')
 136        $this->from_pnm = PNMQUANT . ' 256 | ';
 137  
 138        $this->from_pnm .= constant(strtoupper('PNMTO' . $type)) . " > $file_name";
 139    }
 140  
 141    function reset()
 142    {
 143      $this->cmd_array = array();
 144    }
 145  
 146    function commit()
 147    {
 148      if(!$this->library_installed)
 149        return false;
 150  
 151      array_unshift($this->cmd_array, $this->_toPnm());
 152      array_push($this->cmd_array, $this->from_pnm);
 153  
 154      echo $cmd = implode(' | ', $this->cmd_array);
 155      $this->_runCmd($cmd);
 156  
 157      $this->reset();
 158  
 159      if(is_file($this->current_output_file))
 160        unlink($this->current_output_file);
 161  
 162      $this->current_output_file = '';
 163      $this->current_output_file_type = '';
 164      $this->current_input_file = '';
 165      $this->current_input_file_type = '';
 166  
 167      return true;
 168    }
 169  
 170    function resize($params)
 171    {
 172      if(!$this->library_installed)
 173        return false;
 174  
 175      $info = getimagesize($this->input_file);
 176      $src_width = $info[0];
 177      $src_height = $info[1];
 178  
 179      list($dst_width, $dst_height) = $this->getDstDimensions($src_width, $src_height, $params);
 180  
 181      $this->cmd_array[] = PNMSCALE . " -width={$dst_width} -height={$dst_height}";
 182    }
 183  
 184    function rotate($angle, $bg_color = '')
 185    {
 186      if(!$this->library_installed)
 187        return false;
 188  
 189      $this->cmd_array[] = PNMROTATE . " {$angle}";
 190    }
 191  
 192    function flip($params)
 193    {
 194      if(!$this->library_installed)
 195        return false;
 196  
 197      if($params == FLIP_HORIZONTAL)
 198        $args = '-leftright';
 199  
 200      if($params == FLIP_VERTICAL)
 201        $args = '-topbottom';
 202  
 203      $this->cmd_array[] = PNMFLIP . " {$args}";
 204    }
 205  
 206    function _toPnm()
 207    {
 208      if(!empty($this->current_input_file))
 209      {
 210        $file_name = $this->current_input_file;
 211        $type = $this->current_input_file_type;
 212      }
 213      else
 214      {
 215        $file_name = $this->input_file;
 216        $type = $this->input_file_type;
 217      }
 218  
 219      return constant(strtoupper($type . 'TOPNM')) . " $file_name";
 220    }
 221  
 222    function _fromPnm()
 223    {
 224      if(!empty($this->current_output_file))
 225      {
 226        $file_name = $this->current_output_file;
 227        $type = $this->current_output_file_type;
 228      }
 229      else
 230      {
 231        $file_name = $this->output_file;
 232        $type = $this->output_file_type;
 233      }
 234  
 235      return constant(strtoupper('PNMTO' . $type)) . " > $file_name";
 236    }
 237  
 238    function _runCmd($cmd)
 239    {
 240      $cwd = getcwd();
 241      chdir($this->lib_dir);
 242      shell_exec($cmd);
 243      chdir($cwd);
 244    }
 245  
 246    function cut($x, $y, $w, $h, $bg_color = '')
 247    {
 248      if(!$this->library_installed)
 249        return false;
 250  
 251      $tmp_file = lmbFs :: generateTmpFile('netpbm');
 252      $this->current_output_file = $tmp_file;
 253      $this->current_output_file_type = 'BMP';
 254  
 255      if(sizeof($this->cmd_array) > 0)
 256      {
 257        array_unshift($this->cmd_array, $this->_toPnm());
 258        array_push($this->cmd_array, $this->_fromPnm());
 259  
 260        $cmd = implode(' | ', $this->cmd_array);
 261  
 262        $this->_runCmd($cmd);
 263  
 264        $file_name = $this->current_input_file = $tmp_file;
 265        $type = $this->current_input_file_type = $this->current_output_file_type;
 266  
 267        $this->reset();
 268      }
 269      else
 270      {
 271        $file_name = $this->input_file;
 272        $type = $this->input_file_type;
 273      }
 274  
 275      $info = getimagesize($file_name);
 276  
 277      if($x < 0)
 278      {
 279        $cx = 0;
 280        $cw = $w + $x;
 281      }
 282      else
 283      {
 284        $cx = $x;
 285        $cw = $w;
 286      }
 287  
 288      if($y < 0)
 289      {
 290        $cy = 0;
 291        $ch = $h + $y;
 292      }
 293      else
 294      {
 295        $cy = $y;
 296        $ch = $h;
 297      }
 298  
 299      if($cx + $cw > $info[0])
 300        $cw = $info[0] - $cx;
 301  
 302      if($cy + $ch > $info[1])
 303        $ch = $info[1] - $cy;
 304  
 305      $tmp_file = lmbFs :: generateTmpFile('netpbm');
 306  
 307      $cmd = $this->_toPnm() . ' | ' . PNMCUT . " $cx $cy $cw $ch > {$tmp_file}";
 308  
 309      $this->_runCmd($cmd);
 310  
 311      $cx = ($x < 0) ? -$x : 0;
 312      $cy = ($y < 0) ? -$y : 0;
 313      $cmd = PNMMAKE . " " . $this->_hexColorToX11($bg_color) . " {$w} {$h} | " . PNMCOMP . " {$tmp_file} {$cx} {$cy}" . ' | ' . $this->_fromPnm();
 314  
 315      $this->_runCmd($cmd);
 316  
 317      unlink($tmp_file);
 318  
 319      $this->current_input_file = $this->current_output_file;
 320      $this->current_input_file_type = $this->current_output_file_type;
 321    }
 322  }
 323  ?>


Generated: Tue Oct 14 04:47:40 2008 Cross-referenced by PHPXref 0.7