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

Source for file Thumbnail.php

Documentation is available at Thumbnail.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_Thumbnail class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE:
  10.  *
  11.  * Copyright (c) 2006 Ildar N. Shaimordanov <ildar-sh@mail.ru>
  12.  * All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without
  15.  * modification, are permitted under the terms of the BSD License.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category    Images
  31.  * @package     Image_Tools
  32.  * @author      Ildar N. Shaimordanov <ildar-sh@mail.ru>
  33.  * @copyright   Copyright (c) 2006 Ildar N. Shaimordanov <ildar-sh@mail.ru>
  34.  * @license     http://www.opensource.org/licenses/bsd-license.php
  35.  *               BSD License
  36.  * @version     CVS: $Id: Thumbnail.php,v 1.3 2006/11/23 22:17:31 firman Exp $
  37.  */
  38.  
  39. require_once 'Image/Tools.php';
  40.  
  41. // {{{ Constants
  42.  
  43. /**
  44. * Maximal scaling
  45. */
  46. define('IMAGE_TOOLS_THUMBNAIL_METHOD_SCALE_MAX'0);
  47.  
  48. /**
  49. * Minimal scaling
  50. */
  51. define('IMAGE_TOOLS_THUMBNAIL_METHOD_SCALE_MIN'1);
  52.  
  53. /**
  54. * Cropping of fragment
  55. */
  56. define('IMAGE_TOOLS_THUMBNAIL_METHOD_CROP',      2);
  57.  
  58. /**
  59. * Align constants
  60. */
  61. define('IMAGE_TOOLS_THUMBNAIL_ALIGN_CENTER'0);
  62. define('IMAGE_TOOLS_THUMBNAIL_ALIGN_LEFT',   -1);
  63. define('IMAGE_TOOLS_THUMBNAIL_ALIGN_RIGHT',  +1);
  64. define('IMAGE_TOOLS_THUMBNAIL_ALIGN_TOP',    -1);
  65. define('IMAGE_TOOLS_THUMBNAIL_ALIGN_BOTTOM'+1);
  66.  
  67. // }}}
  68. // {{{ Class: Image_Tools_Thumbnail
  69.  
  70.  
  71.  
  72. /**
  73.  * This class provide thumbnail creating tool for manipulating an image
  74.  *
  75.  * @category    Images
  76.  * @package     Image_Tools
  77.  * @author      Ildar N. Shaimordanov <ildar-sh@mail.ru>
  78.  * @copyright   Copyright (c) 2006 Ildar N. Shaimordanov <ildar-sh@mail.ru>
  79.  * @license     http://www.opensource.org/licenses/bsd-license.php
  80.  *               BSD License
  81.  * @version     Release: 0.4.1
  82. */
  83. {
  84.     // {{{ Properties
  85.  
  86.     
  87.  
  88.     /**
  89.      * Thumbnail options:
  90.      * <pre>
  91.      * image   mixed  Destination image, a filename or an image string data or a GD image resource
  92.      * width   int    Width of thumbnail
  93.      * height  int    Height of thumbnail
  94.      * percent number Size of thumbnail per size of original image
  95.      * method  int    Method of thumbnail creating
  96.      * halign  int    Horizontal align
  97.      * valign  int    Vertical align
  98.      * </pre>
  99.      *
  100.      * @var     array 
  101.      * @access  protected
  102.      */
  103.     var $options = array(
  104.         'image'   => null,
  105.         'width'   => 100,
  106.         'height'  => 100,
  107.         'percent' => 0,
  108.         'method'  => IMAGE_TOOLS_THUMBNAIL_METHOD_SCALE_MAX,
  109.         'halign'  => IMAGE_TOOLS_THUMBNAIL_ALIGN_CENTER,
  110.         'valign'  => IMAGE_TOOLS_THUMBNAIL_ALIGN_CENTER,
  111.     );
  112.  
  113.     /**
  114.      * Available options for Image_Tools_Thumbnail
  115.      *
  116.      * @var     array 
  117.      * @access  protected
  118.      */
  119.     var $availableOptions = array(
  120.         'image'   => 'mixed',
  121.         'width'   => 'int',
  122.         'height'  => 'int',
  123.         'percent' => 'number',
  124.         'method'  => 'int',
  125.         'halign'  => 'int',
  126.         'valign'  => 'int',
  127.     );
  128.  
  129.     /**
  130.      * Image_Tools_Thumbnail API version.
  131.      *
  132.      * @var     string 
  133.      * @access  protected
  134.      */
  135.     var $version = '0.1';
  136.  
  137.     /**
  138.      * Image info.
  139.      *
  140.      * @var     resource 
  141.      * @access  protected
  142.      */
  143.     var $imageInfo = null;
  144.  
  145.     // }}}
  146.     // {{{ render()
  147.  
  148.     
  149.  
  150.     /**
  151.      * Draw thumbnail result to resource.
  152.      *
  153.      * @return  bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
  154.      * @access  public
  155.      */
  156.     function render()
  157.     {
  158.         // Create the source image
  159.         $source Image_Tools::createImage($this->options['image']);
  160.         if PEAR::isError($source) ) {
  161.             return $source;
  162.         }
  163.         if Image_Tools::isGDImageResource($source) ) {
  164.             return PEAR::raiseError('Invalid image resource');
  165.         }
  166.         $this->_init($source);
  167.  
  168.         // Estimate a rectangular portion of the source image and a size of the target image
  169.         if $this->options['method'== IMAGE_TOOLS_THUMBNAIL_METHOD_CROP {
  170.             if $this->options['percent'{
  171.                 $W floor($this->options['percent'$this->imageInfo['width']);
  172.                 $H floor($this->options['percent'$this->imageInfo['height']);
  173.             else {
  174.                 $W $this->options['width'];
  175.                 $H $this->options['height'];
  176.             }
  177.  
  178.             $width $W;
  179.             $height $H;
  180.  
  181.             $Y $this->_coord('valign''height'$H);
  182.             $X $this->_coord('halign''width'$W);
  183.         else {
  184.             $W $this->imageInfo['width'];
  185.             $H $this->imageInfo['height'];
  186.  
  187.             if $this->options['percent'{
  188.                 $width floor($this->options['percent'$W);
  189.                 $height floor($this->options['percent'$H);
  190.             else {
  191.                 $width $this->options['width'];
  192.                 $height $this->options['height'];
  193.  
  194.                 if $this->options['method'== IMAGE_TOOLS_THUMBNAIL_METHOD_SCALE_MIN {
  195.                     $Ww $W $width;
  196.                     $Hh $H $height;
  197.                     if $Ww $Hh {
  198.                         $W floor($width $Hh);
  199.                         $X $this->_coord('halign''width'$W);
  200.                     else {
  201.                         $H floor($height $Ww);
  202.                         $Y $this->_coord('valign''height'$H);
  203.                     }
  204.                 else {
  205.                     if $H $W {
  206.                         $width floor($height $H $W);
  207.                     else {
  208.                         $height floor($width $W $H);
  209.                     }
  210.                 }
  211.             }
  212.  
  213.             $X = 0;
  214.             $Y = 0;
  215.         }
  216.  
  217.         // Create the target image
  218.         if function_exists('imagecreatetruecolor') ) {
  219.             $target imagecreatetruecolor($width$height);
  220.         else {
  221.             $target imagecreate($width$height);
  222.         }
  223.         if Image_Tools::isGDImageResource($target) ) {
  224.             return PEAR::raiseError('Cannot initialize new GD image stream');
  225.         }
  226.  
  227.         // Copy the source image to the target image
  228.         if $this->options['method'== IMAGE_TOOLS_THUMBNAIL_METHOD_CROP {
  229.             $result imagecopy($target$this->resultImage00$X$Y$W$H);
  230.         elseif function_exists('imagecopyresampled') ) {
  231.             $result imagecopyresampled($target$this->resultImage00$X$Y$width$height$W$H);
  232.         else {
  233.             $result imagecopyresized($target$this->resultImage00$X$Y$width$height$W$H);
  234.         }
  235.         if $result {
  236.             return PEAR::raiseError('Cannot resize image');
  237.         }
  238.  
  239.         // Free a memory from the source image and save the resulting thumbnail
  240.         imagedestroy($this->resultImage);
  241.         $this->_init($target);
  242.  
  243.         return true;
  244.     }
  245.  
  246.     // }}}
  247.     // {{{ _init()
  248.  
  249.     
  250.  
  251.     function _init($res)
  252.     {
  253.         $this->resultImage = $res;
  254.         $this->imageInfo = array(
  255.             'width'  => imagesx($this->resultImage),
  256.             'height' => imagesy($this->resultImage),
  257.         );
  258.     }
  259.  
  260.     // }}}
  261.     // {{{ _coord()
  262.  
  263.     
  264.  
  265.     function _coord($align$param$src)
  266.     {
  267.         if $this->options[$alignIMAGE_TOOLS_THUMBNAIL_ALIGN_CENTER {
  268.             $result = 0;
  269.         elseif $this->options[$alignIMAGE_TOOLS_THUMBNAIL_ALIGN_CENTER {
  270.             $result $this->imageInfo[$param$src;
  271.         else {
  272.             $result ($this->imageInfo[$param$src>> 1;
  273.         }
  274.         return $result;
  275.     }
  276.  
  277.     // }}}
  278.  
  279.  
  280.  
  281. }
  282.  
  283. // }}}
  284. /*
  285.  * Local variables:
  286.  * mode: php
  287.  * tab-width: 4
  288.  * c-basic-offset: 4
  289.  * c-hanging-comment-ender-p: nil
  290.  * End:
  291.  */
  292. ?>

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