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.  *
  13.  * Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
  14.  * All rights reserved.
  15.  *
  16.  * Redistribution and use in source and binary forms, with or without
  17.  * modification, are permitted under the terms of the BSD License.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  29.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * @category    Images
  33.  * @package     Image_Tools
  34.  * @author      Firman Wandayandi <firman@php.net>
  35.  * @copyright   Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
  36.  * @license     http://www.opensource.org/licenses/bsd-license.php
  37.  *               BSD License
  38.  * @version     CVS: $Id: Mask.php,v 1.2 2006/11/23 22:17:31 firman Exp $
  39.  */
  40.  
  41. // }}}
  42. // {{{ Dependencies
  43.  
  44.  
  45.  
  46. /**
  47.  * Load Image_Tools as the base class.
  48.  */
  49. require_once 'Image/Tools.php';
  50.  
  51. /**
  52.  * Require for color mixing. Notes: conflict when integrated with
  53.  * Image_Color v1.0.0 (http://pear.php.net/package/Image_Color).
  54.  */
  55. require_once 'Image/Color.php';
  56.  
  57. // }}}
  58. // {{{ Class: Image_Tools_Mask
  59.  
  60.  
  61.  
  62. /**
  63.  * This class provide masking tool for manipulating an image.
  64.  *
  65.  * @category    Images
  66.  * @package     Image_Tools
  67.  * @author      Firman Wandayandi <firman@php.net>
  68.  * @copyright   Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
  69.  * @license     http://www.opensource.org/licenses/bsd-license.php
  70.  *               BSD License
  71.  * @version     Release: 0.4.1
  72.  */
  73. class Image_Tools_Mask extends Image_Tools
  74. {
  75.     // {{{ Properties
  76.  
  77.     
  78.  
  79.     /**
  80.      * Mask options:
  81.      * <pre>
  82.      * image             mixed  Destination image, a filename or an image string
  83.      *                          data or a GD image resource.
  84.      * mask              mixed  Mask image, a filename or an image string
  85.      *                          data or a GD image resource.
  86.      * sample            mixed  Sample image, a filename or an image string
  87.      *                          data or a GD image resource.
  88.      * mask_color        mixed  Mask color, use string for hexa color format or
  89.      *                          array contains 3 indexes 0 for RGB format
  90.      * unmask_color      mixed  Mask color, use string for hexa color format or
  91.      *                          array contains 3 indexes 0 for RGB format
  92.      * antialias         bool   Flag whether attempt to draw antialias mask
  93.      * antialias_factor  int    Antialias factor, this setting for antialias
  94.      *                          mask
  95.      * </pre>
  96.      *
  97.      * @var     array 
  98.      * @access  protected
  99.      */
  100.     var $options = array(
  101.         'image'             => null,   // Destination image.
  102.         
  103.         'mask'              => null,     // Mask image.
  104.         
  105.         'sample'            => null,     // Sample image.
  106.         
  107.         'mask_color'        => '000000'// Mask color.
  108.         
  109.         'unmask_color'      => 'ffffff'// Unmask color.
  110.         
  111.         'antialias'         => true,     // Antialias flag.
  112.         
  113.         'antialias_factor'  => 16        // Antialias factor.
  114.     
  115.     );
  116.  
  117.     /**
  118.      * Available options for Image_Tools_Mask.
  119.      *
  120.      * @var     array 
  121.      * @access  protected
  122.      */
  123.     var $availableOptions = array(
  124.         'image'             => 'mixed',
  125.         'mask'              => 'mixed',
  126.         'sample'            => 'mixed',
  127.         'mask_color'        => 'mixed',
  128.         'unmask_color'      => 'mixed',
  129.         'antialias'         => 'bool',
  130.         'antialias_factor'  => 'int'
  131.     );
  132.  
  133.     /**
  134.      * There is no public methods in Image_Tool_Mask.
  135.      *
  136.      * @var     array 
  137.      * @access  protected
  138.      */
  139.     var $availableMethods = array();
  140.  
  141.     /**
  142.      * Image_Tools_Mask API version.
  143.      *
  144.      * @var     string 
  145.      * @access  protected
  146.      */
  147.     var $version = '0.1';
  148.  
  149.     /**
  150.      * GD image resource for mask image.
  151.      *
  152.      * @var     resource 
  153.      * @access  private
  154.      */
  155.     var $_maskImage;
  156.     
  157.     /**
  158.      * GD image resource for sample image.
  159.      *
  160.      * @var     resource 
  161.      * @access  private
  162.      */
  163.     var $_sampleImage;
  164.  
  165.     // }}}
  166.     // {{{ _init()
  167.  
  168.     
  169.  
  170.     /**
  171.      * Initialize some internal variables.
  172.      *
  173.      * @return bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  174.      * @access private
  175.      */
  176.     function _init()
  177.     {
  178.         $res Image_Tools::createImage($this->options['mask']);
  179.         if (PEAR::isError($res)) {
  180.             return $res;
  181.         }
  182.         $this->_maskImage $res;
  183.  
  184.         $res Image_Tools::createImage($this->options['sample']);
  185.         if (PEAR::isError($res)) {
  186.             return $res;
  187.         }
  188.         $this->_sampleImage $res;
  189.  
  190.         $res Image_Tools::createImage($this->options['image']);
  191.         if (PEAR::isError($res)) {
  192.             return $res;
  193.         }
  194.         $this->resultImage = $res;
  195.  
  196.         return true;
  197.     }
  198.  
  199.     // }}}
  200.     // {{{ render()
  201.  
  202.     
  203.  
  204.     /**
  205.      * Apply tools to image.
  206.      *
  207.      * This function scan for mask color and closes colors position, grab color
  208.      * at found the position on sample image, then set the pixel color at the same
  209.      * position on destination image.
  210.      *
  211.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  212.      * @access  private
  213.      * @see     Image_Tools_Mask::_getNearestColors()
  214.      */
  215.     function render()
  216.     {
  217.         $res $this->_init();
  218.         if (PEAR::isError($res)) {
  219.             return $res;
  220.         }
  221.  
  222.         if (!Image_Tools::isGDImageResource($this->_maskImage)) {
  223.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_maskImage');
  224.         }
  225.  
  226.         if (!Image_Tools::isGDImageResource($this->_sampleImage)) {
  227.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_sampleImage');
  228.         }
  229.  
  230.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  231.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  232.         }
  233.  
  234.         $maskWidth imagesx($this->_maskImage);
  235.         $maskHeight imagesy($this->_maskImage);
  236.  
  237.         $sampleWidth imagesx($this->_sampleImage);
  238.         $sampleHeight imagesy($this->_sampleImage);
  239.  
  240.         if ($this->options['antialias']{
  241.             $closesColors $this->_getNearestColors();
  242.         else {
  243.             $closesColors = array($this->options['maskColor']);
  244.         }
  245.  
  246.         imagealphablending($this->resultImagetrue);
  247.  
  248.         // scan for mask color or closes colors position
  249.         for ($x = 0; $x $maskWidth$x++{
  250.             for ($y = 0; $y $maskHeight$y++{
  251.                 if ($x >= $sampleWidth || $y >= $sampleHeight{
  252.                     continue;
  253.                 }
  254.  
  255.                 // grab color at x, y and convert to hex color format
  256.                 $index imagecolorat($this->_maskImage$x$y);
  257.                 $maskRGBA imagecolorsforindex($this->_maskImage$index);
  258.                 
  259.                 $maskColor = Image_Color::rgb2hex(array_values($maskRGBA));
  260.  
  261.                 // check color in closes colors collection
  262.                 if (in_array($maskColor$closesColors)) {
  263.                     // grab color at x, y from sample image
  264.                     $index imagecolorat($this->_sampleImage$x$y);
  265.                     $sampleRGBA imagecolorsforindex($this->_sampleImage$index);
  266.  
  267.                     // allocate color on destination image
  268.                     $color imagecolorresolvealpha($this->resultImage,
  269.                                                     $sampleRGBA['red'],
  270.                                                     $sampleRGBA['green'],
  271.                                                     $sampleRGBA['blue'],
  272.                                                     $sampleRGBA['alpha']);
  273.                     
  274.                     // set a pixel color at destination image
  275.                     imagesetpixel($this->resultImage$x$y$color);
  276.                 }
  277.             }
  278.         }
  279.  
  280.         return true;
  281.     }
  282.  
  283.     // }}}
  284.     // {{{ _getNearestColors
  285.  
  286.     
  287.  
  288.     /**
  289.      * Get nearest colors between mask color and unmask color using
  290.      * antialias factor.
  291.      *
  292.      * @return  array Colors range.
  293.      * @access  private
  294.      */
  295.     function _getNearestColors()
  296.     {
  297.         $objColor = new Image_Color;
  298.         $objColor->setColors($this->options['mask_color']$this->options['unmask_color']);
  299.         return $objColor->getRange($this->options['antialias_factor']);
  300.     }
  301.  
  302.     // }}}
  303.  
  304. }
  305.  
  306. // }}}
  307. /*
  308.  * Local variables:
  309.  * mode: php
  310.  * tab-width: 4
  311.  * c-basic-offset: 4
  312.  * c-hanging-comment-ender-p: nil
  313.  * End:
  314.  */
  315. ?>

Documentation generated on Thu, 23 Nov 2006 18:00:07 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.