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

Source for file Imagick2.php

Documentation is available at Imagick2.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  4.  
  5. /**
  6.  * imagick PECL extension implementation for Image_Transform package
  7.  *
  8.  * PHP versions 4 and 5
  9.  *
  10.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  11.  * that is available through the world-wide-web at the following URI:
  12.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  13.  * the PHP License and are unable to obtain it through the web, please
  14.  * send a note to license@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category   Image
  17.  * @package    Image_Transform
  18.  * @subpackage Image_Transform_Driver_Imagick2
  19.  * @author     Alan Knowles <alan@akbkhome.com>
  20.  * @author     Peter Bowyer <peter@mapledesign.co.uk>
  21.  * @author     Philippe Jausions <Philippe.Jausions@11abacus.com>
  22.  * @copyright  2002-2005 The PHP Group
  23.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  24.  * @version    CVS: $Id: Imagick2.php,v 1.10 2007/04/19 16:36:09 dufuz Exp $
  25.  * @link       http://pear.php.net/package/Image_Transform
  26.  */
  27.  
  28. require_once 'Image/Transform.php';
  29.  
  30. /**
  31.  * imagick PECL extension implementation for Image_Transform package
  32.  *
  33.  * EXPERIMENTAL - please report bugs
  34.  * Use the latest cvs version of imagick PECL
  35.  *
  36.  * @category   Image
  37.  * @package    Image_Transform
  38.  * @subpackage Image_Transform_Driver_Imagick2
  39.  * @author     Alan Knowles <alan@akbkhome.com>
  40.  * @author     Peter Bowyer <peter@mapledesign.co.uk>
  41.  * @author     Philippe Jausions <Philippe.Jausions@11abacus.com>
  42.  * @copyright  2002-2005 The PHP Group
  43.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  44.  * @version    Release: @package_version@
  45.  * @link       http://pear.php.net/package/Image_Transform
  46.  * @since      PHP 4.0
  47.  */
  48. {
  49.     /**
  50.      * Handler of the imagick image ressource
  51.      * @var array 
  52.      */
  53.     var $imageHandle = null;
  54.  
  55.     /**
  56.      * @see __construct()
  57.      */
  58.     {
  59.         $this->__construct();
  60.     // End Image_Transform_Driver_Imagick2
  61.  
  62.     
  63.     /**
  64.      * @see http://www.imagemagick.org/www/formats.html
  65.      */
  66.     function __construct()
  67.     {
  68.         if (PEAR::loadExtension('imagick')) {
  69.             include 'Image/Transform/Driver/Imagick/ImageTypes.php';
  70.         else {
  71.             $this->isError(PEAR::raiseError('Couldn\'t find the imagick extension.',
  72.                 IMAGE_TRANSFORM_ERROR_UNSUPPORTED));
  73.         }
  74.     }
  75.  
  76.     /**
  77.      * Loads an image
  78.      *
  79.      * @param string $image filename
  80.      * @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
  81.      * @access public
  82.      */
  83.     function load($image)
  84.     {
  85.         if (!($this->imageHandle = imagick_readimage($image))) {
  86.             $this->free();
  87.             return $this->raiseError('Couldn\'t load image.',
  88.                 IMAGE_TRANSFORM_ERROR_IO);
  89.         }
  90.         if (imagick_iserror($this->imageHandle)) {
  91.             return $this->raiseError('Couldn\'t load image.',
  92.                 IMAGE_TRANSFORM_ERROR_IO);
  93.         }
  94.  
  95.         $this->image = $image;
  96.         $result $this->_get_image_details($image);
  97.         if (PEAR::isError($result)) {
  98.             return $result;
  99.         }
  100.  
  101.         return true;
  102.     // End load
  103.  
  104.     
  105.     /**
  106.      * Resize Action
  107.      *
  108.      * @param int   $new_x   New width
  109.      * @param int   $new_y   New height
  110.      * @param mixed $options Optional parameters
  111.      *
  112.      * @return bool|PEAR_ErrorTRUE or PEAR_Error object on error
  113.      * @access protected
  114.      */
  115.     function _resize($new_x$new_y$options = null)
  116.     {
  117.         if (!imagick_resize($this->imageHandle$new_x$new_yIMAGICK_FILTER_UNKNOWN 1)) {
  118.             return $this->raiseError('Couldn\'t resize image.',
  119.                 IMAGE_TRANSFORM_ERROR_FAILED);
  120.         }
  121.  
  122.         $this->new_x = $new_x;
  123.         $this->new_y = $new_y;
  124.         return true;
  125.  
  126.     // End resize
  127.  
  128.     
  129.     /**
  130.      * Rotates the current image
  131.      * Note: color mask are currently not supported
  132.      *
  133.      * @param   int     Rotation angle in degree
  134.      * @param   array   No options are currently supported
  135.      *
  136.      * @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
  137.      * @access public
  138.      */
  139.     function rotate($angle$options = null)
  140.     {
  141.         if (($angle % 360== 0{
  142.             return true;
  143.         }
  144.         if (!imagick_rotate($this->imageHandle$angle)) {
  145.             return $this->raiseError('Cannot create a new imagick image for the rotation.',
  146.                 IMAGE_TRANSFORM_ERROR_FAILED);
  147.         }
  148.  
  149.         $this->new_x = imagick_getwidth($this->imageHandle);
  150.         $this->new_y = imagick_getheight($this->imageHandle);
  151.         return true;
  152.  
  153.     // End rotate
  154.  
  155.     
  156.     /**
  157.      * addText
  158.      *
  159.      * @param   array   options     Array contains options
  160.      *                               array(
  161.      *                                   'text'  The string to draw
  162.      *                                   'x'     Horizontal position
  163.      *                                   'y'     Vertical Position
  164.      *                                   'Color' Font color
  165.      *                                   'font'  Font to be used
  166.      *                                   'size'  Size of the fonts in pixel
  167.      *                                   'resize_first'  Tell if the image has to be resized
  168.      *                                                   before drawing the text
  169.      *                               )
  170.      *
  171.      * @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
  172.      * @access public
  173.      */
  174.     function addText($params)
  175.     {
  176.         static $default_params = array(
  177.                                 'text'          => 'This is a Text',
  178.                                 'x'             => 10,
  179.                                 'y'             => 20,
  180.                                 'size'          => 12,
  181.                                 'color'         => 'red',
  182.                                 'font'          => 'Helvetica',
  183.                                 'resize_first'  => false // Carry out the scaling of the image before annotation?
  184.                                                                 );
  185.         $params = array_merge($default_params$params);
  186.  
  187.  
  188.         $params['color']is_array($params['color'])?$this->colorarray2colorhex($params['color']):strtolower($params['color']);
  189.  
  190.  
  191.         static $cmds = array(
  192.             'setfillcolor' => 'color',
  193.             'setfontsize'  => 'size',
  194.             'setfontface'  => 'font'
  195.         );
  196.         imagick_begindraw($this->imageHandle ;
  197.  
  198.         foreach ($cmds as $cmd => $v{
  199.             if (!call_user_func('imagick_' $cmd$this->imageHandle$parms[$v])) {
  200.                 return $this->raiseError("Problem with adding Text::{$v} = {$parms[$v]}",
  201.                     IMAGE_TRANSFORM_ERROR_FAILED);
  202.             }
  203.         }
  204.         if (!imagick_drawannotation($this->imageHandle$params['x']$params['y']$params['text'])) {
  205.             return $this->raiseError('Problem with adding Text',
  206.                 IMAGE_TRANSFORM_ERROR_FAILED);
  207.         }
  208.  
  209.         return true;
  210.  
  211.     // End addText
  212.  
  213.  
  214.     
  215.     /**
  216.      * Saves the image to a file
  217.      *
  218.      * @param $filename string the name of the file to write to
  219.      * @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
  220.      * @access public
  221.      */
  222.     function save($filename$type ''$quality = null)
  223.     {
  224.         $options (is_array($quality)) $quality : array();
  225.         if (is_numeric($quality)) {
  226.             $options['quality'$quality;
  227.         }
  228.         $quality $this->_getOption('quality'$options75);
  229.         imagick_setcompressionquality($this->imageHandle$quality);
  230.  
  231.         if ($type && strcasecmp($type$this->type)
  232.             && !imagick_convert($this->imageHandle$type)) {
  233.             return $this->raiseError('Couldn\'t save image to file (conversion failed).',
  234.                 IMAGE_TRANSFORM_ERROR_FAILED);
  235.         }
  236.  
  237.         if (!imagick_write($this->imageHandle$filename)) {
  238.             return $this->raiseError('Couldn\'t save image to file.',
  239.                 IMAGE_TRANSFORM_ERROR_IO);
  240.         }
  241.         $this->free();
  242.         return true;
  243.  
  244.     // End save
  245.  
  246.     
  247.     /**
  248.      * Displays image without saving and lose changes
  249.      *
  250.      * This method adds the Content-type HTTP header
  251.      *
  252.      * @param string type (JPG,PNG...);
  253.      * @param int quality 75
  254.      *
  255.      * @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
  256.      * @access public
  257.      */
  258.     function display($type ''$quality = null)
  259.     {
  260.         $options (is_array($quality)) $quality : array();
  261.         if (is_numeric($quality)) {
  262.             $options['quality'$quality;
  263.         }
  264.         $quality $this->_getOption('quality'$options75);
  265.         imagick_setcompressionquality($this->imageHandle$quality);
  266.  
  267.         if ($type && strcasecomp($type$this->type)
  268.             && !imagick_convert($this->imageHandle$type)) {
  269.             return $this->raiseError('Couldn\'t save image to file (conversion failed).',
  270.                 IMAGE_TRANSFORM_ERROR_FAILED);
  271.         }
  272.         if (!($image = imagick_image2blob($this->imageHandle))) {
  273.             return $this->raiseError('Couldn\'t display image.',
  274.                 IMAGE_TRANSFORM_ERROR_IO);
  275.         }
  276.         header('Content-type: ' . imagick_getmimetype($this->imageHandle));
  277.         echo $image;
  278.         $this->free();
  279.         return true;
  280.     }
  281.  
  282.     /**
  283.      * Adjusts the image gamma
  284.      *
  285.      * @param float $outputgamma 
  286.      * @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
  287.      * @access public
  288.      */
  289.     function gamma($outputgamma = 1.0{
  290.         if ($outputgamma != 1.0{
  291.             imagick_gamma($this->imageHandle$outputgamma);
  292.         }
  293.         return true;
  294.     }
  295.  
  296.     /**
  297.      * Crops the image
  298.      *
  299.      * @param int width Cropped image width
  300.      * @param int height Cropped image height
  301.      * @param int x X-coordinate to crop at
  302.      * @param int y Y-coordinate to crop at
  303.      *
  304.      * @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
  305.      * @access public
  306.      */
  307.     function crop($width$height$x = 0$y = 0)
  308.     {
  309.         // Sanity check
  310.         if (!$this->intersects($width$height$x$y)) {
  311.             return PEAR::raiseError('Nothing to crop'IMAGE_TRANSFORM_ERROR_OUTOFBOUND);
  312.         }
  313.         if (!imagick_crop($this->imageHandle$x$y$width$height)) {
  314.             return $this->raiseError('Couldn\'t crop image.',
  315.                 IMAGE_TRANSFORM_ERROR_FAILED);
  316.         }
  317.  
  318.         // I think that setting img_x/y is wrong, but scaleByLength() & friends
  319.         // mess up the aspect after a crop otherwise.
  320.         $this->new_x = $width;
  321.         $this->new_y = $height;
  322.  
  323.         return true;
  324.     }
  325.  
  326.     /**
  327.      * Horizontal mirroring
  328.      *
  329.      * @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
  330.      * @access public
  331.      */
  332.     function mirror()
  333.     {
  334.         if (!imagick_flop($this->imageHandle)) {
  335.             return $this->raiseError('Couldn\'t mirror the image.',
  336.                 IMAGE_TRANSFORM_ERROR_FAILED);
  337.         }
  338.         return true;
  339.     }
  340.  
  341.     /**
  342.      * Vertical mirroring
  343.      *
  344.      * @return bool|PEAR_ErrorTRUE or a PEAR_Error object on error
  345.      * @access public
  346.      */
  347.     function flip()
  348.     {
  349.         if (!imagick_flip($this->imageHandle)) {
  350.             return $this->raiseError('Couldn\'t flip the image.',
  351.                 IMAGE_TRANSFORM_ERROR_FAILED);
  352.         }
  353.         return true;
  354.     }
  355.  
  356.     /**
  357.      * Destroy image handle
  358.      *
  359.      * @access public
  360.      */
  361.     function free()
  362.     {
  363.         if (is_resource($this->imageHandle)) {
  364.             imagick_destroyhandle($this->imageHandle);
  365.         }
  366.         $this->imageHandle = null;
  367.     }
  368.  
  369.     /**
  370.      * RaiseError Method - shows imagick Raw errors.
  371.      *
  372.      * @param string $message message = prefixed message..
  373.      * @param int    $code error code
  374.      * @return PEAR error object
  375.      * @access protected
  376.      */
  377.     function raiseError($message$code = 0)
  378.     {
  379.         if (is_resource($this->imageHandle)) {
  380.             $message .= "\nReason: "
  381.                         .  imagick_failedreason($this->imageHandle)
  382.                         . "\nDescription: "
  383.                         . imagick_faileddescription($this->imageHandle);
  384.         }
  385.         return PEAR::raiseError($message$code);
  386.     }
  387.  

Documentation generated on Thu, 19 Apr 2007 13:30:15 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.