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

Source for file Border.php

Documentation is available at Border.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_Border 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: Border.php,v 1.5 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 Image_Color for color handling.
  53.  */
  54. require_once 'Image/Color.php';
  55.  
  56. // }}}
  57. // {{{ Class: Image_Tools_Border
  58.  
  59.  
  60.  
  61. /**
  62.  * This class provide border creation on an image.
  63.  *
  64.  * @category    Images
  65.  * @package     Image_Tools
  66.  * @author      Firman Wandayandi <firman@php.net>
  67.  * @copyright   Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
  68.  * @license     http://www.opensource.org/licenses/bsd-license.php
  69.  *               BSD License
  70.  * @version     Release: 0.4.1
  71.  */
  72. {
  73.     // {{{ Properties
  74.  
  75.     
  76.  
  77.     /**
  78.      * Border options:
  79.      * <pre>
  80.      * image    mixed Destination image, a filename or an image string
  81.      *                data or a GD image resource.
  82.      * </pre>
  83.      *
  84.      * @var     array 
  85.      * @access  protected
  86.      */
  87.     var $options = array(
  88.         'image' => null    // An image.
  89.     
  90.     );
  91.  
  92.     /**
  93.      * Available options for Image_Tools_Border.
  94.      *
  95.      * @var     array 
  96.      * @access  protected
  97.      */
  98.     var $availableOptions = array(
  99.         'image' => 'mixed'
  100.     );
  101.  
  102.     /**
  103.      * Available methods for Image_Tool_Border (only public methods).
  104.      *
  105.      * @var     array 
  106.      * @access  protected
  107.      */
  108.     var $availableMethods = array(
  109.         'rounded' => array(
  110.             'radius'        => 'integer',
  111.             'background'    => 'mixed',
  112.             'antiAlias'     => 'integer'
  113.         ),
  114.         'bevel' => array(
  115.             'size'          => 'integer',
  116.             'highlight'     => 'mixed',
  117.             'shadow'        => 'mixed'
  118.         )
  119.     );
  120.  
  121.     /**
  122.      * Image_Tools_Border API version.
  123.      *
  124.      * @var     string 
  125.      * @access  protected
  126.      */
  127.     var $version = '0.1';
  128.  
  129.     /**
  130.      * Image width.
  131.      *
  132.      * @var     int 
  133.      * @access  private
  134.      */
  135.     var $_iWidth = 0;
  136.  
  137.     /**
  138.      * Image height.
  139.      *
  140.      * @var     int 
  141.      * @access  private
  142.      */
  143.     var $_iHeight = 0;
  144.  
  145.     // }}}
  146.     // {{{ _init()
  147.     
  148.     /**
  149.      * Initialize some internal variables.
  150.      *
  151.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  152.      * @access  private
  153.      */
  154.     function _init()
  155.     {
  156.         $res Image_Tools::createImage($this->options['image']);
  157.         if (PEAR::isError($res)) {
  158.             return $res;
  159.         }
  160.  
  161.         $this->resultImage = $res;
  162.         $this->_iWidth imagesx($this->resultImage);
  163.         $this->_iHeight imagesy($this->resultImage);
  164.  
  165.         return true;
  166.     }
  167.  
  168.     // }}}
  169.     // {{{ rounded()
  170.  
  171.     
  172.  
  173.     /**
  174.      * Make an image be a rounded edge.
  175.      *
  176.      * @param   int $radius optional Radius size.
  177.      * @param   mixed $background Background color.
  178.      * @param   int $antiAlias Anti-alias factor.
  179.      *
  180.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  181.      * @access  public
  182.      */
  183.     function rounded($radius = 3$background 'FFFFFF'$antiAlias = 3)
  184.     {
  185.         $res $this->_init();
  186.         if (PEAR::isError($res)) {
  187.             return $res;
  188.         }
  189.  
  190.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  191.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  192.         }
  193.  
  194.         $background = Image_Color::hex2rgb($background);
  195.         $antiAlias min(3$antiAlias);
  196.  
  197.         $iDot imagecreate(11);
  198.         imagecolorallocate($iDot$background[0]$background[1]$background[2]);
  199.  
  200.         for ($i = 0 - $radius$i <= $radius$i++{
  201.             $y $i < 0 ? $i $radius - 1 : $this->_iHeight ($radius $i);
  202.             for ($j = 0 - $radius$j <= $radius$j++{
  203.                 $x $j < 0 ? $j $radius - 1 : $this->_iWidth ($radius $j);
  204.                 if ($i != 0 || $j != 0{
  205.                     $distance round(sqrt(($i $i($j $j)));
  206.                     $opacity $distance $radius $antiAlias ?
  207.                                0 : max(0100 - (($radius $distance* 33));
  208.                     $opacity $distance $radius ? 100 : $opacity;
  209.                     imagecopymerge($this->resultImage$iDot$x$y0011$opacity);
  210.                 }
  211.             }
  212.         }
  213.  
  214.         imagedestroy($iDot);
  215.  
  216.         return true;
  217.     }
  218.  
  219.     // }}}
  220.     // {{{ bevel()
  221.  
  222.     
  223.  
  224.     /**
  225.      * Make an image bevel border.
  226.      *
  227.      * @param   int $size Border size.
  228.      * @param   mixed $highlight Highlight color.
  229.      * @param   mixed $shadow Shadow color.
  230.      *
  231.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  232.      * @access  public
  233.      */
  234.     function bevel($size = 8$highlight 'FFFFFF'$shadow '000000')
  235.     {
  236.         $res $this->_init();
  237.         if (PEAR::isError($res)) {
  238.             return $res;
  239.         }
  240.  
  241.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  242.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  243.         }
  244.  
  245.         $highlight = Image_Color::hex2rgb($highlight);
  246.         $shadow = Image_Color::hex2rgb($shadow);
  247.  
  248.         // Create an image resource for highlight.
  249.         $iLight imagecreate($this->_iWidth$this->_iHeight);
  250.         imagecolorallocate($iLight$highlight[0]$highlight[1]$highlight[2]);
  251.  
  252.         // Create an image resource for shadow.
  253.         $iShadow imagecreate(11);
  254.         imagecolorallocate($iShadow$shadow[0]$shadow[1]$shadow[2]);
  255.  
  256.         for ($j = 0; $j $size$j++{
  257.             $opacity =  100 - (($j + 1(100 / $size));
  258.             imagecopymerge($this->resultImage$iLight$j$j,
  259.                            001$this->_iHeight (2 * $j)$opacity);
  260.             imagecopymerge($this->resultImage$iLight$j - 1$j - 1,
  261.                            00$this->_iWidth (2 * $j)1$opacity);
  262.             imagecopymerge($this->resultImage$iShadow$this->_iWidth ($j + 1)$j,
  263.                            001$this->_iHeight (2 * $j)max(0$opacity - 10));
  264.             imagecopymerge($this->resultImage$iShadow$j$this->_iHeight ($j + 1),
  265.                            00$this->_iWidth (2 * $j)1max(0$opacity - 10));
  266.         }
  267.  
  268.         // Free highlight and shadow image resources.
  269.         imagedestroy($iLight);
  270.         imagedestroy($iShadow);
  271.  
  272.         return true;
  273.     }
  274.  
  275.     // }}}
  276.     // {{{ render()
  277.  
  278.     
  279.  
  280.     /**
  281.      * This method is useless, use directly call for specific border style
  282.      * method.
  283.      *
  284.      * @return  bool always TRUE.
  285.      * @access  protected
  286.      */
  287.     function render()
  288.     {
  289.         return true;
  290.     }
  291.  
  292.     // }}}
  293.  
  294. }
  295.  
  296. // }}}
  297. /*
  298.  * Local variables:
  299.  * mode: php
  300.  * tab-width: 4
  301.  * c-basic-offset: 4
  302.  * c-hanging-comment-ender-p: nil
  303.  * End:
  304.  */
  305. ?>

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