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

Source for file Imlib.php

Documentation is available at Imlib.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.02 of the PHP license,      |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Jason Rust <jrust@rustyparts.com>                           |
  16. // +----------------------------------------------------------------------+
  17. // $Id: Imlib.php,v 1.6 2007/04/19 16:36:09 dufuz Exp $
  18. // {{{ requires
  19.  
  20. require_once 'Image/Transform.php';
  21.  
  22. // }}}
  23. // {{{ example usage
  24.  
  25. //    $img    = new Image_Transform::factory('Imlib');
  26. //    $angle  = -90;
  27. //    $img->load('test.png');
  28. //    $img->rotate($angle);
  29. //    $img->addText(array('text'=>"Rotation $angle",'x'=>0,'y'=>100,'font'=>'arial.ttf','color'=>'#ffffff'));
  30. //    $img->display();
  31.  
  32. // }}}
  33. // {{{ class Image_Transform_Driver_Imlib
  34.  
  35. /**
  36.  * Performs image manipulation with the imlib library.
  37.  *
  38.  * @see http://mmcc.cx/php_imlib/index.php
  39.  * @version Revision: 1.0
  40.  * @author  Jason Rust <jrust@rustyparts.com>
  41.  * @package Image_Transform
  42.  */
  43.  
  44. // }}}
  45.     // {{{ properties
  46.  
  47.     
  48.     /**
  49.      * Holds the image file for manipulation
  50.      */
  51.     var $imageHandle = '';
  52.  
  53.     /**
  54.      * Holds the original image file
  55.      */
  56.     var $oldHandle = '';
  57.  
  58.     // }}}
  59.     // {{{ constructor
  60.  
  61.     
  62.     /**
  63.      * Check settings
  64.      *
  65.      * @see __construct()
  66.      */
  67.     function Image_Transform_Imlib()
  68.     {
  69.         $this->__construct();
  70.     }
  71.  
  72.     /**
  73.      * Check settings
  74.      *
  75.      * @return mixed true or  or a PEAR error object on error
  76.      *
  77.      * @see PEAR::isError()
  78.      */
  79.     function __construct()
  80.     {
  81.         if (!PEAR::loadExtension('imlib')) {
  82.             $this->isError(PEAR::raiseError('Couldn\'t find the imlib extension.'true));
  83.         }
  84.     }
  85.  
  86.     // }}}
  87.     // {{{ load()
  88.  
  89.     
  90.     /**
  91.      * Load image
  92.      *
  93.      * @param string filename
  94.      *
  95.      * @return mixed TRUE or a PEAR error object on error
  96.      * @see PEAR::isError()
  97.      */
  98.     function load($image)
  99.     {
  100.         $this->image = $image;
  101.         $this->imageHandle = imlib_load_image($this->image);
  102.         $result =$this->_get_image_details($image);
  103.         if (PEAR::isError($result)) {
  104.             return $result;
  105.         }
  106.  
  107.         return true;
  108.     }
  109.  
  110.     // }}}
  111.     // {{{ addText()
  112.  
  113.     
  114.     /**
  115.      * Adds text to the image.  Note that the angle should be one of the following
  116.      * constants:  IMLIB_TEXT_TO_RIGHT, IMLIB_TEXT_TO_LEFT, IMLIB_TEXT_TO_DOWN,
  117.      * IMLIB_TEXT_TO_UP, IMLIB_TEXT_TO_ANGLE
  118.      *
  119.      * @param   array   options     Array contains options
  120.      *                               array(
  121.      *                                   'text'  The string to draw
  122.      *                                   'x'     Horizontal position
  123.      *                                   'y'     Vertical Position
  124.      *                                   'color' Font color
  125.      *                                   'font'  Font to be used
  126.      *                                   'size'  Size of the fonts in pixel
  127.      *                                   'angle' A imlib direction constant
  128.      *                               )
  129.      *
  130.      * @return TRUE or PEAR Error object on error
  131.      * @see PEAR::isError()
  132.      */
  133.     function addText($params)
  134.     {
  135.         $default_params = array(
  136.                                 'text' => 'This is Text',
  137.                                 'x' => 10,
  138.                                 'y' => 20,
  139.                                 'color' => array(255,0,0),
  140.                                 'font' => 'Arial.ttf',
  141.                                 'size' => '12',
  142.                                 'angle' => IMLIB_TEXT_TO_RIGHT,
  143.                                 );
  144.         $params array_merge($default_params$params);
  145.         extract($params);
  146.  
  147.         if (!is_array($color)){
  148.             if ($color[0== '#'){
  149.                 $color $this->colorhex2colorarray($color);
  150.             else {
  151.                 include_once('Image/Transform/Driver/ColorsDefs.php');
  152.                 $color = isset($colornames[$color]$colornames[$color: false;
  153.             }
  154.         }
  155.  
  156.         $fontResource = imlib_load_font($font '/' $size);
  157.         imlib_text_draw($this->imageHandle$fontResource$x$y$text$angle$color[0]$color[1]$color[2]255);
  158.         return true;
  159.     }
  160.  
  161.     // }}}
  162.     // {{{ rotate()
  163.  
  164.     
  165.     /**
  166.      * Rotate image by the given angle
  167.      *
  168.      * @param int       $angle      Rotation angle
  169.      *
  170.      * @return TRUE or PEAR Error object on error
  171.      */
  172.     function rotate($angle)
  173.     {
  174.         $this->oldHandle = $this->imageHandle;
  175.         $this->imageHandle = imlib_create_rotated_image($this->imageHandle$angle);
  176.         $new_x = imlib_image_get_width($this->imageHandle);
  177.         $new_y = imlib_image_get_height($this->imageHandle);
  178.         // when rotating it creates a bigger picture than before so that it can rotate at any angle
  179.         // so for right angles we crop it back to the original size
  180.         if ($angle % 90 == 0{
  181.             if (abs($angle== 90 || $angle == 270{
  182.                 $y_pos ($new_x $this->img_x/ 2;
  183.                 $x_pos ($new_y $this->img_y/ 2;
  184.                 $y_pos++;
  185.                 $x_pos++;
  186.                 $this->crop($this->img_y$this->img_x$x_pos$y_pos);
  187.             }
  188.             else {
  189.                 $x_pos ($new_x $this->img_x/ 2;
  190.                 $y_pos ($new_y $this->img_y/ 2;
  191.                 $this->crop($this->img_x$this->img_y$x_pos$y_pos);
  192.             }
  193.         }
  194.         else {
  195.             $this->img_x = $new_x;
  196.             $this->img_y = $new_y;
  197.         }
  198.  
  199.         return true;
  200.     }
  201.  
  202.     // }}}
  203.     // {{{ crop()
  204.  
  205.     
  206.     /**
  207.      * Crops the current image to a specified height and width
  208.      *
  209.      * @param int $in_cropWidth The width of the new image
  210.      * @param int $in_cropHeight The height of the new image
  211.      * @param int $in_cropX The X coordinate on the image to start the crop
  212.      * @param int $in_cropY The Y coordinate on the image to start the crop
  213.      *
  214.      * @access public
  215.      * @return TRUE or PEAR Error object on error
  216.      */
  217.     function crop($in_cropWidth$in_cropHeight$in_cropX$in_cropY)
  218.     {
  219.         // Sanity check
  220.         if (!$this->_intersects($in_cropWidth$in_cropHeight$in_cropX$in_cropY)) {
  221.             return PEAR::raiseError('Nothing to crop'IMAGE_TRANSFORM_ERROR_OUTOFBOUND);
  222.         }
  223.         $this->oldHandle = $this->imageHandle;
  224.         $this->imageHandle = imlib_create_cropped_image($this->imageHandle$in_cropX$in_cropY$in_cropWidth$in_cropHeight);
  225.         $this->img_x = $in_cropWidth;
  226.         $this->img_y = $in_cropHeight;
  227.         return true;
  228.     }
  229.  
  230.     // }}}
  231.     // {{{ save()
  232.  
  233.     
  234.     /**
  235.      * Save the image file.  Determines what type of image to save based on extension.
  236.      *
  237.      * @param $filename string  the name of the file to write to
  238.      * @param $type     string  (optional) define the output format, default
  239.      *                           is the current used format
  240.      * @param $quality  int     (optional) output DPI, default is 75
  241.      *
  242.      * @return TRUE on success or PEAR Error object on error
  243.      */
  244.     function save($filename$type ''$quality = 75)
  245.     {
  246.         if (!is_resource($this->imageHandle)) {
  247.             return PEAR::raiseError('Invalid image'true);
  248.         }
  249.  
  250.         $err = 0;
  251.         $type    ($type == ''$this->type : $type;
  252.         $quality (is_null($quality)) $this->_options['quality'$quality;
  253.         imlib_image_set_format($this->imageHandle$type);
  254.         $return = imlib_save_image($this->imageHandle$filename$err$quality);
  255.         $this->imageHandle = $this->oldHandle;
  256.         $this->resized = false;
  257.         if (!$return{
  258.             return PEAR::raiseError('Couldn\'t save image. Reason: ' $errtrue);
  259.         }
  260.         return true;
  261.     }
  262.  
  263.     // }}}
  264.     // {{{ display()
  265.  
  266.     
  267.     /**
  268.      * Display image without saving and lose changes
  269.      *
  270.      * This method adds the Content-type HTTP header
  271.      *
  272.      * @param string $type (optional) (JPG,PNG...);
  273.      * @param int $quality (optional) 75
  274.      *
  275.      * @return TRUE on success or PEAR Error object on error
  276.      */
  277.     function display($type ''$quality = null)
  278.     {
  279.         if (!is_resource($this->imageHandle)) {
  280.             return PEAR::raiseError('Invalid image'true);
  281.         }
  282.  
  283.         $type    ($type == ''$this->type : $type;
  284.         $quality (is_null($quality)) $this->_options['quality'$quality;
  285.         imlib_image_set_format($this->imageHandle$type);
  286.         $err = 0;
  287.         header('Content-type: ' $this->getMimeType($type));
  288.         $return = imlib_dump_image($this->imageHandle$err$quality);
  289.         $this->imageHandle = $this->oldHandle;
  290.         $this->resized = false;
  291.         imlib_free_image($this->oldHandle);
  292.         if (!$return{
  293.             return PEAR::raiseError('Couldn\'t output image. Reason: ' $errtrue);
  294.         }
  295.         return true;
  296.     }
  297.  
  298.     // }}}
  299.     // {{{ free()
  300.  
  301.     
  302.     /**
  303.      * Destroy image handle
  304.      *
  305.      * @return void 
  306.      */
  307.     function free()
  308.     {
  309.         if (is_resource($this->imageHandle)) {
  310.             imlib_free_image($this->imageHandle);
  311.         }
  312.     }
  313.  
  314.     // }}}
  315.     // {{{ _resize()
  316.  
  317.     
  318.     /**
  319.      * Resize the image.
  320.      *
  321.      * @access private
  322.      *
  323.      * @param int   $new_x   New width
  324.      * @param int   $new_y   New height
  325.      * @param mixed $options Optional parameters
  326.      *
  327.      * @return TRUE on success or PEAR Error object on error
  328.      * @see PEAR::isError()
  329.      */
  330.     function _resize($new_x$new_y$options = null)
  331.     {
  332.         if ($this->resized === true{
  333.             return PEAR::raiseError('You have already resized the image without saving it.  Your previous resizing will be overwritten'nullPEAR_ERROR_TRIGGERE_USER_NOTICE);
  334.         }
  335.  
  336.         $this->oldHandle = $this->imageHandle;
  337.         $this->imageHandle = imlib_create_scaled_image($this->imageHandle$new_x$new_y);
  338.         $this->img_x = $new_x;
  339.         $this->img_y = $new_y;
  340.         $this->resized = true;
  341.         return true;
  342.     }
  343.  
  344.     // }}}
  345.     // {{{ _get_image_details()
  346.  
  347.     
  348.     /**
  349.      * Gets the image details
  350.      *
  351.      * @access private
  352.      * @return TRUE on success or PEAR Error object on error
  353.      */
  354.     function _get_image_details()
  355.     {
  356.         $this->img_x = imlib_image_get_width($this->imageHandle);
  357.         $this->img_y = imlib_image_get_height($this->imageHandle);
  358.         $this->type = imlib_image_format($this->imageHandle);
  359.         $this->type = ($this->type == '''png' $this->type;
  360.         return true;
  361.     }
  362.  
  363.     // }}}
  364.  
  365.     
  366.     /**
  367.      * Horizontal mirroring
  368.      *
  369.      * @return TRUE on success, PEAR Error object on error
  370.      */
  371.     function mirror()
  372.     {
  373.         imlib_image_flip_horizontal($this->imageHandle);
  374.         return true;
  375.     }
  376.  
  377.     /**
  378.      * Vertical mirroring
  379.      *
  380.      * @return TRUE on success, PEAR Error object on error
  381.      */
  382.     function flip()
  383.     {
  384.         imlib_image_flip_vertical($this->imageHandle);
  385.         return true;
  386.     }
  387. }

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