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

Source for file IM.php

Documentation is available at IM.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  4.  
  5. /**
  6.  * ImageMagick binaries 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.  * @author     Peter Bowyer <peter@mapledesign.co.uk>
  19.  * @author     Philippe Jausions <Philippe.Jausions@11abacus.com>
  20.  * @copyright  2002-2005 The PHP Group
  21.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  22.  * @version    CVS: $Id: IM.php,v 1.25 2007/04/19 16:36:09 dufuz Exp $
  23.  * @link       http://pear.php.net/package/Image_Transform
  24.  */
  25.  
  26. require_once 'Image/Transform.php';
  27. require_once 'System.php';
  28.  
  29. /**
  30.  * ImageMagick binaries implementation for Image_Transform package
  31.  *
  32.  * @category   Image
  33.  * @package    Image_Transform
  34.  * @subpackage Image_Transform_Driver_IM
  35.  * @author     Peter Bowyer <peter@mapledesign.co.uk>
  36.  * @author     Philippe Jausions <Philippe.Jausions@11abacus.com>
  37.  * @copyright  2002-2005 The PHP Group
  38.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  39.  * @version    Release: @package_version@
  40.  * @link       http://pear.php.net/package/Image_Transform
  41.  * @link http://www.imagemagick.org/
  42.  ***/
  43. {
  44.     /**
  45.      * associative array commands to be executed
  46.      * @var array 
  47.      * @access private
  48.      */
  49.     var $command;
  50.  
  51.     /**
  52.      * Class constructor
  53.      */
  54.     function Image_Transform_Driver_IM()
  55.     {
  56.         $this->__construct();
  57.     // End Image_IM
  58.  
  59.  
  60.     
  61.     /**
  62.      * Class constructor
  63.      */
  64.     function __construct()
  65.     {
  66.         $this->_init();
  67.         if (!defined('IMAGE_TRANSFORM_IM_PATH')) {
  68.             $path dirname(System::which('convert'))
  69.                     . DIRECTORY_SEPARATOR;
  70.             define('IMAGE_TRANSFORM_IM_PATH'$path);
  71.         }
  72.         if (System::which(IMAGE_TRANSFORM_IM_PATH . 'convert' ((OS_WINDOWS'.exe' ''))) {
  73.             include 'Image/Transform/Driver/Imagick/ImageTypes.php';
  74.         else {
  75.             $this->isError(PEAR::raiseError('Couldn\'t find "convert" binary',
  76.                 IMAGE_TRANSFORM_ERROR_UNSUPPORTED));
  77.         }
  78.     // End Image_IM
  79.  
  80.     
  81.     /**
  82.      * Initialize the state of the object
  83.      **/
  84.  
  85.     function _init()
  86.     {
  87.         $this->command = array();
  88.     }
  89.  
  90.     /**
  91.      * Load an image.
  92.      *
  93.      * This method doesn't support remote files.
  94.      *
  95.      * @param string filename
  96.      *
  97.      * @return mixed TRUE or a PEAR error object on error
  98.      * @see PEAR::isError()
  99.      */
  100.     function load($image)
  101.     {
  102.         $this->_init();
  103.         if (!file_exists($image)) {
  104.             return PEAR::raiseError('The image file ' $image
  105.                 . ' doesn\'t exist'IMAGE_TRANSFORM_ERROR_IO);
  106.         }
  107.         $this->image = $image;
  108.         $result $this->_get_image_details($image);
  109.         if (PEAR::isError($result)) {
  110.             return $result;
  111.         }
  112.         return true;
  113.  
  114.     // End load
  115.  
  116.     
  117.     /**
  118.      * Image_Transform_Driver_IM::_get_image_details()
  119.      *
  120.      * @param string $image the path and name of the image file
  121.      * @return none 
  122.      */
  123.     function _get_image_details($image)
  124.     {
  125.         $retval Image_Transform::_get_image_details($image);
  126.         if (PEAR::isError($retval)) {
  127.             unset($retval);
  128.  
  129.             if (!System::which(IMAGE_TRANSFORM_IM_PATH . 'identify' ((OS_WINDOWS'.exe' ''))) {
  130.                 $this->isError(PEAR::raiseError('Couldn\'t find "identify" binary'IMAGE_TRANSFORM_ERROR_UNSUPPORTED));
  131.             }
  132.             $cmd $this->_prepare_cmd(IMAGE_TRANSFORM_IM_PATH,
  133.                 'identify',
  134.                 '-format %w:%h:%m ' escapeshellarg($image));
  135.             exec($cmd$res$exit);
  136.  
  137.             if ($exit == 0{
  138.                 $data  explode(':'$res[0]);
  139.                 $this->img_x = $data[0];
  140.                 $this->img_y = $data[1];
  141.                 $this->type  = strtolower($data[2]);
  142.                 $retval = true;
  143.             else {
  144.                 return PEAR::raiseError("Cannot fetch image or images details."true);
  145.             }
  146.  
  147.         }
  148.  
  149.         return $retval;
  150.     }
  151.  
  152.     /**
  153.      * Resize the image.
  154.      *
  155.      * @access private
  156.      *
  157.      * @param int   $new_x   New width
  158.      * @param int   $new_y   New height
  159.      * @param mixed $options Optional parameters
  160.      *
  161.      * @return true on success or PEAR Error object on error
  162.      * @see PEAR::isError()
  163.      */
  164.     function _resize($new_x$new_y$options = null)
  165.     {
  166.         if (isset($this->command['resize'])) {
  167.             return PEAR::raiseError('You cannot scale or resize an image more than once without calling save() or display()'true);
  168.         }
  169.         $this->command['resize''-geometry '
  170.             . ((int) $new_x'x' ((int) $new_y'!';
  171.  
  172.         $this->new_x = $new_x;
  173.         $this->new_y = $new_y;
  174.  
  175.         return true;
  176.     // End resize
  177.  
  178.     
  179.     /**
  180.      * rotate
  181.      *
  182.      * @param   int     angle   rotation angle
  183.      * @param   array   options no option allowed
  184.      * @return mixed TRUE or a PEAR error object on error
  185.      */
  186.     function rotate($angle$options = null)
  187.     {
  188.         $angle $this->_rotation_angle($angle);
  189.         if ($angle % 360{
  190.             $this->command['rotate''-rotate ' . (float) $angle;
  191.         }
  192.         return true;
  193.  
  194.     // End rotate
  195.  
  196.     
  197.     /**
  198.      * Crop image
  199.      *
  200.      * @author Ian Eure <ieure@websprockets.com>
  201.      * @since 0.8
  202.      *
  203.      * @param int width Cropped image width
  204.      * @param int height Cropped image height
  205.      * @param int x X-coordinate to crop at
  206.      * @param int y Y-coordinate to crop at
  207.      *
  208.      * @return mixed TRUE or a PEAR error object on error
  209.      */
  210.     function crop($width$height$x = 0$y = 0{
  211.         // Do we want a safety check - i.e. if $width+$x > $this->img_x then we
  212.         // raise a warning? [and obviously same for $height+$y]
  213.         $this->command['crop''-crop '
  214.             . ((int) $width)  'x' ((int) $height)
  215.             . '+' ((int) $x'+' ((int) $y);
  216.  
  217.         // I think that setting img_x/y is wrong, but scaleByLength() & friends
  218.         // mess up the aspect after a crop otherwise.
  219.         $this->new_x = $this->img_x = $width $x;
  220.         $this->new_y = $this->img_y = $height $y;
  221.  
  222.         return true;
  223.     }
  224.  
  225.     /**
  226.      * addText
  227.      *
  228.      * @param   array   options     Array contains options
  229.      *                               array(
  230.      *                                   'text'  The string to draw
  231.      *                                   'x'     Horizontal position
  232.      *                                   'y'     Vertical Position
  233.      *                                   'Color' Font color
  234.      *                                   'font'  Font to be used
  235.      *                                   'size'  Size of the fonts in pixel
  236.      *                                   'resize_first'  Tell if the image has to be resized
  237.      *                                                   before drawing the text
  238.      *                               )
  239.      *
  240.      * @return mixed TRUE or a PEAR error object on error
  241.      * @see PEAR::isError()
  242.      */
  243.     function addText($params)
  244.     {
  245.          $params array_merge($this->_get_default_text_params()$params);
  246.          extract($params);
  247.          if (true === $resize_first{
  248.              // Set the key so that this will be the last item in the array
  249.             $key 'ztext';
  250.          else {
  251.             $key 'text';
  252.          }
  253.          $this->command[$key'-font ' escapeshellarg($font)
  254.             . ' -fill ' escapeshellarg($color)
  255.             . ' -draw \'text ' escapeshellarg($x ',' $y)
  256.             . ' "' escapeshellarg($text'"\'';
  257.          // Producing error: gs: not found gs: not found convert: Postscript delegate failed [No such file or directory].
  258.         return true;
  259.  
  260.     // End addText
  261.  
  262.     
  263.     /**
  264.      * Adjust the image gamma
  265.      *
  266.      * @access public
  267.      * @param float $outputgamma 
  268.      * @return mixed TRUE or a PEAR error object on error
  269.      */
  270.     function gamma($outputgamma = 1.0{
  271.         if ($outputgamme != 1.0{
  272.             $this->command['gamma''-gamma ' . (float) $outputgamma;
  273.         }
  274.         return true;
  275.     }
  276.  
  277.     /**
  278.      * Convert the image to greyscale
  279.      *
  280.      * @access public
  281.      * @return mixed TRUE or a PEAR error object on error
  282.      */
  283.     function greyscale({
  284.         $this->command['type''-type Grayscale';
  285.         return true;
  286.     }
  287.  
  288.     /**
  289.      * Horizontal mirroring
  290.      *
  291.      * @access public
  292.      * @return TRUE or PEAR Error object on error
  293.      */
  294.     function mirror({
  295.         // We can only apply "flop" once
  296.         if (isset($this->command['flop'])) {
  297.             unset($this->command['flop']);
  298.         else {
  299.             $this->command['flop''-flop';
  300.         }
  301.         return true;
  302.     }
  303.  
  304.     /**
  305.      * Vertical mirroring
  306.      *
  307.      * @access public
  308.      * @return TRUE or PEAR Error object on error
  309.      */
  310.     function flip({
  311.         // We can only apply "flip" once
  312.         if (isset($this->command['flip'])) {
  313.             unset($this->command['flip']);
  314.         else {
  315.             $this->command['flip''-flip';
  316.         }
  317.         return true;
  318.     }
  319.  
  320.     /**
  321.      * Save the image file
  322.      *
  323.      * @access public
  324.      *
  325.      * @param $filename string  the name of the file to write to
  326.      * @param $quality  quality image dpi, default=75
  327.      * @param $type     string  (JPEG, PNG...)
  328.      *
  329.      * @return mixed TRUE or a PEAR error object on error
  330.      */
  331.     function save($filename$type ''$quality = null)
  332.     {
  333.         $type strtoupper(($type == ''$this->type : $type);
  334.         switch ($type{
  335.             case 'JPEG':
  336.                 $type 'JPG';
  337.                 break;
  338.         }
  339.         $options = array();
  340.         if (!is_null($quality)) {
  341.             $options['quality'$quality;
  342.         }
  343.         $quality $this->_getOption('quality'$options75);
  344.  
  345.         $cmd $this->_prepare_cmd(
  346.             IMAGE_TRANSFORM_IM_PATH,
  347.             'convert',
  348.             implode(' '$this->command)
  349.                . ' -quality ' ((int) $quality' '
  350.                . escapeshellarg($this->image' ' $type ':'
  351.                . escapeshellarg($filename' 2>&1');
  352.         exec($cmd$res$exit);
  353.  
  354.         return ($exit == 0? true : PEAR::raiseError(implode('. '$res),
  355.             IMAGE_TRANSFORM_ERROR_IO);
  356.     // End save
  357.  
  358.     
  359.     /**
  360.      * Display image without saving and lose changes
  361.      *
  362.      * This method adds the Content-type HTTP header
  363.      *
  364.      * @access public
  365.      *
  366.      * @param string type (JPEG,PNG...);
  367.      * @param int quality 75
  368.      *
  369.      * @return mixed TRUE or a PEAR error object on error
  370.      */
  371.     function display($type ''$quality = null)
  372.     {
  373.         $type    strtoupper(($type == ''$this->type : $type);
  374.         switch ($type{
  375.             case 'JPEG':
  376.                 $type 'JPG';
  377.                 break;
  378.         }
  379.         $options = array();
  380.         if (!is_null($quality)) {
  381.             $options['quality'$quality;
  382.         }
  383.         $quality $this->_getOption('quality'$options75);
  384.  
  385.         $this->_send_display_headers($type);
  386.  
  387.         $cmd $this->_prepare_cmd(
  388.             IMAGE_TRANSFORM_IM_PATH,
  389.             'convert',
  390.             implode(' '$this->command. " -quality $quality "  .
  391.                    $this->image . ' ' $type ":-");
  392.         passthru($cmd);
  393.  
  394.         if (!$this->keep_settings_on_save{
  395.             $this->free();
  396.         }
  397.         return true;
  398.     }
  399.  
  400.     /**
  401.      * Destroy image handle
  402.      *
  403.      * @return void 
  404.      */
  405.     function free()
  406.     {
  407.         $this->command = array();
  408.         $this->image = '';
  409.         $this->type = '';
  410.     }
  411.  

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