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

Source for file Swap.php

Documentation is available at Swap.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_Swap 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: Swap.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.  * Load PHP_Compat for PHP5 backward compalitiby function (str_split())
  53.  */
  54. require_once 'PHP/Compat.php';
  55.  
  56. // load str_split() function
  57. @PHP_Compat::loadFunction('str_split');
  58.  
  59. // }}}
  60. // {{{ Class: Image_Tools_Swap
  61.  
  62.  
  63.  
  64. /**
  65.  * This class provide masking tool for manipulating an image.
  66.  *
  67.  * @category    Images
  68.  * @package     Image_Tools
  69.  * @author      Firman Wandayandi <firman@php.net>
  70.  * @copyright   Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
  71.  * @license     http://www.opensource.org/licenses/bsd-license.php
  72.  *               BSD License
  73.  * @version     Release: 0.4.1
  74.  */
  75. class Image_Tools_Swap extends Image_Tools
  76. {
  77.     // {{{ Properties
  78.  
  79.     
  80.  
  81.     /**
  82.      * Swap options:
  83.      * <pre>
  84.      * image   mixed   Destination image, a filename or an image string
  85.      *                 data or a GD image resource.
  86.      * format  string  Destination color format (mix 'R', 'G', 'B'),
  87.      *                 only 5 variations (RBG, BGR, BRG, GRB, GBR)
  88.      * </pre>
  89.      *
  90.      * @var     array 
  91.      * @access  protected
  92.      */
  93.     var $options = array(
  94.         'image'     => null,
  95.         'format'    => 'RBG'
  96.     );
  97.  
  98.     /**
  99.      * Available options for Image_Tools_Swap.
  100.      *
  101.      * @var array 
  102.      * @access protected
  103.      */
  104.     var $availableOptions = array(
  105.         'image'     => 'mixed',
  106.         'format'    => 'string'
  107.     );
  108.  
  109.     /**
  110.      * Available methods for Image_Tool_Swap (only public methods).
  111.      *
  112.      * @var     array 
  113.      * @access  protected
  114.      */
  115.     var $availableMethods = array(
  116.         'swapColor' => array(
  117.             'format' => 'string',
  118.             'rgb'    => 'array'
  119.         )
  120.     );
  121.  
  122.     /**
  123.      * Image_Tools_Swap API version.
  124.      *
  125.      * @var     string 
  126.      * @access  protected
  127.      */
  128.     var $version = '0.1';
  129.  
  130.     // }}}
  131.     // {{{ _init()
  132.  
  133.     
  134.  
  135.     /**
  136.      * Initialize some internal variables.
  137.      *
  138.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  139.      * @access  private
  140.      * @see     Image_Tools::createImage()
  141.      */
  142.     function _init()
  143.     {
  144.         $res Image_Tools::createImage($this->options['image']);
  145.         if (PEAR::isError($res)) {
  146.             return $res;
  147.         }
  148.         $this->resultImage = $res;
  149.  
  150.         return true;
  151.     }
  152.  
  153.     // }}}
  154.     // {{{ render()
  155.  
  156.     
  157.  
  158.     /**
  159.      * Apply swap color to image and output result.
  160.      *
  161.      * This function swap channel color 'R', 'G', 'B' to set format.
  162.      *
  163.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  164.      * @access  protected
  165.      */
  166.     function render()
  167.     {
  168.         $res $this->_init();
  169.         if (PEAR::isError($res)) {
  170.             return $res;
  171.         }
  172.  
  173.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  174.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  175.         }
  176.  
  177.         if (!preg_match('@RBG|BGR|BRG|GRB|GBR$@i'$this->options['format'])) {
  178.             return PEAR::raiseError('Invalid swap format');
  179.         }
  180.  
  181.         $width imagesx($this->resultImage);
  182.         $height imagesy($this->resultImage);
  183.         $destImg imagecreatetruecolor($width$height);
  184.  
  185.         for ($x = 0; $x $width$x++{
  186.             for ($y = 0; $y $height$y++{
  187.                 $index imagecolorat($this->resultImage$x$y);
  188.                 $rgb imagecolorsforindex($this->resultImage$index);
  189.                 $rgb Image_Tools_Swap::swapColor($this->options['format']$rgb);
  190.                 $color imagecolorallocate($destImg,
  191.                                             $rgb['r'],
  192.                                             $rgb['g'],
  193.                                             $rgb['b']);
  194.                 imagesetpixel($destImg$x$y$color);
  195.             }
  196.         }
  197.  
  198.         $this->resultImage = $destImg;
  199.         return true;
  200.     }
  201.  
  202.     // }}}
  203.     // {{{ swapColor()
  204.  
  205.     
  206.  
  207.     /**
  208.      * Swap RGB color channel.
  209.      *
  210.      * This function swap color channel array to given format,
  211.      * e.g RGB->RBG mean red->red, green->blue and blue->green.
  212.      *
  213.      * @param   string $format Swap format, mix 'R', 'G' and 'B' to other string.
  214.      * @param   array $rgb RGB color, an array contains keys
  215.      *                      - red, green and blue, or
  216.      *                      - r, g and b, or
  217.      *                      - 0, 1 and 2
  218.      *
  219.      * @return  array|PEAR_ErrorSwapped color channel, an array contains
  220.      *                            keys r, g and b on success or PEAR_Error
  221.      *                            on failure.
  222.      * @access  public
  223.      */
  224.     function swapColor($format$rgb)
  225.     {
  226.         if (!is_array($rgb)) {
  227.             return PEAR::raiseError('Type mismatch for argument 2');
  228.         }
  229.  
  230.         $format = str_split(strtolower($format));
  231.  
  232.         if (isset($rgb['red']&& isset($rgb['green']&& isset($rgb['blue'])) {
  233.             $color['r'$rgb['red'];
  234.             $color['g'$rgb['green'];
  235.             $color['b'$rgb['blue'];
  236.         elseif (isset($rgb['r']&& isset($rgb['g']&& isset($rgb['b'])) {
  237.             $color $rgb;
  238.         elseif (isset($rgb[0]&& isset($rgb[1]&& isset($rgb[2])) {
  239.             $color['r'$rgb[0];
  240.             $color['g'$rgb[1];
  241.             $color['b'$rgb[2];
  242.         else {
  243.             return PEAR::raiseError('Invalid RGB color');
  244.         }
  245.  
  246.         return array(
  247.             $format[0=> $color['r'],
  248.             $format[1=> $color['g'],
  249.             $format[2=> $color['b']
  250.         );
  251.     }
  252.  
  253.     // }}}
  254.  
  255. }
  256.  
  257. // }}}
  258. /*
  259.  * Local variables:
  260.  * mode: php
  261.  * tab-width: 4
  262.  * c-basic-offset: 4
  263.  * c-hanging-comment-ender-p: nil
  264.  * End:
  265.  */
  266. ?>

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