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.  * 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: Border.php,v 1.7 2008/05/26 06:13:06 firman Exp $
  29.  */
  30.  
  31. // }}}
  32. // {{{ Dependencies
  33.  
  34. /**
  35.  * Image_Tools
  36.  */
  37. require_once 'Image/Tools.php';
  38.  
  39. /**
  40.  * Image_Tools_Utils
  41.  */
  42. require_once 'Image/Tools/Utils.php';
  43.  
  44. // }}}
  45. // {{{ Class: Image_Tools_Border
  46.  
  47. /**
  48.  * This class provide border creation on an image.
  49.  *
  50.  * @category    Images
  51.  * @package     Image_Tools
  52.  * @author      Firman Wandayandi <firman@php.net>
  53.  * @copyright   Copyright (c) 2005-2006 Firman Wandayandi <firman@php.net>
  54.  * @license     http://www.opensource.org/licenses/bsd-license.php
  55.  *               BSD License
  56.  * @version     Release: 1.0.0RC1
  57.  */
  58. {
  59.     // {{{ Properties
  60.  
  61.     /**
  62.      * Border options:
  63.      * <pre>
  64.      * image    mixed   Destination image, a filename or an image string
  65.      *                  data or a GD image resource.
  66.      * style    string  Border style
  67.      * params   array   Parameters
  68.      * </pre>
  69.      *
  70.      * @var     array 
  71.      * @access  protected
  72.      */
  73.     var $options = array(
  74.         'image'  => null,
  75.         'style'  => 'rounded',
  76.         'params' => array()
  77.     );
  78.  
  79.     /**
  80.      * Available options for Image_Tools_Border.
  81.      *
  82.      * @var     array 
  83.      * @access  protected
  84.      */
  85.     var $availableOptions = array(
  86.         'image'  => 'mixed',
  87.         'style'  => 'string',
  88.         'params' => 'array'
  89.     );
  90.  
  91.     /**
  92.      * Available methods for Image_Tool_Border (only public methods).
  93.      *
  94.      * @var     array 
  95.      * @access  protected
  96.      */
  97.     var $availableMethods = array(
  98.         'rounded' => array(
  99.             'radius'        => 'integer',
  100.             'background'    => 'mixed',
  101.             'antiAlias'     => 'integer'
  102.         ),
  103.         'bevel' => array(
  104.             'size'          => 'integer',
  105.             'highlight'     => 'mixed',
  106.             'shadow'        => 'mixed'
  107.         ),
  108.         'line' => array(
  109.             'width'         => 'integer',
  110.             'color'         => 'mixed',
  111.             'offset'        => 'integer',
  112.             'background'    => 'mixed'
  113.         )
  114.     );
  115.  
  116.     /**
  117.      * Image_Tools_Border API version.
  118.      *
  119.      * @var     string 
  120.      * @access  protected
  121.      */
  122.     var $version = '1.0';
  123.  
  124.     /**
  125.      * Image width.
  126.      *
  127.      * @var     int 
  128.      * @access  private
  129.      */
  130.     var $_iWidth = 0;
  131.  
  132.     /**
  133.      * Image height.
  134.      *
  135.      * @var     int 
  136.      * @access  private
  137.      */
  138.     var $_iHeight = 0;
  139.  
  140.     // }}}
  141.     // {{{ preRender()
  142.     /**
  143.      * Function which called before render.
  144.      *
  145.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  146.      * @access  protected
  147.      */
  148.     function preRender()
  149.     {
  150.         $res Image_Tools::createImage($this->options['image']);
  151.         if (PEAR::isError($res)) {
  152.             return $res;
  153.         }
  154.  
  155.         $this->resultImage = $res;
  156.         $this->_iWidth imagesx($this->resultImage);
  157.         $this->_iHeight imagesy($this->resultImage);
  158.  
  159.         return true;
  160.     }
  161.  
  162.     // }}}
  163.     // {{{ _rounded()
  164.  
  165.     /**
  166.      * Make an image be a rounded edge.
  167.      *
  168.      * @param   int $radius optional Radius size.
  169.      * @param   mixed $background Background color.
  170.      * @param   int $antiAlias Anti-alias factor.
  171.      *
  172.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  173.      * @access  private
  174.      */
  175.     function _rounded($radius = 3$background 'FFFFFF'$antiAlias = 3)
  176.     {
  177.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  178.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  179.         }
  180.  
  181.         $background Image_Tools_Utils::colorToRGBA($background);
  182.         $antiAlias min(3$antiAlias);
  183.  
  184.         $iDot imagecreate(11);
  185.         imagecolorallocate($iDot$background['r']$background['g']$background['b']);
  186.  
  187.         for ($i = 0 - $radius$i <= $radius$i++{
  188.             $y $i < 0 ? $i $radius - 1 : $this->_iHeight ($radius $i);
  189.             for ($j = 0 - $radius$j <= $radius$j++{
  190.                 $x $j < 0 ? $j $radius - 1 : $this->_iWidth ($radius $j);
  191.                 if ($i != 0 || $j != 0{
  192.                     $distance round(sqrt(($i $i($j $j)));
  193.                     $opacity $distance $radius $antiAlias ?
  194.                                0 : max(0100 - (($radius $distance* 33));
  195.                     $opacity $distance $radius ? 100 : $opacity;
  196.                     imagecopymerge($this->resultImage$iDot$x$y0011$opacity);
  197.                 }
  198.             }
  199.         }
  200.  
  201.         imagedestroy($iDot);
  202.  
  203.         return true;
  204.     }
  205.  
  206.     // }}}
  207.     // {{{ _bevel()
  208.  
  209.     /**
  210.      * Make an image bevel border.
  211.      *
  212.      * @param   int $size Border size.
  213.      * @param   mixed $highlight Highlight color.
  214.      * @param   mixed $shadow Shadow color.
  215.      *
  216.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  217.      * @access  private
  218.      */
  219.     function _bevel($size = 8$highlight 'FFFFFF'$shadow '000000')
  220.     {
  221.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  222.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  223.         }
  224.  
  225.         $highlight Image_Tools_Utils::colorToRGBA($highlight);
  226.         $shadow Image_Tools_Utils::colorToRGBA($shadow);
  227.  
  228.         // Create an image resource for highlight.
  229.         $iLight imagecreate($this->_iWidth$this->_iHeight);
  230.         imagecolorallocate($iLight$highlight['r']$highlight['g']$highlight['b']);
  231.  
  232.         // Create an image resource for shadow.
  233.         $iShadow imagecreate(11);
  234.         imagecolorallocate($iShadow$shadow['r']$shadow['g']$shadow['b']);
  235.  
  236.         for ($j = 0; $j $size$j++{
  237.             $opacity =  100 - (($j + 1(100 / $size));
  238.             imagecopymerge($this->resultImage$iLight$j$j,
  239.                            001$this->_iHeight (2 * $j)$opacity);
  240.             imagecopymerge($this->resultImage$iLight$j - 1$j - 1,
  241.                            00$this->_iWidth (2 * $j)1$opacity);
  242.             imagecopymerge($this->resultImage$iShadow$this->_iWidth ($j + 1)$j,
  243.                            001$this->_iHeight (2 * $j)max(0$opacity - 10));
  244.             imagecopymerge($this->resultImage$iShadow$j$this->_iHeight ($j + 1),
  245.                            00$this->_iWidth (2 * $j)1max(0$opacity - 10));
  246.         }
  247.  
  248.         // Free highlight and shadow image resources.
  249.         imagedestroy($iLight);
  250.         imagedestroy($iShadow);
  251.  
  252.         return true;
  253.     }
  254.  
  255.     // }}}
  256.     // {{{ _line()
  257.  
  258.     /**
  259.      * Draw a line around an image
  260.      *
  261.      * Draw a line around an image. If $offset is 0, the line is just around the image.
  262.      * If $offset is positive, the line is outside the image, and the gad is filled with background color.
  263.      * If $offset is -width / 2, the line is centered on the border of the image.
  264.      * If $offset is -$width, the line in just inside the image.
  265.      * If $offset is negative, the line is inside the image.
  266.      *
  267.      * @param   int $width Line width
  268.      * @param   mixed $color Line color
  269.      * @param   int $offset Distance between line and image.
  270.      *
  271.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  272.      * @access  private
  273.      * @author  Charles Brunet <charles.fmj@gmail.com>
  274.      */
  275.     function _line($width = 2$color '000000'$offset = 0$background 'FFFFFF')
  276.     {
  277.         // Agrandir l'image si nécessaire
  278.         $mag $width $offset;
  279.         if ($mag > 0{
  280.             $w$this->_iWidth (2 * $mag);
  281.             $h$this->_iHeight (2 * $mag);
  282.  
  283.             // Create the target image
  284.             if function_exists('imagecreatetruecolor') ) {
  285.                 $target imagecreatetruecolor($w$h);
  286.             else {
  287.                 $target imagecreate($w$h);
  288.             }
  289.             if Image_Tools::isGDImageResource($target) ) {
  290.                 return PEAR::raiseError('Cannot initialize new GD image stream');
  291.             }
  292.             $background Image_Tools_Utils::colorToRGBA($background);
  293.             $background imagecolorallocate($target$background['r']$background['g']$background['b']);
  294.             imagefilledrectangle($target00$w-1$h-1$background);
  295.             imagecopy($target$this->resultImage$mag$mag00$this->_iWidth$this->_iHeight);
  296.             $this->_iWidth $w;
  297.             $this->_iHeight $h;
  298.             imagedestroy($this->resultImage);
  299.             $this->resultImage = $target;
  300.         }
  301.  
  302.         if (!Image_Tools::isGDImageResource($this->resultImage)) {
  303.             return PEAR::raiseError('Invalid image resource Image_Tools_Mask::$_resultImage');
  304.         }
  305.  
  306.         $color Image_Tools_Utils::colorToRGBA($color);
  307.         $color imagecolorallocate($this->resultImage$color['r']$color['g']$color['b']);
  308.  
  309.         $a ($mag < 0)?0-$mag:0;
  310.         for ($i=$a$i<$a+$width; ++$i{
  311.             imagerectangle($this->resultImage$i$i$this->_iWidth-$i-1$this->_iHeight-$i-1$color);
  312.         }
  313.  
  314.         return true;
  315.     }
  316.  
  317.     // }}}
  318.     // {{{ render()
  319.  
  320.     /**
  321.      * This method is useless, use directly call for specific border style
  322.      * method.
  323.      *
  324.      * @return  TRUE|PEAR_Error
  325.      * @access  protected
  326.      */
  327.     function render()
  328.     {
  329.         $callback = array($this"_{$this->options['style']}");
  330.         if (!is_callable($callback)) {
  331.             return PEAR::raiseError('Invalid border style or not supported');
  332.         }
  333.  
  334.         return call_user_func_array($callback$this->options['params']);
  335.     }
  336.  
  337.     // }}}
  338. }
  339.  
  340. // }}}
  341.  
  342. /*
  343.  * Local variables:
  344.  * mode: php
  345.  * tab-width: 4
  346.  * c-basic-offset: 4
  347.  * c-hanging-comment-ender-p: nil
  348.  * End:
  349.  */
  350. ?>

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