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

Source for file Marquee.php

Documentation is available at Marquee.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_Marquee 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: Marquee.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. // {{{ Constants
  53.  
  54.  
  55.  
  56. /**
  57.  * Image_Tools_Marquee rectangle marquee.
  58.  *
  59.  * @name IMAGE_TOOLS_MARQUEE_TYPE_RECTANGLE
  60.  * @access public
  61.  */
  62. define('IMAGE_TOOLS_MARQUEE_TYPE_RECTANGLE''marquee_rectangle');
  63.  
  64. /**
  65.  * Image_Tools_Marquee single column marquee.
  66.  *
  67.  * @name IMAGE_TOOLS_MARQUEE_TYPE_SINGLECOL
  68.  * @access public
  69.  */
  70. define('IMAGE_TOOLS_MARQUEE_TYPE_SINGLECOL''marquee_singlecol');
  71.  
  72. /**
  73.  * Image_Tools_Marquee single row marquee.
  74.  *
  75.  * @name IMAGE_TOOLS_MARQUEE_TYPE_SINGLEROW
  76.  * @access public
  77.  */
  78. define('IMAGE_TOOLS_MARQUEE_TYPE_SINGLEROW''marquee_singlerow');
  79.  
  80. /**
  81.  * Image_Tools_Marquee polygon marquee.
  82.  * Notes: this constant is useless at this time.
  83.  *
  84.  * @name IMAGE_TOOLS_MARQUEE_TYPE_POLYGON
  85.  * @access public
  86.  */
  87. define('IMAGE_TOOLS_MARQUEE_TYPE_POLYGON''marquee_polygon');
  88.  
  89. // }}}
  90. // {{{ Class: Image_Tools_Marquee
  91.  
  92.  
  93.  
  94. /**
  95.  * This class provide marquee extraction tool for manipulating an image.
  96.  *
  97.  * @category    Images
  98.  * @package     Image_Tools
  99.  * @author      Firman Wandayandi <firman@php.net>
  100.  * @copyright   Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
  101.  * @license     http://www.opensource.org/licenses/bsd-license.php
  102.  *               BSD License
  103.  * @version     Release: 0.4.1
  104.  */
  105. {
  106.     // {{{ Properties
  107.  
  108.     
  109.  
  110.     /**
  111.      * Marquee options:
  112.      * <pre>
  113.      * sample  mixed  Sample image, a GD image resource or string filename.
  114.      * x       int    X position, this sets X position where marquee will be
  115.      *                placed.
  116.      * y       int    Y position, this sets Y position where marquee will be
  117.      *                placed.
  118.      * </pre>
  119.      *
  120.      * @var     array 
  121.      * @access  protected
  122.      */
  123.     var $options = array(
  124.         'image'     => null,   // Destination image.
  125.         
  126.         'sample'    => null,   // Sample image.
  127.         
  128.         'x'         => 0,      // X position.
  129.         
  130.         'y'         => 0       // Y position.
  131.     
  132.     );
  133.  
  134.     /**
  135.      * Available options for Image_Tools_Marquee.
  136.      *
  137.      * @var     array 
  138.      * @access  protected
  139.      */
  140.     var $availableOptions = array(
  141.         'image'     => 'mixed',
  142.         'sample'    => 'mixed',
  143.         'x'         => 'int',
  144.         'y'         => 'int'
  145.     );
  146.  
  147.     /**
  148.      * Available methods for Image_Tool_Marquee (only public methods).
  149.      *
  150.      * @var     array 
  151.      * @access  protected
  152.      */
  153.     var $availableMethods = array(
  154.         'setRectangleMarquee' => array(
  155.             'topLeftX'     => 'int',
  156.             'topLeftY'     => 'int',
  157.             'bottomRightX' => 'int',
  158.             'bottomRightY' => 'int'
  159.         ),
  160.         'setSingleColMarquee' => array(
  161.             'x' => 'int'
  162.         ),
  163.         'setSingleRowMarquee' => array(
  164.             'y' => 'int'
  165.         )
  166.     );
  167.  
  168.     /**
  169.      * Image_Tools_Marquee API version.
  170.      *
  171.      * @var     string 
  172.      * @access  protected
  173.      */
  174.     var $version = '0.1';
  175.  
  176.     /**
  177.      * Marquee information.
  178.      *
  179.      * @var     array 
  180.      * @access  private
  181.      */
  182.     var $_marquee = array();
  183.  
  184.     /**
  185.      * Sample image.
  186.      *
  187.      * @var     resource 
  188.      * @access  private
  189.      */
  190.     var $_sample = null;
  191.  
  192.     // }}}
  193.     // {{{ _init()
  194.  
  195.     
  196.  
  197.     /**
  198.      * Initialize some internal variables.
  199.      *
  200.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  201.      * @access  private
  202.      * @see     Image_Tools::createImage()
  203.      */
  204.     function _init()
  205.     {
  206.         $res Image_Tools::createImage($this->options['sample']);
  207.         if (PEAR::isError($res)) {
  208.             return $res;
  209.         }
  210.         $this->_sample $res;
  211.  
  212.         $res Image_Tools::createImage($this->options['image']);
  213.         if (PEAR::isError($res)) {
  214.             return $res;
  215.         }
  216.         $this->resultImage = $res;
  217.  
  218.         return true;
  219.     }
  220.  
  221.     // }}}
  222.     // {{{ setRectangleMarquee()
  223.  
  224.     
  225.  
  226.     /**
  227.      * Set marquee using rectangle.
  228.      *
  229.      * @param   int $topLeftX Top left x coordinate.
  230.      * @param   int $topLeftY Top left y coordinate.
  231.      * @param   int $bottomRightX Bottom right x coordinate.
  232.      * @param   int $bottomRightY Bottom right y coordinate.
  233.      *
  234.      * @return  bool Always TRUE.
  235.      * @access  public
  236.      * @see     _setMarquee()
  237.      */
  238.     function setRectangleMarquee($topLeftX$topLeftY,
  239.                                  $bottomRightX$bottomRightY)
  240.     {
  241.         if ($topLeftX $bottomRightX{
  242.             $tmp $topLeftX;
  243.             $topLeftX $bottomRightX;
  244.             $bottomRightX $tmp;
  245.         }
  246.  
  247.         if ($topLeftY $bottomRightY{
  248.             $tmp $bottomRightY;
  249.             $topLeftY $bottomRightY;
  250.             $bottomRightY $tmp;
  251.         }
  252.  
  253.         $points = array($topLeftX$topLeftY$bottomRightX$bottomRightY);
  254.         $this->_setMarquee(IMAGE_TOOLS_MARQUEE_TYPE_RECTANGLE$points);
  255.         return true;
  256.     }
  257.  
  258.     // }}}
  259.     // {{{ setSingleColumnMarquee()
  260.  
  261.     
  262.  
  263.     /**
  264.      * Set marquee using single column marquee.
  265.      *
  266.      * @param   int $x X coordinate.
  267.      *
  268.      * @return  bool Always TRUE.
  269.      * @access  public
  270.      * @see     _setMarquee()
  271.      */
  272.     function setSingleColumnMarquee($x)
  273.     {
  274.         $this->_setMarquee(IMAGE_EXTRACT_MARQUEE_TYPE_SINGLECOL$x);
  275.         return true;
  276.     }
  277.  
  278.     // }}}
  279.     // {{{ setSingleRowMarquee()
  280.     
  281.     
  282.     
  283.     /**
  284.      * Set marquee using single row marquee.
  285.      *
  286.      * @param int $y Y coordinate.
  287.      *
  288.      * @return bool Always TRUE.
  289.      * @access public
  290.      * @see Image_Tools_Marquee::_setMarquee()
  291.      */
  292.     function setSingleRowMarquee($y)
  293.     {
  294.         $this->_setMarquee(IMAGE_EXTRACT_MARQUEE_TYPE_SINGLEROW$y);
  295.         return true;
  296.     }
  297.  
  298.     // }}}
  299.     // {{{ render()
  300.  
  301.     
  302.  
  303.     /**
  304.      * Draw extraction result to resource.
  305.      *
  306.      * @return bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  307.      * @access public
  308.      * @see Image_Tools_Marquee::_extractRectangle(),
  309.      *       Image_Tools_Marquee::_extractSingleCol(),
  310.      *       Image_Tools_Marquee::_extractSingleRow()
  311.      */
  312.     function render()
  313.     {
  314.         $res $this->_init();
  315.         if (PEAR::isError($res)) {
  316.             return $res;
  317.         }
  318.  
  319.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  320.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  321.         }
  322.  
  323.         $x $this->options['x'];
  324.         $y $this->options['y'];
  325.  
  326.         switch ($this->_marquee['type']{
  327.             case IMAGE_TOOLS_MARQUEE_TYPE_RECTANGLE:
  328.                 $this->_extractRectangle($this->resultImage$x$y);
  329.                 break;
  330.             case IMAGE_TOOLS_MARQUEE_TYPE_SINGLECOL:
  331.                 $this->_extractSingleColumn($this->resultImage$x$y);
  332.                 break;
  333.             case IMAGE_TOOLS_MARQUEE_TYPE_SINGLEROW:
  334.                 $this->_extractSingleRow($this->resultImage$x$y);
  335.                 break;
  336.         }
  337.  
  338.         return true;
  339.     }
  340.  
  341.     // }}}
  342.     // {{{ _setMarquee()
  343.  
  344.     
  345.  
  346.     /**
  347.      * Set marquee informations.
  348.      *
  349.      * @param string $type Marquee type. Use marquee type defined constants.
  350.      * @param mixed $points Marquee coordinate points.
  351.      *
  352.      * @access private
  353.      */
  354.     function _setMarquee($type$points)
  355.     {
  356.         $this->_marquee = array(
  357.                             'type' => $type,
  358.                             'points' => $points
  359.                           );
  360.     }
  361.  
  362.     // }}}
  363.     // {{{ _extractRectangle()
  364.  
  365.     
  366.  
  367.     /**
  368.      * Extract sample image using rectangle marquee and
  369.      * draw it on destination image.
  370.      *
  371.      * @param resource $img GD image resource.
  372.      * @param int $dstX Top left x coordinate.
  373.      * @param int $dstY Top left y coordinate.
  374.      *
  375.      * @access private
  376.      */
  377.     function _extractRectangle(&$img$dstX$dstY)
  378.     {
  379.         $srcX $this->_marquee['points'][0];
  380.         $srcY $this->_marquee['points'][1];
  381.         $srcW $this->_marquee['points'][2$srcX;
  382.         $srcH $this->_marquee['points'][3$srcY;
  383.         imagecopymerge($img$this->_sample,
  384.                        $dstX$dstY$srcX$srcY,
  385.                        $srcW$srcH100);
  386.     }
  387.  
  388.     // }}}
  389.     // {{{ _extractSingleColumn()
  390.  
  391.     
  392.  
  393.     /**
  394.      * Extract sample image using single column marquee and
  395.      * draw it on destination image.
  396.      *
  397.      * @param resource $img GD image resource.
  398.      * @param int $dstX Top left x coordinate.
  399.      * @param int $dstY Top left y coordinate.
  400.      *
  401.      * @access private
  402.      */
  403.     function _extractSingleColumn(&$img$dstX$dstY)
  404.     {
  405.         $srcX $this->_marquee['points'];
  406.         $srcY = 0;
  407.         $srcW = 1;
  408.         $srcH imagesy($this->_sample);
  409.         imagecopymerge($img$this->_sample,
  410.                        $dstX$dstY$srcX$srcY,
  411.                        $srcW$srcH100);
  412.     }
  413.  
  414.     // }}}
  415.     // {{{ _extractSingleRow()
  416.  
  417.     
  418.  
  419.     /**
  420.      * Extract sample image using single column marquee and
  421.      * draw it on destination image.
  422.      *
  423.      * @param resource $img GD image resource.
  424.      * @param int $dstX Top left x coordinate.
  425.      * @param int $dstY Top left y coordinate.
  426.      *
  427.      * @access private
  428.      */
  429.     function _extractSingleRow(&$img$dstX$dstY)
  430.     {
  431.         $srcX = 0;
  432.         $srcY $this->_marquee['points'];
  433.         $srcW imagesx($this->_sample);
  434.         $srcH = 1;
  435.         imagecopymerge($img$this->_sample,
  436.                        $dstX$dstY$srcX$srcY,
  437.                        $srcW$srcH100);
  438.     }
  439.  
  440.     // }}}
  441.  
  442. }
  443.  
  444. // }}}
  445. /*
  446.  * Local variables:
  447.  * mode: php
  448.  * tab-width: 4
  449.  * c-basic-offset: 4
  450.  * c-hanging-comment-ender-p: nil
  451.  * End:
  452.  */
  453. ?>

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