source: trunk/plugins/ImageManager/Classes/IM.php @ 999

Last change on this file since 999 was 999, checked in by ray, 11 years ago

#1195 Allow to specify an external url to load a plugin from

  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 7.8 KB
Line 
1<?php
2
3/***********************************************************************
4** Title.........:  ImageMagick Driver
5** Version.......:  1.0
6** Author........:  Xiang Wei ZHUO <wei@zhuo.org>
7** Filename......:  IM.php
8** Last changed..:  30 Aug 2003
9** Notes.........:  Orginal is from PEAR
10**/
11
12// +----------------------------------------------------------------------+
13// | PHP Version 4                                                        |
14// +----------------------------------------------------------------------+
15// | Copyright (c) 1997-2002 The PHP Group                                |
16// +----------------------------------------------------------------------+
17// | This source file is subject to version 2.02 of the PHP license,      |
18// | that is bundled with this package in the file LICENSE, and is        |
19// | available at through the world-wide-web at                           |
20// | http://www.php.net/license/2_02.txt.                                 |
21// | If you did not receive a copy of the PHP license and are unable to   |
22// | obtain it through the world-wide-web, please send a note to          |
23// | license@php.net so we can mail you a copy immediately.               |
24// +----------------------------------------------------------------------+
25// | Authors: Peter Bowyer <peter@mapledesign.co.uk>                      |
26// +----------------------------------------------------------------------+
27//
28// $Id:IM.php 937 2008-01-20 23:13:25Z ray $
29//
30// Image Transformation interface using command line ImageMagick
31//
32
33require_once "../ImageManager/Classes/Transform.php";
34
35Class Image_Transform_Driver_IM extends Image_Transform
36{
37    /**
38     * associative array commands to be executed
39     * @var array
40     */
41    var $command = array();
42
43    /**
44     *
45     *
46     */
47    function Image_Transform_Driver_IM()
48    {
49        return true;
50    } // End Image_IM
51
52    /**
53     * Load image
54     *
55     * @param string filename
56     *
57     * @return mixed none or a PEAR error object on error
58     * @see PEAR::isError()
59     */
60    function load($image)
61    {
62
63        $this->uid = md5($_SERVER['REMOTE_ADDR']);
64        /*if (!file_exists($image)) {
65            return PEAR::raiseError('The image file ' . $image . ' does\'t exist', true);
66        }*/
67        $this->image = $image;
68        $this->_get_image_details($image);
69    } // End load
70
71    /**
72     * Resize Action
73     *
74     * @param int   new_x   new width
75     * @param int   new_y   new height
76     *
77     * @return none
78     * @see PEAR::isError()
79     */
80    function _resize($new_x, $new_y)
81    {
82        /*if (isset($this->command['resize'])) {
83            return PEAR::raiseError("You cannot scale or resize an image more than once without calling save or display", true);
84        }*/
85        $this->command['resize'] = "-geometry ${new_x}x${new_y}!";
86
87        $this->new_x = $new_x;
88        $this->new_y = $new_y;
89    } // End resize
90
91    /**
92     * Crop the image
93     *
94     * @param int $crop_x left column of the image
95     * @param int $crop_y top row of the image
96     * @param int $crop_width new cropped image width
97     * @param int $crop_height new cropped image height
98     */
99    function crop($crop_x, $crop_y, $crop_width, $crop_height)
100    {
101        $this->command['crop'] = "-crop {$crop_width}x{$crop_height}+{$crop_x}+{$crop_y}";
102    }
103
104    /**
105     * Flip the image horizontally or vertically
106     *
107     * @param boolean $horizontal true if horizontal flip, vertical otherwise
108     */
109    function flip($horizontal)
110    {
111        if($horizontal)
112            $this->command['flop'] = "-flop";
113        else
114            $this->command['flip'] = "-flip";
115    }
116    /**
117     * rotate
118     *
119     * @param   int     angle   rotation angle
120     * @param   array   options no option allowed
121     *
122     */
123    function rotate($angle, $options=null)
124    {
125        if ('-' == $angle{0}) {
126            $angle = 360 - substr($angle, 1);
127        }
128         $this->command['rotate'] = "-rotate $angle";
129    } // End rotate
130
131    /**
132     * addText
133     *
134     * @param   array   options     Array contains options
135     *                              array(
136     *                                  'text'  The string to draw
137     *                                  'x'     Horizontal position
138     *                                  'y'     Vertical Position
139     *                                  'Color' Font color
140     *                                  'font'  Font to be used
141     *                                  'size'  Size of the fonts in pixel
142     *                                  'resize_first'  Tell if the image has to be resized
143     *                                                  before drawing the text
144     *                              )
145     *
146     * @return none
147     * @see PEAR::isError()
148     */
149    function addText($params)
150    {
151        $default_params = array(
152                                'text' => 'This is Text',
153                                'x' => 10,
154                                'y' => 20,
155                                'color' => 'red',
156                                'font' => 'Arial.ttf',
157                                'resize_first' => false // Carry out the scaling of the image before annotation?
158                                );
159         $params = array_merge($default_params, $params);
160         extract($params);
161         if (true === $resize_first) {
162             // Set the key so that this will be the last item in the array
163            $key = 'ztext';
164         } else {
165            $key = 'text';
166         }
167         $this->command[$key] = "-font $font -fill $color -draw 'text $x,$y \"$text\"'";
168         // Producing error: gs: not found gs: not found convert: Postscript delegate failed [No such file or directory].
169    } // End addText
170
171    /**
172     * Adjust the image gamma
173     *
174     * @param float $outputgamma
175     *
176     * @return none
177     */
178    function gamma($outputgamma=1.0) {
179        $this->command['gamma'] = "-gamma $outputgamma";
180    }
181       
182        function reduce_colors($number = 256)
183        {
184                 $this->command['colors'] = "-colors $number";
185        }
186    /**
187     * Save the image file
188     *
189     * @param string  $filename the name of the file to write to
190     * @param quality $quality  image dpi, default=75
191     * @param string  $type     (JPG,PNG...)
192     *
193     * @return none
194     */
195    function save($filename, $type='', $quality = 85)
196    {
197        $type == '' ? $this->type : $type;
198        $cmd = '' . IMAGE_TRANSFORM_LIB_PATH . 'convert ';
199                $cmd .= implode(' ', $this->command) . " -quality $quality ";
200                $cmd .= '"'.($this->image) . '" "' . ($filename) . '"';
201       
202        //$cmd = str_replace('/', '\\', $cmd);
203                //echo($cmd.'<br>');
204        exec($cmd,$retval);
205                //error_log('IM '.print_r($retval,true));
206    } // End save
207
208    /**
209     * Display image without saving and lose changes
210     *
211     * @param string type (JPG,PNG...);
212     * @param int quality 75
213     *
214     * @return none
215     */
216    function display($type = '', $quality = 75)
217    {
218        if ($type == '') {
219            header('Content-type: image/' . $this->type);
220            passthru(IMAGE_TRANSFORM_LIB_PATH . '  ' . implode(' ', $this->command) . " -quality $quality "  . escapeshellarg($this->image) . ' ' . strtoupper($this->type) . ":-");
221        } else {
222            header('Content-type: image/' . $type);
223            passthru(IMAGE_TRANSFORM_LIB_PATH . 'convert ' . implode(' ', $this->command) . " -quality $quality "  . escapeshellarg($this->image) . ' ' . strtoupper($type) . ":-");
224        }
225    }
226
227
228    /**
229     * Destroy image handle
230     *
231     * @return none
232     */
233    function free()
234    {
235        return true;
236    }
237
238} // End class ImageIM
239?>
Note: See TracBrowser for help on using the repository browser.