Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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