Image_Tools
[ class tree: Image_Tools ] [ index: Image_Tools ] [ all elements ]

Source for file Mask.php

Documentation is available at Mask.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  3.  
  4. // {{{ Header
  5.  
  6. /**
  7.  * This is a driver file contains the Image_Tools_Mask class.
  8.  *
  9.  * PHP versions 4 and 5
  10.  *
  11.  * LICENSE:
  12.  * Copyright (c) 2005-2008 Firman Wandayandi <firman@php.net>
  13.  *
  14.  * This source file is subject to the BSD License license that is bundled
  15.  * with this package in the file LICENSE.txt.
  16.  * It is also available through the world-wide-web at this URL:
  17.  * http://www.opensource.org/licenses/bsd-license.php
  18.  * If you did not receive a copy of the license and are unable to
  19.  * obtain it through the world-wide-web, please send an email
  20.  * to pear-dev@list.php.net so we can send you a copy immediately.
  21.  *
  22.  * @category    Images
  23.  * @package     Image_Tools
  24.  * @author      Firman Wandayandi <firman@php.net>
  25.  * @copyright   Copyright (c) 2005-2008 Firman Wandayandi <firman@php.net>
  26.  * @license     http://www.opensource.org/licenses/bsd-license.php
  27.  *               BSD License
  28.  * @version     CVS: $Id: Mask.php,v 1.4 2008/05/26 06:32:15 firman Exp $
  29.  */
  30.  
  31. // }}}
  32. // {{{ Dependencies
  33.  
  34. /**
  35.  * Image_Tools
  36.  */
  37. require_once 'Image/Tools.php';
  38.  
  39. /**
  40.  * Image_Color
  41.  * Notes: conflict with Image_Color v1.0.0
  42.  */
  43. require_once 'Image/Color.php';
  44.  
  45. // }}}
  46. // {{{ Class: Image_Tools_Mask
  47.  
  48. /**
  49.  * This class provide masking tool for manipulating an image.
  50.  *
  51.  * @category    Images
  52.  * @package     Image_Tools
  53.  * @author      Firman Wandayandi <firman@php.net>
  54.  * @copyright   Copyright (c) 2005-2008 Firman Wandayandi <firman@php.net>
  55.  * @license     http://www.opensource.org/licenses/bsd-license.php
  56.  *               BSD License
  57.  * @version     Release: 1.0.0RC1
  58.  */
  59. class Image_Tools_Mask extends Image_Tools
  60. {
  61.     // {{{ Properties
  62.  
  63.     /**
  64.      * Mask options:
  65.      * <pre>
  66.      * image             mixed  Destination image, a filename or an image string
  67.      *                          data or a GD image resource.
  68.      * mask              mixed  Mask image, a filename or an image string
  69.      *                          data or a GD image resource.
  70.      * sample            mixed  Sample image, a filename or an image string
  71.      *                          data or a GD image resource.
  72.      * mask_color        mixed  Mask color, use string for hexa color format or
  73.      *                          array contains 3 indexes 0 for RGB format
  74.      * unmask_color      mixed  Mask color, use string for hexa color format or
  75.      *                          array contains 3 indexes 0 for RGB format
  76.      * antialias         bool   Flag whether attempt to draw antialias mask
  77.      * antialias_factor  int    Antialias factor, this setting for antialias
  78.      *                          mask
  79.      * </pre>
  80.      *
  81.      * @var     array 
  82.      * @access  protected
  83.      */
  84.     var $options = array(
  85.         'image'             => null,   // Destination image.
  86.         'mask'              => null,     // Mask image.
  87.         'sample'            => null,     // Sample image.
  88.         'mask_color'        => '000000'// Mask color.
  89.         'unmask_color'      => 'ffffff'// Unmask color.
  90.         'antialias'         => true,     // Antialias flag.
  91.         'antialias_factor'  => 16        // Antialias factor.
  92.     );
  93.  
  94.     /**
  95.      * Available options for Image_Tools_Mask.
  96.      *
  97.      * @var     array 
  98.      * @access  protected
  99.      */
  100.     var $availableOptions = array(
  101.         'image'             => 'mixed',
  102.         'mask'              => 'mixed',
  103.         'sample'            => 'mixed',
  104.         'mask_color'        => 'mixed',
  105.         'unmask_color'      => 'mixed',
  106.         'antialias'         => 'bool',
  107.         'antialias_factor'  => 'int'
  108.     );
  109.  
  110.     /**
  111.      * There is no public methods in Image_Tool_Mask.
  112.      *
  113.      * @var     array 
  114.      * @access  protected
  115.      */
  116.     var $availableMethods = array();
  117.  
  118.     /**
  119.      * Image_Tools_Mask API version.
  120.      *
  121.      * @var     string 
  122.      * @access  protected
  123.      */
  124.     var $version = '1.0';
  125.  
  126.     /**
  127.      * GD image resource for mask image.
  128.      *
  129.      * @var     resource 
  130.      * @access  private
  131.      */
  132.     var $_maskImage;
  133.  
  134.     /**
  135.      * GD image resource for sample image.
  136.      *
  137.      * @var     resource 
  138.      * @access  private
  139.      */
  140.     var $_sampleImage;
  141.  
  142.     // }}}
  143.     // {{{ preRender()
  144.  
  145.     /**
  146.      * Function which called before render.
  147.      *
  148.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  149.      * @access  protected
  150.      * @see     Image_Tools::createImage()
  151.      */
  152.     function preRender()
  153.     {
  154.         $res Image_Tools::createImage($this->options['mask']);
  155.         if (PEAR::isError($res)) {
  156.             return $res;
  157.         }
  158.         $this->_maskImage $res;
  159.  
  160.         $res Image_Tools::createImage($this->options['sample']);
  161.         if (PEAR::isError($res)) {
  162.             return $res;
  163.         }
  164.         $this->_sampleImage $res;
  165.  
  166.         $res Image_Tools::createImage($this->options['image']);
  167.         if (PEAR::isError($res)) {
  168.             return $res;
  169.         }
  170.         $this->resultImage = $res;
  171.  
  172.         return true;
  173.     }
  174.  
  175.     // }}}
  176.     // {{{ render()
  177.  
  178.     /**
  179.      * Apply tools to image.
  180.      *
  181.      * This function scan for mask color and closes colors position, grab color
  182.      * at found the position on sample image, then set the pixel color at the same
  183.      * position on destination image.
  184.      *
  185.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  186.      * @access  private
  187.      * @see     Image_Tools_Mask::_getNearestColors()
  188.      */
  189.     function render()
  190.     {
  191.         if (!Image_Tools::isGDImageResource($this->_maskImage)) {
  192.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_maskImage');
  193.         }
  194.  
  195.         if (!Image_Tools::isGDImageResource($this->_sampleImage)) {
  196.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_sampleImage');
  197.         }
  198.  
  199.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  200.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  201.         }
  202.  
  203.         $maskWidth imagesx($this->_maskImage);
  204.         $maskHeight imagesy($this->_maskImage);
  205.  
  206.         $sampleWidth imagesx($this->_sampleImage);
  207.         $sampleHeight imagesy($this->_sampleImage);
  208.  
  209.         if ($this->options['antialias']{
  210.             $closesColors $this->_getNearestColors();
  211.         else {
  212.             $closesColors = array($this->options['maskColor']);
  213.         }
  214.  
  215.         imagealphablending($this->resultImagetrue);
  216.  
  217.         // scan for mask color or closes colors position
  218.         for ($x = 0; $x $maskWidth$x++{
  219.             for ($y = 0; $y $maskHeight$y++{
  220.                 if ($x >= $sampleWidth || $y >= $sampleHeight{
  221.                     continue;
  222.                 }
  223.  
  224.                 // grab color at x, y and convert to hex color format
  225.                 $index imagecolorat($this->_maskImage$x$y);
  226.                 $maskRGBA imagecolorsforindex($this->_maskImage$index);
  227.  
  228.                 $maskColor = Image_Color::rgb2hex(array_values($maskRGBA));
  229.  
  230.                 // check color in closes colors collection
  231.                 if (in_array($maskColor$closesColors)) {
  232.                     // grab color at x, y from sample image
  233.                     $index imagecolorat($this->_sampleImage$x$y);
  234.                     $sampleRGBA imagecolorsforindex($this->_sampleImage$index);
  235.  
  236.                     // allocate color on destination image
  237.                     $color imagecolorresolvealpha($this->resultImage,
  238.                                                     $sampleRGBA['red'],
  239.                                                     $sampleRGBA['green'],
  240.                                                     $sampleRGBA['blue'],
  241.                                                     $sampleRGBA['alpha']);
  242.  
  243.                     // set a pixel color at destination image
  244.                     imagesetpixel($this->resultImage$x$y$color);
  245.                 }
  246.             }
  247.         }
  248.  
  249.         return true;
  250.     }
  251.  
  252.     // }}}
  253.     // {{{ _getNearestColors
  254.  
  255.     /**
  256.      * Get nearest colors between mask color and unmask color using
  257.      * antialias factor.
  258.      *
  259.      * @return  array Colors range.
  260.      * @access  private
  261.      */
  262.     function _getNearestColors()
  263.     {
  264.         $imcolor = new Image_Color;
  265.         $imcolor->setColors($this->options['mask_color']$this->options['unmask_color']);
  266.         return $imcolor->getRange($this->options['antialias_factor']);
  267.     }
  268.  
  269.     // }}}
  270. }
  271.  
  272. // }}}
  273.  
  274. /*
  275.  * Local variables:
  276.  * mode: php
  277.  * tab-width: 4
  278.  * c-basic-offset: 4
  279.  * c-hanging-comment-ender-p: nil
  280.  * End:
  281.  */
  282. ?>

Documentation generated on Mon, 26 May 2008 06:30:11 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.