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

Source for file Watermark.php

Documentation is available at Watermark.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker */
  3.  
  4. /**
  5.  * This is a driver file contains the Image_Tools_Watermark class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE:
  10.  *
  11.  * Copyright (c) 2008 Charles Brunet <charles.fmj@gmail.com>
  12.  *
  13.  * This source file is subject to the BSD License license that is bundled
  14.  * with this package in the file LICENSE.txt.
  15.  * It is also available through the world-wide-web at this URL:
  16.  * http://www.opensource.org/licenses/bsd-license.php
  17.  * If you did not receive a copy of the license and are unable to
  18.  * obtain it through the world-wide-web, please send an email
  19.  * to pear-dev@list.php.net so we can send you a copy immediately.
  20.  *
  21.  * @category    Images
  22.  * @package     Image_Tools
  23.  * @author      Charles Brunet <charles.fmj@gmail.com>
  24.  * @copyright   Copyright (c) 2008 Charles Brunet <charles.fmj@gmail.com>
  25.  * @license     http://www.opensource.org/licenses/bsd-license.php
  26.  *               BSD License
  27.  * @since       File available since Release 1.0.0RC1
  28.  */
  29.  
  30. require_once 'Image/Tools.php';
  31.  
  32. // {{{ Constants
  33.  
  34. /**
  35. * Position for the logo
  36. */
  37. define('IMAGE_TOOLS_WATERMARK_POSITION_LEFT'0);
  38. define('IMAGE_TOOLS_WATERMARK_POSITION_CENTER'1);
  39. define('IMAGE_TOOLS_WATERMARK_POSITION_RIGHT'2);
  40. define('IMAGE_TOOLS_WATERMARK_POSITION_TOP'0);
  41. define('IMAGE_TOOLS_WATERMARK_POSITION_MIDDLE'1);
  42. define('IMAGE_TOOLS_WATERMARK_POSITION_BOTTOM'2);
  43.  
  44. // }}}
  45. // {{{ Class: Image_Tools_Watermark
  46.  
  47. /**
  48.  * This class provide a tool to add a little logo on an image
  49.  *
  50.  * @category    Images
  51.  * @package     Image_Tools
  52.  * @author      Charles Brunet <charles.fmj@gmail.com>
  53.  * @copyright   Copyright (c) 2008 Charles Brunet <charles.fmj@gmail.com>
  54.  * @license     http://www.opensource.org/licenses/bsd-license.php
  55.  *               BSD License
  56.  * @version     Release: 1.0.0RC1
  57.  * @since       Class available since Release 1.0.0RC1
  58. */
  59. {
  60.     // {{{ Properties
  61.  
  62.     /**
  63.      * Thumbnail options:
  64.      * <pre>
  65.      * image    mixed  Destination image, a filename or an image string data or a GD image resource
  66.      * logo     mixed  Source logo, a filename or an image string data or a GD image resource
  67.      * width    int    Width of logo; 0 to keep original width
  68.      * height   int    Height of logo; 0 to keep original height
  69.      * marginx  int    Horizontal margin from the border of the image
  70.      * marginy  int    Vertical margin from the border of the image
  71.      * horipos  number Horizontal position of the logo on destination image
  72.      * vertpos  number Vertical position of the logo on destination image
  73.      * blend    string Blending mode to use on composite operation, see
  74.      *                 Image_Tools_Blend of the supported blend mode. Default
  75.      *                 is none (no blending).
  76.      * </pre>
  77.      *
  78.      * @var     array 
  79.      * @access  protected
  80.      */
  81.     var $options = array(
  82.         'image'   => null,
  83.         'mark'    => null,
  84.         'width'   => 0,
  85.         'height'  => 0,
  86.         'marginx' => 0,
  87.         'marginy' => 0,
  88.         'horipos' => IMAGE_TOOLS_WATERMARK_POSITION_RIGHT,
  89.         'vertpos' => IMAGE_TOOLS_WATERMARK_POSITION_BOTTOM,
  90.         'blend'   => 'none'
  91.     );
  92.  
  93.     /**
  94.      * Available options for Image_Tools_Watermark
  95.      *
  96.      * @var     array 
  97.      * @access  protected
  98.      */
  99.     var $availableOptions = array(
  100.         'image'   => 'mixed',
  101.         'mark'    => 'mixed',
  102.         'width'   => 'int',
  103.         'height'  => 'int',
  104.         'marginx' => 'int',
  105.         'marginy' => 'int',
  106.         'horipos' => 'number',
  107.         'vertpos' => 'number',
  108.         'blend'   => 'string'
  109.     );
  110.  
  111.     /**
  112.      * Image_Tools_Watermark API version.
  113.      *
  114.      * @var     string 
  115.      * @access  protected
  116.      */
  117.     var $version = '0.2';
  118.  
  119.     /**
  120.      * Resource of source image.
  121.      *
  122.      * @var resource 
  123.      * @access private
  124.      */
  125.     var $_source = null;
  126.  
  127.     /**
  128.      * Resource of mark image.
  129.      *
  130.      * @var resource 
  131.      * @access private
  132.      */
  133.     var $_mark = null;
  134.  
  135.     // }}}
  136.     // {{{ preRender()
  137.  
  138.     /**
  139.      * Function which called before render.
  140.      *
  141.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  142.      * @access  protected
  143.      * @see     Image_Tools::createImage()
  144.      */
  145.     function preRender()
  146.     {
  147.         // Create the source image
  148.         $source Image_Tools::createImage($this->options['image']);
  149.         if (PEAR::isError($source)) {
  150.             return $source;
  151.         }
  152.         $this->_source $source;
  153.  
  154.         // Create the mark image
  155.         $mark Image_Tools::createImage($this->options['mark']);
  156.         if (PEAR::isError($mark)) {
  157.             return $mark;
  158.         }
  159.         $this->_mark $mark;
  160.  
  161.         // includes the Image_Tools_Blend if blend mode enable
  162.         if ($this->options['blend'!= 'none'{
  163.             require_once 'Image/Tools/Blend.php';
  164.         }
  165.     }
  166.  
  167.     // }}}
  168.     // {{{ render()
  169.  
  170.     /**
  171.      * Draw image with logo result to resource.
  172.      *
  173.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  174.      * @access  public
  175.      */
  176.     function render()
  177.     {
  178.         $W imagesx($this->_source);
  179.         $H imagesy($this->_source);
  180.  
  181.         $MW imagesx($this->_mark);
  182.         $MH imagesy($this->_mark);
  183.  
  184.         // resize the logo image
  185.         if ($this->options['width'== 0 && $this->options['height'== 0{
  186.             $this->options['width'$MW;
  187.             $this->options['height'$MH;
  188.         else if ($this->options['width'== 0{
  189.             $this->options['width'ceil($this->options['height'$MW $MH);
  190.         else if ($this->options['height'== 0{
  191.             $this->options['height'ceil($this->options['width'$MH $MW);
  192.         }
  193.  
  194.         // calculates the x position
  195.         switch ($this->options['horipos']{
  196.             case IMAGE_TOOLS_WATERMARK_POSITION_LEFT:
  197.                 $posx $this->options['marginx'];
  198.                 break;
  199.             case IMAGE_TOOLS_WATERMARK_POSITION_CENTER:
  200.                 $posx round(($W $this->options['width'])/2);
  201.                 break;
  202.             case IMAGE_TOOLS_WATERMARK_POSITION_RIGHT:
  203.             default:
  204.                 $posx $W $this->options['width'$this->options['marginx'];
  205.                 break;
  206.         }
  207.  
  208.         // calculates the y position
  209.         switch ($this->options['vertpos']{
  210.             case IMAGE_TOOLS_WATERMARK_POSITION_TOP:
  211.                 $posy $this->options['marginy'];
  212.                 break;
  213.             case IMAGE_TOOLS_WATERMARK_POSITION_MIDDLE:
  214.                 $posy round(($H $this->options['height'])/2);
  215.                 break;
  216.             case IMAGE_TOOLS_WATERMARK_POSITION_BOTTOM:
  217.             default:
  218.                 $posy $H $this->options['height'$this->options['marginy'];
  219.             break;
  220.         }
  221.  
  222.         settype($posx'integer');
  223.         settype($posy'integer');
  224.  
  225.         // Create the target image
  226.         imagealphablending($this->_markfalse);
  227.  
  228.         // resize the mark image
  229.         if ($MH != $this->options['height'&& $MW != $this->options['width']{
  230.             if (function_exists('imagecreatetruecolor')) {
  231.                 $mark imagecreatetruecolor($this->options['width']$this->options['height']);
  232.             else {
  233.                 $mark imagecreate($this->options['width']$this->options['height']);
  234.             }
  235.  
  236.             imagealphablending($markfalse);
  237.  
  238.             if (function_exists('imagecoypresampled')) {
  239.                 $result imagecopyresampled(
  240.                     $mark$this->_mark0000,
  241.                     $this->options['width']$this->options['height']$MW$MH
  242.                 );
  243.             else {
  244.                 $result imagecopyresized(
  245.                     $mark$this->_mark0000,
  246.                     $this->options['width']$this->options['height']$MW$MH
  247.                 );
  248.             }
  249.  
  250.             $this->_mark $mark;
  251.         }
  252.  
  253.         if ($this->options['blend'!= 'none'{
  254.             $blend Image_Tools::factory('blend');
  255.             $blend->set('image1'$this->_source);
  256.             $blend->set('image2'$this->_mark);
  257.             $blend->set('x'$posx);
  258.             $blend->set('y'$posy);
  259.             $blend->set('mode'$this->options['blend']);
  260.  
  261.             // applies the blending mode.
  262.             $this->_source $blend->getResultImage();
  263.         else {
  264.             $result imagecopy($this->_source$this->_mark$posx$posy00$MW$MH);
  265.             if (!$result{
  266.                 return PEAR::raiseError('Cannot copy logo image');
  267.             }
  268.         }
  269.  
  270.         $this->resultImage = $this->_source;
  271.         unset($this->_mark);
  272.  
  273.         return true;
  274.     }
  275.  
  276.     // }}}
  277. }
  278.  
  279. // }}}
  280.  
  281. /*
  282.  * Local variables:
  283.  * mode: php
  284.  * tab-width: 4
  285.  * c-basic-offset: 4
  286.  * c-hanging-comment-ender-p: nil
  287.  * End:
  288.  */
  289. ?>

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