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

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