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

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