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-2008 Firman Wandayandi <firman@php.net>
  14.  *
  15.  * This source file is subject to the BSD License license that is bundled
  16.  * with this package in the file LICENSE.txt.
  17.  * It is also available through the world-wide-web at this URL:
  18.  * http://www.opensource.org/licenses/bsd-license.php
  19.  * If you did not receive a copy of the license and are unable to
  20.  * obtain it through the world-wide-web, please send an email
  21.  * to pear-dev@list.php.net so we can send you a copy immediately.
  22.  *
  23.  * @category    Images
  24.  * @package     Image_Tools
  25.  * @author      Firman Wandayandi <firman@php.net>
  26.  * @copyright   Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
  27.  * @license     http://www.opensource.org/licenses/bsd-license.php
  28.  *               BSD License
  29.  * @version     CVS: $Id: Marquee.php,v 1.4 2008/05/26 06:30:00 firman Exp $
  30.  */
  31.  
  32. // }}}
  33. // {{{ Dependencies
  34.  
  35. /**
  36.  * Load Image_Tools as the base class.
  37.  */
  38. require_once 'Image/Tools.php';
  39.  
  40. // }}}
  41. // {{{ Constants
  42.  
  43. /**
  44.  * Image_Tools_Marquee rectangle marquee.
  45.  *
  46.  * @name IMAGE_TOOLS_MARQUEE_TYPE_RECTANGLE
  47.  * @access public
  48.  */
  49. define('IMAGE_TOOLS_MARQUEE_TYPE_RECTANGLE''marquee_rectangle');
  50.  
  51. /**
  52.  * Image_Tools_Marquee single column marquee.
  53.  *
  54.  * @name IMAGE_TOOLS_MARQUEE_TYPE_SINGLECOL
  55.  * @access public
  56.  */
  57. define('IMAGE_TOOLS_MARQUEE_TYPE_SINGLECOL''marquee_singlecol');
  58.  
  59. /**
  60.  * Image_Tools_Marquee single row marquee.
  61.  *
  62.  * @name IMAGE_TOOLS_MARQUEE_TYPE_SINGLEROW
  63.  * @access public
  64.  */
  65. define('IMAGE_TOOLS_MARQUEE_TYPE_SINGLEROW''marquee_singlerow');
  66.  
  67. /**
  68.  * Image_Tools_Marquee polygon marquee.
  69.  * Notes: this constant is useless at this time.
  70.  *
  71.  * @name IMAGE_TOOLS_MARQUEE_TYPE_POLYGON
  72.  * @access public
  73.  */
  74. define('IMAGE_TOOLS_MARQUEE_TYPE_POLYGON''marquee_polygon');
  75.  
  76. // }}}
  77. // {{{ Class: Image_Tools_Marquee
  78.  
  79. /**
  80.  * This class provide marquee extraction tool for manipulating an image.
  81.  *
  82.  * @category    Images
  83.  * @package     Image_Tools
  84.  * @author      Firman Wandayandi <firman@php.net>
  85.  * @copyright   Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
  86.  * @license     http://www.opensource.org/licenses/bsd-license.php
  87.  *               BSD License
  88.  * @version     Release: 1.0.0RC1
  89.  */
  90. {
  91.     // {{{ Properties
  92.  
  93.     /**
  94.      * Marquee options:
  95.      * <pre>
  96.      * sample  mixed  Sample image, a GD image resource or string filename.
  97.      * x       int    X position, this sets X position where marquee will be
  98.      *                placed.
  99.      * y       int    Y position, this sets Y position where marquee will be
  100.      *                placed.
  101.      * </pre>
  102.      *
  103.      * @var     array 
  104.      * @access  protected
  105.      */
  106.     var $options = array(
  107.         'image'     => null,   // Destination image.
  108.         'sample'    => null,   // Sample image.
  109.         'x'         => 0,      // X position.
  110.         'y'         => 0       // Y position.
  111.     );
  112.  
  113.     /**
  114.      * Available options for Image_Tools_Marquee.
  115.      *
  116.      * @var     array 
  117.      * @access  protected
  118.      */
  119.     var $availableOptions = array(
  120.         'image'     => 'mixed',
  121.         'sample'    => 'mixed',
  122.         'x'         => 'int',
  123.         'y'         => 'int'
  124.     );
  125.  
  126.     /**
  127.      * Available methods for Image_Tool_Marquee (only public methods).
  128.      *
  129.      * @var     array 
  130.      * @access  protected
  131.      */
  132.     var $availableMethods = array(
  133.         'setRectangleMarquee' => array(
  134.             'topLeftX'     => 'int',
  135.             'topLeftY'     => 'int',
  136.             'bottomRightX' => 'int',
  137.             'bottomRightY' => 'int'
  138.         ),
  139.         'setSingleColMarquee' => array(
  140.             'x' => 'int'
  141.         ),
  142.         'setSingleRowMarquee' => array(
  143.             'y' => 'int'
  144.         )
  145.     );
  146.  
  147.     /**
  148.      * Image_Tools_Marquee API version.
  149.      *
  150.      * @var     string 
  151.      * @access  protected
  152.      */
  153.     var $version = '1.0';
  154.  
  155.     /**
  156.      * Marquee information.
  157.      *
  158.      * @var     array 
  159.      * @access  private
  160.      */
  161.     var $_marquee = array();
  162.  
  163.     /**
  164.      * Sample image.
  165.      *
  166.      * @var     resource 
  167.      * @access  private
  168.      */
  169.     var $_sample = null;
  170.  
  171.     // }}}
  172.     // {{{ preRender()
  173.  
  174.     /**
  175.      * Function which called before render.
  176.      *
  177.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  178.      * @access  protected
  179.      * @see     Image_Tools::createImage()
  180.      */
  181.     function preRender()
  182.     {
  183.         $res Image_Tools::createImage($this->options['sample']);
  184.         if (PEAR::isError($res)) {
  185.             return $res;
  186.         }
  187.         $this->_sample $res;
  188.  
  189.         $res Image_Tools::createImage($this->options['image']);
  190.         if (PEAR::isError($res)) {
  191.             return $res;
  192.         }
  193.         $this->resultImage = $res;
  194.  
  195.         return true;
  196.     }
  197.  
  198.     // }}}
  199.     // {{{ setRectangleMarquee()
  200.  
  201.     /**
  202.      * Set marquee using rectangle.
  203.      *
  204.      * @param   int $topLeftX Top left x coordinate.
  205.      * @param   int $topLeftY Top left y coordinate.
  206.      * @param   int $bottomRightX Bottom right x coordinate.
  207.      * @param   int $bottomRightY Bottom right y coordinate.
  208.      *
  209.      * @return  bool Always TRUE.
  210.      * @access  public
  211.      * @see     _setMarquee()
  212.      */
  213.     function setRectangleMarquee($topLeftX$topLeftY,
  214.                                  $bottomRightX$bottomRightY)
  215.     {
  216.         if ($topLeftX $bottomRightX{
  217.             $tmp $topLeftX;
  218.             $topLeftX $bottomRightX;
  219.             $bottomRightX $tmp;
  220.         }
  221.  
  222.         if ($topLeftY $bottomRightY{
  223.             $tmp $bottomRightY;
  224.             $topLeftY $bottomRightY;
  225.             $bottomRightY $tmp;
  226.         }
  227.  
  228.         $points = array($topLeftX$topLeftY$bottomRightX$bottomRightY);
  229.         $this->_setMarquee(IMAGE_TOOLS_MARQUEE_TYPE_RECTANGLE$points);
  230.         return true;
  231.     }
  232.  
  233.     // }}}
  234.     // {{{ setSingleColumnMarquee()
  235.  
  236.     /**
  237.      * Set marquee using single column marquee.
  238.      *
  239.      * @param   int $x X coordinate.
  240.      *
  241.      * @return  bool Always TRUE.
  242.      * @access  public
  243.      * @see     _setMarquee()
  244.      */
  245.     function setSingleColumnMarquee($x)
  246.     {
  247.         $this->_setMarquee(IMAGE_EXTRACT_MARQUEE_TYPE_SINGLECOL$x);
  248.         return true;
  249.     }
  250.  
  251.     // }}}
  252.     // {{{ setSingleRowMarquee()
  253.  
  254.     /**
  255.      * Set marquee using single row marquee.
  256.      *
  257.      * @param int $y Y coordinate.
  258.      *
  259.      * @return bool Always TRUE.
  260.      * @access public
  261.      * @see Image_Tools_Marquee::_setMarquee()
  262.      */
  263.     function setSingleRowMarquee($y)
  264.     {
  265.         $this->_setMarquee(IMAGE_EXTRACT_MARQUEE_TYPE_SINGLEROW$y);
  266.         return true;
  267.     }
  268.  
  269.     // }}}
  270.     // {{{ render()
  271.  
  272.     /**
  273.      * Draw extraction result to resource.
  274.      *
  275.      * @return bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  276.      * @access public
  277.      * @see Image_Tools_Marquee::_extractRectangle(),
  278.      *       Image_Tools_Marquee::_extractSingleCol(),
  279.      *       Image_Tools_Marquee::_extractSingleRow()
  280.      */
  281.     function render()
  282.     {
  283.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  284.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  285.         }
  286.  
  287.         $x $this->options['x'];
  288.         $y $this->options['y'];
  289.  
  290.         switch ($this->_marquee['type']{
  291.             case IMAGE_TOOLS_MARQUEE_TYPE_RECTANGLE:
  292.                 $this->_extractRectangle($this->resultImage$x$y);
  293.                 break;
  294.             case IMAGE_TOOLS_MARQUEE_TYPE_SINGLECOL:
  295.                 $this->_extractSingleColumn($this->resultImage$x$y);
  296.                 break;
  297.             case IMAGE_TOOLS_MARQUEE_TYPE_SINGLEROW:
  298.                 $this->_extractSingleRow($this->resultImage$x$y);
  299.                 break;
  300.         }
  301.  
  302.         return true;
  303.     }
  304.  
  305.     // }}}
  306.     // {{{ _setMarquee()
  307.  
  308.     /**
  309.      * Set marquee informations.
  310.      *
  311.      * @param string $type Marquee type. Use marquee type defined constants.
  312.      * @param mixed $points Marquee coordinate points.
  313.      *
  314.      * @access private
  315.      */
  316.     function _setMarquee($type$points)
  317.     {
  318.         $this->_marquee = array(
  319.                             'type' => $type,
  320.                             'points' => $points
  321.                           );
  322.     }
  323.  
  324.     // }}}
  325.     // {{{ _extractRectangle()
  326.  
  327.     /**
  328.      * Extract sample image using rectangle marquee and
  329.      * draw it on destination image.
  330.      *
  331.      * @param resource $img GD image resource.
  332.      * @param int $dstX Top left x coordinate.
  333.      * @param int $dstY Top left y coordinate.
  334.      *
  335.      * @access private
  336.      */
  337.     function _extractRectangle(&$img$dstX$dstY)
  338.     {
  339.         $srcX $this->_marquee['points'][0];
  340.         $srcY $this->_marquee['points'][1];
  341.         $srcW $this->_marquee['points'][2$srcX;
  342.         $srcH $this->_marquee['points'][3$srcY;
  343.         imagecopymerge($img$this->_sample,
  344.                        $dstX$dstY$srcX$srcY,
  345.                        $srcW$srcH100);
  346.     }
  347.  
  348.     // }}}
  349.     // {{{ _extractSingleColumn()
  350.  
  351.     /**
  352.      * Extract sample image using single column marquee and
  353.      * draw it on destination image.
  354.      *
  355.      * @param resource $img GD image resource.
  356.      * @param int $dstX Top left x coordinate.
  357.      * @param int $dstY Top left y coordinate.
  358.      *
  359.      * @access private
  360.      */
  361.     function _extractSingleColumn(&$img$dstX$dstY)
  362.     {
  363.         $srcX $this->_marquee['points'];
  364.         $srcY = 0;
  365.         $srcW = 1;
  366.         $srcH imagesy($this->_sample);
  367.         imagecopymerge($img$this->_sample,
  368.                        $dstX$dstY$srcX$srcY,
  369.                        $srcW$srcH100);
  370.     }
  371.  
  372.     // }}}
  373.     // {{{ _extractSingleRow()
  374.  
  375.     /**
  376.      * Extract sample image using single column marquee and
  377.      * draw it on destination image.
  378.      *
  379.      * @param resource $img GD image resource.
  380.      * @param int $dstX Top left x coordinate.
  381.      * @param int $dstY Top left y coordinate.
  382.      *
  383.      * @access private
  384.      */
  385.     function _extractSingleRow(&$img$dstX$dstY)
  386.     {
  387.         $srcX = 0;
  388.         $srcY $this->_marquee['points'];
  389.         $srcW imagesx($this->_sample);
  390.         $srcH = 1;
  391.         imagecopymerge($img$this->_sample,
  392.                        $dstX$dstY$srcX$srcY,
  393.                        $srcW$srcH100);
  394.     }
  395.  
  396.     // }}}
  397. }
  398.  
  399. // }}}
  400.  
  401. /*
  402.  * Local variables:
  403.  * mode: php
  404.  * tab-width: 4
  405.  * c-basic-offset: 4
  406.  * c-hanging-comment-ender-p: nil
  407.  * End:
  408.  */
  409. ?>

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