Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/* vim: set expandtab tabstop=4 shiftwidth=4: */
4
 
5
/**
6
 * Image Transformation interface using old ImageMagick extension
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
 * @copyright  2002-2005 The PHP Group
20
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
21
 * @version    CVS: $Id: Imagick.php,v 1.9 2007/04/19 16:36:09 dufuz Exp $
22
 * @deprecated
23
 * @link       http://pear.php.net/package/Image_Transform
24
 */
25
 
26
/**
27
 * Include of base class
28
 */
29
require_once 'Image/Transform.php';
30
 
31
 
32
/**
33
 * Image Transformation interface using old ImageMagick extension
34
 *
35
 * DEPRECATED: current CVS/release imagick extension should use
36
 * the Imagick2 driver
37
 *
38
 * @deprecated
39
 */
40
class Image_Transform_Driver_Imagick extends Image_Transform
41
{
42
    /**
43
     * Handler of the imagick image ressource
44
     * @var array
45
     */
46
    var $imageHandle;
47
 
48
    /**
49
     * Handler of the image ressource before
50
     * the last transformation
51
     * @var array
52
     */
53
    var $oldImage;
54
 
55
    /**
56
     *
57
     *
58
     */
59
    function Image_Transform_Driver_Imagick()
60
    {
61
        if (!PEAR::loadExtension('imagick')) {
62
            return PEAR::raiseError('The imagick extension can not be found.', true);
63
        }
64
        include 'Image/Transform/Driver/Imagick/ImageTypes.php';
65
        return true;
66
    } // End Image_IM
67
 
68
    /**
69
     * Load image
70
     *
71
     * @param string filename
72
     *
73
     * @return mixed none or a PEAR error object on error
74
     * @see PEAR::isError()
75
     */
76
    function load($image)
77
    {
78
        $this->imageHandle = imagick_create();
79
        if ( !is_resource( $this->imageHandle ) ) {
80
            return PEAR::raiseError('Cannot initialize imagick image.', true);
81
        }
82
 
83
        if ( !imagick_read($this->imageHandle, $image) ){
84
            return PEAR::raiseError('The image file ' . $image . ' does\'t exist', true);
85
        }
86
        $this->image = $image;
87
        $result = $this->_get_image_details($image);
88
        if (PEAR::isError($result)) {
89
            return $result;
90
        }
91
    } // End load
92
 
93
    /**
94
     * Resize Action
95
     *
96
     * @param int   new_x   new width
97
     * @param int   new_y   new width
98
     * @param mixed $options Optional parameters
99
     *
100
     * @return none
101
     * @see PEAR::isError()
102
     */
103
    function _resize($new_x, $new_y, $options = null)
104
    {
105
        if ($img2 = imagick_copy_resize($this->imageHandle, $new_x, $new_y, IMAGICK_FILTER_CUBIC, 1)){
106
            $this->oldImage = $this->imageHandle;
107
            $this->imageHandle =$img2;
108
            $this->new_x = $new_x;
109
            $this->new_y = $new_y;
110
        } else {
111
            return PEAR::raiseError("Cannot create a new imagick imagick image for the resize.", true);
112
        }
113
    } // End resize
114
 
115
    /**
116
     * rotate
117
     * Note: color mask are currently not supported
118
     *
119
     * @param   int     Rotation angle in degree
120
     * @param   array   No option are actually allowed
121
     *
122
     * @return none
123
     * @see PEAR::isError()
124
     */
125
    function rotate($angle,$options=null)
126
    {
127
        if ($img2 = imagick_copy_rotate ($this->imageHandle, $angle)){
128
            $this->oldImage     = $this->imageHandle;
129
            $this->imageHandle  = $img2;
130
            $this->new_x = imagick_get_attribute($img2,'width');
131
            $this->new_y = imagick_get_attribute($img2,'height');
132
        } else {
133
            return PEAR::raiseError("Cannot create a new imagick imagick image for the resize.", true);
134
        }
135
    } // End rotate
136
 
137
    /**
138
     * addText
139
     *
140
     * @param   array   options     Array contains options
141
     *                              array(
142
     *                                  'text'  The string to draw
143
     *                                  'x'     Horizontal position
144
     *                                  'y'     Vertical Position
145
     *                                  'Color' Font color
146
     *                                  'font'  Font to be used
147
     *                                  'size'  Size of the fonts in pixel
148
     *                                  'resize_first'  Tell if the image has to be resized
149
     *                                                  before drawing the text
150
     *                              )
151
     *
152
     * @return none
153
     * @see PEAR::isError()
154
     */
155
    function addText($params)
156
    {
157
        $default_params = array(
158
                                'text'          => 'This is a Text',
159
                                'x'             => 10,
160
                                'y'             => 20,
161
                                'size'          => 12,
162
                                'color'         => 'red',
163
                                'font'          => 'Arial.ttf',
164
                                'resize_first'  => false // Carry out the scaling of the image before annotation?
165
                                );
166
        $params = array_merge($default_params, $params);
167
        extract($params);
168
 
169
        $color = is_array($color)?$this->colorarray2colorhex($color):strtolower($color);
170
 
171
        imagick_annotate($this->imageHandle,array(
172
                    "primitive"     => "text $x,$y ".$text,
173
                    "pointsize"     => $size,
174
                    "antialias"     => 0,
175
                    "fill"          => $color,
176
                    "font"          => $font,
177
                    ));
178
    } // End addText
179
 
180
    /**
181
     * Save the image file
182
     *
183
     * @param $filename string the name of the file to write to
184
     *
185
     * @return none
186
     */
187
    function save($filename, $type='', $quality = 75)
188
    {
189
        if (function_exists('imagick_setcompressionquality')) {
190
            imagick_setcompressionquality($this->imageHandle, $quality);
191
        }
192
        if ($type != '') {
193
            $type = strtoupper($type);
194
            imagick_write($this->imageHandle, $filename, $type);
195
        } else {
196
            imagick_write($this->imageHandle, $filename);
197
        }
198
        imagick_free($handle);
199
    } // End save
200
 
201
    /**
202
     * Display image without saving and lose changes
203
     *
204
     * @param string type (JPG,PNG...);
205
     * @param int quality 75
206
     *
207
     * @return none
208
     */
209
    function display($type = '', $quality = 75)
210
    {
211
        if ($type == '') {
212
            header('Content-type: image/' . $this->type);
213
            if (!imagick_dump($this->imageHandle));
214
        } else {
215
            header('Content-type: image/' . $type);
216
            if (!imagick_dump($this->imageHandle, $this->type));
217
        }
218
        $this->free();
219
    }
220
 
221
 
222
    /**
223
     * Destroy image handle
224
     *
225
     * @return none
226
     */
227
    function free()
228
    {
229
        if (is_resource($this->imageHandle)){
230
            imagick_free($this->imageHandle);
231
        }
232
        if (is_resource($this->oldImage)){
233
            imagick_free($this->oldImage);
234
        }
235
        return true;
236
    }
237
 
238
} // End class ImageIM