[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/kcfinder/lib/ -> class_gd.php (source)

   1  <?php
   2  
   3  /** This file is part of KCFinder project
   4    *
   5    *      @desc GD extension class
   6    *   @package KCFinder
   7    *   @version 2.21
   8    *    @author Pavel Tzonkov <[email protected]>
   9    * @copyright 2010 KCFinder Project
  10    *   @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
  11    *   @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
  12    *      @link http://kcfinder.sunhater.com
  13    */
  14  
  15  class gd {
  16  
  17    /** GD resource
  18      * @var resource */
  19      protected $image;
  20  
  21    /** Image width
  22      * @var integer */
  23      protected $width;
  24  
  25    /** Image height
  26      * @var integer */
  27      protected $height;
  28  
  29    /** Init error
  30      * @var bool */
  31      public $init_error = false;
  32  
  33    /** Last builded image type constant (IMAGETYPE_XXX)
  34      * @var integer */
  35      public $type;
  36  
  37    /** Returns an array. Element 0 - GD resource. Element 1 - width. Element 2 - height.
  38      * Returns FALSE on failure. The only one parameter $image can be an instance of this class,
  39      * a GD resource, an array(width, height) or path to image file.
  40      * @param mixed $image
  41      * @return array */
  42  
  43      protected function build_image($image) {
  44  
  45          if ($image instanceof gd) {
  46              $width = $image->get_width();
  47              $height = $image->get_height();
  48              $image = $image->get_image();
  49  
  50          } elseif (is_resource($image) && (get_resource_type($image) == "gd")) {
  51              $width = @imagesx($image);
  52              $height = @imagesy($image);
  53  
  54          } elseif (is_array($image)) {
  55              list($key, $width) = each($image);
  56              list($key, $height) = each($image);
  57              $image = imagecreatetruecolor($width, $height);
  58  
  59          } elseif (false !== (list($width, $height, $type) = @getimagesize($image))) {
  60              $image =
  61                  ($type == IMAGETYPE_GIF)      ? @imagecreatefromgif($image)  : (
  62                  ($type == IMAGETYPE_WBMP)     ? @imagecreatefromwbmp($image) : (
  63                  ($type == IMAGETYPE_JPEG)     ? @imagecreatefromjpeg($image) : (
  64                  ($type == IMAGETYPE_JPEG2000) ? @imagecreatefromjpeg($image) : (
  65                  ($type == IMAGETYPE_PNG)      ? imagecreatefrompng($image)  : (
  66                  ($type == IMAGETYPE_XBM)      ? @imagecreatefromxbm($image)  : false
  67              )))));
  68  
  69              if ($type == IMAGETYPE_PNG)
  70                  imagealphablending($image, false);
  71          }
  72  
  73          $return = (
  74              is_resource($image) &&
  75              (get_resource_type($image) == "gd") &&
  76              isset($width) &&
  77              isset($height) &&
  78              (preg_match('/^[1-9][0-9]*$/', $width) !== false) &&
  79              (preg_match('/^[1-9][0-9]*$/', $height) !== false)
  80          )
  81              ? array($image, $width, $height)
  82              : false;
  83  
  84          if (($return !== false) && isset($type))
  85              $this->type = $type;
  86  
  87          return $return;
  88      }
  89  
  90    /** Parameter $image can be:
  91      *   1. An instance of this class (copy instance).
  92      *   2. A GD resource.
  93      *   3. An array with two elements. First - width, second - height. Create a blank image.
  94      *   4. A filename string. Get image form file.
  95      * The non-required parameter $bigger_size is the bigger dimension (width or height) the image
  96      * will be resized to. The other dimension (height or width) will be calculated autamaticaly
  97      * @param mixed $image
  98      * @param integer $bigger_size
  99      * @return gd */
 100  
 101      public function __construct($image, $bigger_size=null) {
 102          $this->image = $this->width = $this->height = null;
 103  
 104          $image_details = $this->build_image($image);
 105  
 106          if ($image_details !== false)
 107              list($this->image, $this->width, $this->height) = $image_details;
 108          else
 109              $this->init_error = true;
 110  
 111          if (!is_null($this->image) &&
 112              !is_null($bigger_size) &&
 113              (preg_match('/^[1-9][0-9]*$/', $bigger_size) !== false)
 114          ) {
 115              $image = $this->image;
 116              list($width, $height) = $this->get_prop_size($bigger_size);
 117              $this->image = imagecreatetruecolor($width, $height);
 118              if ($this->type == IMAGETYPE_PNG) {
 119                  imagealphablending($this->image, false);
 120                  imagesavealpha($this->image, true);
 121              }
 122              $this->width = $width;
 123              $this->height = $height;
 124              $this->imagecopyresampled($image);
 125          }
 126      }
 127  
 128    /** Returns the GD resource
 129      * @return resource */
 130  
 131      public function get_image() {
 132          return $this->image;
 133      }
 134  
 135    /** Returns the image width
 136      * @return integer */
 137  
 138      public function get_width() {
 139          return $this->width;
 140      }
 141  
 142    /** Returns the image height
 143      * @return integer */
 144  
 145      public function get_height() {
 146          return $this->height;
 147      }
 148  
 149    /** Returns calculated proportional width from the given height
 150      * @param integer $resized_height
 151      * @return integer */
 152  
 153      public function get_prop_width($resized_height) {
 154          return intval(($this->width * $resized_height) / $this->height);
 155      }
 156  
 157    /** Returns calculated proportional height from the given width
 158      * @param integer $resized_width
 159      * @return integer */
 160  
 161      public function get_prop_height($resized_width) {
 162          return intval(($this->height * $resized_width) / $this->width);
 163      }
 164  
 165    /** Returns an array with calculated proportional width & height.
 166      * The parameter $bigger_size is the bigger dimension (width or height) of calculated sizes.
 167      * The other dimension (height or width) will be calculated autamaticaly
 168      * @param integer $bigger_size
 169      * @return array */
 170  
 171      public function get_prop_size($bigger_size) {
 172  
 173          if ($this->width > $this->height) {
 174              $width = $bigger_size;
 175              $height = $this->get_prop_height($width);
 176  
 177          } elseif ($this->height > $this->width) {
 178              $height = $bigger_size;
 179              $width = $this->get_prop_width($height);
 180  
 181          } else
 182              $width = $height = $bigger_size;
 183  
 184          return array($width, $height);
 185      }
 186  
 187    /** Resize image. Returns TRUE on success or FALSE on failure
 188      * @param integer $width
 189      * @param integer $height
 190      * @return bool */
 191  
 192      public function resize($width, $height) {
 193          if (!$width) $width = 1;
 194          if (!$height) $height = 1;
 195          return (
 196              (false !== ($img = new gd(array($width, $height)))) &&
 197              $img->imagecopyresampled($this) &&
 198              (false !== ($this->image = $img->get_image())) &&
 199              (false !== ($this->width = $img->get_width())) &&
 200              (false !== ($this->height = $img->get_height()))
 201          );
 202      }
 203  
 204    /** Resize the given image source (GD, gd object or image file path) to fit in the own image.
 205      * The outside ares will be cropped out. Returns TRUE on success or FALSE on failure
 206      * @param mixed $src
 207      * @return bool */
 208  
 209      public function resize_crop($src) {
 210          $image_details = $this->build_image($src);
 211  
 212          if ($image_details !== false) {
 213              list($src, $src_width, $src_height) = $image_details;
 214  
 215              if (($src_width / $src_height) > ($this->width / $this->height)) {
 216                  $src_w = $this->get_prop_width($src_height);
 217                  $src_h = $src_height;
 218                  $src_x = intval(($src_width - $src_w) / 2);
 219                  $src_y = 0;
 220  
 221              } else {
 222                  $src_w = $src_width;
 223                  $src_h = $this->get_prop_height($src_width);
 224                  $src_x = 0;
 225                  $src_y = intval(($src_height - $src_h) / 2);
 226              }
 227  
 228              return imagecopyresampled($this->image, $src, 0, 0, $src_x, $src_y, $this->width, $this->height, $src_w, $src_h);
 229  
 230          } else
 231              return false;
 232      }
 233  
 234    /** Resize image to fit in given resolution. Returns TRUE on success or FALSE on failure
 235      * @param integer $width
 236      * @param integer $height
 237      * @return bool */
 238  
 239      public function resize_fit($width, $height) {
 240          if ((!$width && !$height) || (($width == $this->width) && ($height == $this->height)))
 241              return true;
 242          if (!$width || (($width / $height) > ($this->width / $this->height)))
 243              $width = intval(($this->width * $height) / $this->height);
 244          elseif (!$height || (($width / $height) < ($this->width / $this->height)))
 245              $height = intval(($this->height * $width) / $this->width);
 246          return $this->resize($width, $height);
 247      }
 248  
 249    /** Neka si predstavim vyobrazhaem pravoygylnik s razmeri $width i $height.
 250      * Izobrazhenieto shte se preorazmeri taka che to shte izliza ot tozi pravoygylnik,
 251      * no samo po edno (x ili y) izmerenie
 252      * @param integer $width
 253      * @param integer $height
 254      * @return bool */
 255  
 256      public function resize_overflow($width, $height) {
 257  
 258          $big = (($this->width / $this->height) > ($width / $height))
 259              ? ($this->width * $height) / $this->height
 260              : ($this->height * $width) / $this->width;
 261          $big = intval($big);
 262  
 263          $return = ($img = new gd($this->image, $big));
 264  
 265          if ($return) {
 266              $this->image = $img->get_image();
 267              $this->width = $img->get_width();
 268              $this->height = $img->get_height();
 269          }
 270  
 271          return $return;
 272      }
 273  
 274      public function gd_color() {
 275          $args = func_get_args();
 276  
 277          $expr_rgb = '/^rgb\(\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\,\s*(\d{1,3})\s*\)$/i';
 278          $expr_hex1 = '/^\#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i';
 279          $expr_hex2 = '/^\#?([0-9a-f])([0-9a-f])([0-9a-f])$/i';
 280          $expr_byte = '/^([01]?\d?\d|2[0-4]\d|25[0-5])$/';
 281  
 282          if (!isset($args[0]))
 283              return false;
 284  
 285          if (count($args[0]) == 3) {
 286              list($r, $g, $b) = $args[0];
 287  
 288          } elseif (preg_match($expr_rgb, $args[0])) {
 289              list($r, $g, $b) = explode(" ", preg_replace($expr_rgb, "$1 $2 $3", $args[0]));
 290  
 291          } elseif (preg_match($expr_hex1, $args[0])) {
 292              list($r, $g, $b) = explode(" ", preg_replace($expr_hex1, "$1 $2 $3", $args[0]));
 293              $r = hexdec($r);
 294              $g = hexdec($g);
 295              $b = hexdec($b);
 296  
 297          } elseif (preg_match($expr_hex2, $args[0])) {
 298              list($r, $g, $b) = explode(" ", preg_replace($expr_hex2, "$1$1 $2$2 $3$3", $args[0]));
 299              $r = hexdec($r);
 300              $g = hexdec($g);
 301              $b = hexdec($b);
 302  
 303          } elseif ((count($args) == 3) &&
 304              preg_match($expr_byte, $args[0]) &&
 305              preg_match($expr_byte, $args[1]) &&
 306              preg_match($expr_byte, $args[2])
 307          ) {
 308              list($r, $g, $b) = $args;
 309  
 310          } else
 311              return false;
 312  
 313          return imagecolorallocate($this->image, $r, $g, $b);
 314      }
 315  
 316      public function fill_color($color) {
 317          return $this->imagefilledrectangle(0, 0, $this->width - 1, $this->height - 1, $color);
 318      }
 319  
 320  
 321  /*   G D   M E T H O D S   */
 322  
 323      public function imagecopy(
 324          $src,
 325          $dst_x=0, $dst_y=0,
 326          $src_x=0, $src_y=0,
 327          $dst_w=null, $dst_h=null,
 328          $src_w=null, $src_h=null
 329      ) {
 330          $image_details = $this->build_image($src);
 331  
 332          if ($image_details !== false) {
 333              list($src, $src_width, $src_height) = $image_details;
 334  
 335              if (is_null($dst_w)) $dst_w = $this->width - $dst_x;
 336              if (is_null($dst_h)) $dst_h = $this->height - $dst_y;
 337              if (is_null($src_w)) $src_w = $src_width - $src_x;
 338              if (is_null($src_h)) $src_h = $src_height - $src_y;
 339              return imagecopy($this->image, $src, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
 340  
 341          } else
 342              return false;
 343      }
 344  
 345      public function imagecopyresampled(
 346          $src,
 347          $dst_x=0, $dst_y=0,
 348          $src_x=0, $src_y=0,
 349          $dst_w=null, $dst_h=null,
 350          $src_w=null, $src_h=null
 351      ) {
 352          $image_details = $this->build_image($src);
 353  
 354          if ($image_details !== false) {
 355              list($src, $src_width, $src_height) = $image_details;
 356  
 357              if (is_null($dst_w)) $dst_w = $this->width - $dst_x;
 358              if (is_null($dst_h)) $dst_h = $this->height - $dst_y;
 359              if (is_null($src_w)) $src_w = $src_width - $src_x;
 360              if (is_null($src_h)) $src_h = $src_height - $src_y;
 361              return imagecopyresampled($this->image, $src, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
 362  
 363          } else
 364              return false;
 365      }
 366  
 367      public function imagefilledrectangle($x1, $y1, $x2, $y2, $color) {
 368          $color = $this->gd_color($color);
 369          if ($color === false) return false;
 370          return imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $color);
 371      }
 372  
 373      public function imagepng($filename=null, $quality=null, $filters=null) {
 374          if (is_null($filename) && !headers_sent())
 375              header("Content-Type: image/png");
 376          @imagesavealpha($this->image, true);
 377          return imagepng($this->image, $filename, $quality, $filters);
 378      }
 379  
 380      public function imagejpeg($filename=null, $quality=75) {
 381          if (is_null($filename) && !headers_sent())
 382              header("Content-Type: image/jpeg");
 383          return imagejpeg($this->image, $filename, $quality);
 384      }
 385  
 386      public function imagegif($filename=null) {
 387          if (is_null($filename) && !headers_sent())
 388              header("Content-Type: image/gif");
 389          return imagegif($this->image, $filename);
 390      }
 391  }
 392  
 393  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1