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

Documentation generated on Mon, 11 Mar 2019 15:00:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.