|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* GD implementation for Image_Transform package |
|
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 [email protected] so we can mail you a copy immediately. |
|
15
|
|
|
* |
|
16
|
|
|
* @category Image |
|
17
|
|
|
* @package Image_Transform |
|
18
|
|
|
* @subpackage Image_Transform_Driver_GD |
|
19
|
|
|
* @author Alan Knowles <[email protected]> |
|
20
|
|
|
* @author Peter Bowyer <[email protected]> |
|
21
|
|
|
* @author Philippe Jausions <[email protected]> |
|
22
|
|
|
* @copyright 2002-2005 The PHP Group |
|
23
|
|
|
* @license http://www.php.net/license/3_0.txt PHP License 3.0 |
|
24
|
|
|
* @version CVS: $Id: GD.php 322661 2012-01-24 12:02:59Z clockwerx $ |
|
25
|
|
|
* @link http://pear.php.net/package/Image_Transform |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
//require_once __DIR__ . '/Image/Transform.php'; |
|
29
|
|
|
require_once XOOPS_ROOT_PATH . '/modules/extgallery/class/pear/Image/Transform.php'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* GD implementation for Image_Transform package |
|
33
|
|
|
* |
|
34
|
|
|
* Usage : |
|
35
|
|
|
* $img =& Image_Transform::factory('GD'); |
|
36
|
|
|
* $angle = -78; |
|
37
|
|
|
* $img->load('magick.png'); |
|
38
|
|
|
* |
|
39
|
|
|
* if ($img->rotate($angle, array( |
|
40
|
|
|
* 'autoresize' => true, |
|
41
|
|
|
* 'color_mask' => array(255, 0, 0)))) { |
|
42
|
|
|
* $img->addText(array( |
|
43
|
|
|
* 'text' => 'Rotation ' . $angle, |
|
44
|
|
|
* 'x' => 0, |
|
45
|
|
|
* 'y' => 100, |
|
46
|
|
|
* 'font' => '/usr/share/fonts/default/TrueType/cogb____.ttf')); |
|
47
|
|
|
* $img->display(); |
|
48
|
|
|
* } else { |
|
49
|
|
|
* echo "Error"; |
|
50
|
|
|
* } |
|
51
|
|
|
* $img->free(); |
|
52
|
|
|
* |
|
53
|
|
|
* @category Image |
|
54
|
|
|
* @package Image_Transform |
|
55
|
|
|
* @subpackage Image_Transform_Driver_GD |
|
56
|
|
|
* @author Alan Knowles <[email protected]> |
|
57
|
|
|
* @author Peter Bowyer <[email protected]> |
|
58
|
|
|
* @author Philippe Jausions <[email protected]> |
|
59
|
|
|
* @copyright 2002-2005 The PHP Group |
|
60
|
|
|
* @license http://www.php.net/license/3_0.txt PHP License 3.0 |
|
61
|
|
|
* @version Release: @package_version@ |
|
62
|
|
|
* @link http://pear.php.net/package/Image_Transform |
|
63
|
|
|
* @since PHP 4.0 |
|
64
|
|
|
*/ |
|
65
|
|
|
class Image_Transform_Driver_GD extends Image_Transform |
|
66
|
|
|
{ |
|
67
|
|
|
/** |
|
68
|
|
|
* Holds the image resource for manipulation |
|
69
|
|
|
* |
|
70
|
|
|
* @var resource $imageHandle |
|
71
|
|
|
* @access protected |
|
72
|
|
|
*/ |
|
73
|
|
|
public $imageHandle = null; |
|
74
|
|
|
/** |
|
75
|
|
|
* Holds the original image file |
|
76
|
|
|
* |
|
77
|
|
|
* @var resource $imageHandle |
|
78
|
|
|
* @access protected |
|
79
|
|
|
*/ |
|
80
|
|
|
public $oldImage = null; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Check settings |
|
84
|
|
|
*/ |
|
85
|
|
|
public function Image_Transform_Driver_GD() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->__construct(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// End function Image |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Check settings |
|
94
|
|
|
* |
|
95
|
|
|
* @since PHP 5 |
|
96
|
|
|
*/ |
|
97
|
|
|
public function __construct() |
|
98
|
|
|
{ |
|
99
|
|
|
if (!PEAR::loadExtension('gd')) { |
|
100
|
|
|
$this->isError(PEAR::raiseError('GD library is not available.', IMAGE_TRANSFORM_ERROR_UNSUPPORTED)); |
|
|
|
|
|
|
101
|
|
|
} else { |
|
102
|
|
|
$types = imagetypes(); |
|
103
|
|
|
if ($types & IMG_PNG) { |
|
104
|
|
|
$this->_supported_image_types['png'] = 'rw'; |
|
105
|
|
|
} |
|
106
|
|
|
if (($types & IMG_GIF) |
|
107
|
|
|
|| function_exists('imagegif')) { |
|
108
|
|
|
$this->_supported_image_types['gif'] = 'rw'; |
|
109
|
|
|
} elseif (function_exists('imagecreatefromgif')) { |
|
110
|
|
|
$this->_supported_image_types['gif'] = 'rw'; |
|
111
|
|
|
} |
|
112
|
|
|
if ($types & IMG_JPG) { |
|
113
|
|
|
$this->_supported_image_types['jpeg'] = 'rw'; |
|
114
|
|
|
} |
|
115
|
|
|
if ($types & IMG_WBMP) { |
|
116
|
|
|
$this->_supported_image_types['wbmp'] = 'rw'; |
|
117
|
|
|
} |
|
118
|
|
|
if (!$this->_supported_image_types) { |
|
|
|
|
|
|
119
|
|
|
$this->isError(PEAR::raiseError('No supported image types available', IMAGE_TRANSFORM_ERROR_UNSUPPORTED)); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// End function Image |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Loads an image from file |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $image filename |
|
130
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
131
|
|
|
* @access public |
|
132
|
|
|
*/ |
|
133
|
|
|
public function load($image) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->free(); |
|
136
|
|
|
|
|
137
|
|
|
$this->image = $image; |
|
138
|
|
|
$result = $this->_get_image_details($image); |
|
139
|
|
|
if (PEAR::isError($result)) { |
|
140
|
|
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
if (!$this->supportsType($this->type, 'r')) { |
|
143
|
|
|
return PEAR::raiseError('Image type not supported for input', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$functionName = 'ImageCreateFrom' . $this->type; |
|
147
|
|
|
$this->imageHandle = $functionName($this->image); |
|
148
|
|
|
if (!$this->imageHandle) { |
|
149
|
|
|
$this->imageHandle = null; |
|
150
|
|
|
|
|
151
|
|
|
return PEAR::raiseError('Error while loading image file.', IMAGE_TRANSFORM_ERROR_IO); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return true; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// End load |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Returns the GD image handle |
|
161
|
|
|
* |
|
162
|
|
|
* @return resource |
|
163
|
|
|
* |
|
164
|
|
|
* @access public |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getHandle() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->imageHandle; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
//function getHandle() |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Adds a border of constant width around an image |
|
175
|
|
|
* |
|
176
|
|
|
* @param int $border_width Width of border to add |
|
177
|
|
|
* @param string $color |
|
178
|
|
|
* @return bool TRUE |
|
179
|
|
|
* @author Peter Bowyer |
|
180
|
|
|
* @access public |
|
181
|
|
|
*/ |
|
182
|
|
|
public function addBorder($border_width = null, $color = '') |
|
183
|
|
|
{ |
|
184
|
|
|
$this->new_x = $this->img_x + 2 * $border_width; |
|
185
|
|
|
$this->new_y = $this->img_y + 2 * $border_width; |
|
186
|
|
|
|
|
187
|
|
|
$new_img = $this->_createImage($new_x, $new_y, $this->true_color); |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
$options = ['pencilColor', $color]; |
|
190
|
|
|
$color = $this->_getColor('pencilColor', $options, [0, 0, 0]); |
|
191
|
|
|
if ($color) { |
|
|
|
|
|
|
192
|
|
|
if ($this->true_color) { |
|
193
|
|
|
$c = imagecolorresolve($this->imageHandle, $color[0], $color[1], $color[2]); |
|
194
|
|
|
imagefill($new_img, 0, 0, $c); |
|
195
|
|
|
} else { |
|
196
|
|
|
imagecolorset($new_img, imagecolorat($new_img, 0, 0), $color[0], $color[1], $color[2]); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
imagecopy($new_img, $this->imageHandle, $border_width, $border_width, 0, 0, $this->img_x, $this->img_y); |
|
200
|
|
|
$this->imageHandle = $new_img; |
|
201
|
|
|
$this->resized = true; |
|
202
|
|
|
|
|
203
|
|
|
return true; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* addText |
|
208
|
|
|
* |
|
209
|
|
|
* @param array $params Array contains options |
|
210
|
|
|
* array( |
|
211
|
|
|
* 'text' The string to draw |
|
212
|
|
|
* 'x' Horizontal position |
|
213
|
|
|
* 'y' Vertical Position |
|
214
|
|
|
* 'color' Font color |
|
215
|
|
|
* 'font' Font to be used |
|
216
|
|
|
* 'size' Size of the fonts in pixel |
|
217
|
|
|
* 'resize_first' Tell if the image has to be resized |
|
218
|
|
|
* before drawing the text |
|
219
|
|
|
* ) |
|
220
|
|
|
* |
|
221
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
222
|
|
|
*/ |
|
223
|
|
|
public function addText($params = null) |
|
224
|
|
|
{ |
|
225
|
|
|
$this->oldImage = $this->imageHandle; |
|
226
|
|
|
$params = array_merge($this->_get_default_text_params(), $params); |
|
|
|
|
|
|
227
|
|
|
extract($params); |
|
228
|
|
|
|
|
229
|
|
|
$options = ['fontColor' => $color]; |
|
230
|
|
|
$color = $this->_getColor('fontColor', $options, [0, 0, 0]); |
|
231
|
|
|
|
|
232
|
|
|
$c = imagecolorresolve($this->imageHandle, $color[0], $color[1], $color[2]); |
|
233
|
|
|
|
|
234
|
|
|
if ('ttf' == mb_substr($font, -3)) { |
|
235
|
|
|
imagettftext($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); |
|
236
|
|
|
} else { |
|
237
|
|
|
imagepstext($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return true; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
// End addText |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Rotates image by the given angle |
|
247
|
|
|
* |
|
248
|
|
|
* Uses a fast rotation algorythm for custom angles |
|
249
|
|
|
* or lines copy for multiple of 90 degrees |
|
250
|
|
|
* |
|
251
|
|
|
* @param int $angle Rotation angle |
|
252
|
|
|
* @param array $options array( |
|
253
|
|
|
* 'canvasColor' => array(r ,g, b), named color or #rrggbb |
|
254
|
|
|
* ) |
|
255
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
256
|
|
|
* @access public |
|
257
|
|
|
* @author Pierre-Alain Joye |
|
258
|
|
|
*/ |
|
259
|
|
|
public function rotate($angle, $options = null) |
|
260
|
|
|
{ |
|
261
|
|
|
if (0 == ($angle % 360)) { |
|
262
|
|
|
return true; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
$color_mask = $this->_getColor('canvasColor', $options, [255, 255, 255]); |
|
266
|
|
|
|
|
267
|
|
|
$mask = imagecolorresolve($this->imageHandle, $color_mask[0], $color_mask[1], $color_mask[2]); |
|
268
|
|
|
|
|
269
|
|
|
$this->oldImage = $this->imageHandle; |
|
270
|
|
|
|
|
271
|
|
|
// Multiply by -1 to change the sign, so the image is rotated clockwise |
|
272
|
|
|
$this->imageHandle = imagerotate($this->imageHandle, $angle * -1, $mask); |
|
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
return true; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Horizontal mirroring |
|
279
|
|
|
* |
|
280
|
|
|
* @return mixed TRUE or PEAR_Error object on error |
|
281
|
|
|
* @access public |
|
282
|
|
|
* @see flip() |
|
283
|
|
|
**/ |
|
284
|
|
|
public function mirror() |
|
285
|
|
|
{ |
|
286
|
|
|
$new_img = $this->_createImage(); |
|
287
|
|
|
for ($x = 0; $x < $this->new_x; ++$x) { |
|
288
|
|
|
imagecopy($new_img, $this->imageHandle, $x, 0, $this->new_x - $x - 1, 0, 1, $this->new_y); |
|
289
|
|
|
} |
|
290
|
|
|
imagedestroy($this->imageHandle); |
|
291
|
|
|
$this->imageHandle = $new_img; |
|
292
|
|
|
|
|
293
|
|
|
return true; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Vertical mirroring |
|
298
|
|
|
* |
|
299
|
|
|
* @return true or PEAR Error object on error |
|
300
|
|
|
* @access public |
|
301
|
|
|
* @see mirror() |
|
302
|
|
|
**/ |
|
303
|
|
|
public function flip() |
|
304
|
|
|
{ |
|
305
|
|
|
$new_img = $this->_createImage(); |
|
306
|
|
|
for ($y = 0; $y < $this->new_y; ++$y) { |
|
307
|
|
|
imagecopy($new_img, $this->imageHandle, 0, $y, 0, $this->new_y - $y - 1, $this->new_x, 1); |
|
308
|
|
|
} |
|
309
|
|
|
imagedestroy($this->imageHandle); |
|
310
|
|
|
$this->imageHandle = $new_img; |
|
311
|
|
|
|
|
312
|
|
|
/* for very large images we may want to use the following |
|
313
|
|
|
Needs to find out what is the threshhold |
|
314
|
|
|
for ($x = 0; $x < $this->new_x; ++$x) { |
|
315
|
|
|
for ($y1 = 0; $y1 < $this->new_y / 2; ++$y1) { |
|
316
|
|
|
$y2 = $this->new_y - 1 - $y1; |
|
317
|
|
|
$color1 = imagecolorat($this->imageHandle, $x, $y1); |
|
318
|
|
|
$color2 = imagecolorat($this->imageHandle, $x, $y2); |
|
319
|
|
|
imagesetpixel($this->imageHandle, $x, $y1, $color2); |
|
320
|
|
|
imagesetpixel($this->imageHandle, $x, $y2, $color1); |
|
321
|
|
|
} |
|
322
|
|
|
} */ |
|
323
|
|
|
|
|
324
|
|
|
return true; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Crops image by size and start coordinates |
|
329
|
|
|
* |
|
330
|
|
|
* @param mixed $width |
|
331
|
|
|
* @param mixed $height |
|
332
|
|
|
* @param mixed $x |
|
333
|
|
|
* @param mixed $y |
|
334
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
335
|
|
|
* @access public |
|
336
|
|
|
*/ |
|
337
|
|
|
public function crop($width, $height, $x = 0, $y = 0) |
|
338
|
|
|
{ |
|
339
|
|
|
// Sanity check |
|
340
|
|
|
if (!$this->intersects($width, $height, $x, $y)) { |
|
341
|
|
|
return PEAR::raiseError('Nothing to crop', IMAGE_TRANSFORM_ERROR_OUTOFBOUND); |
|
342
|
|
|
} |
|
343
|
|
|
$x = min($this->new_x, max(0, $x)); |
|
344
|
|
|
$y = min($this->new_y, max(0, $y)); |
|
345
|
|
|
$width = min($width, $this->new_x - $x); |
|
346
|
|
|
$height = min($height, $this->new_y - $y); |
|
347
|
|
|
$new_img = $this->_createImage($width, $height); |
|
348
|
|
|
|
|
349
|
|
|
if (!imagecopy($new_img, $this->imageHandle, 0, 0, $x, $y, $width, $height)) { |
|
350
|
|
|
imagedestroy($new_img); |
|
351
|
|
|
|
|
352
|
|
|
return PEAR::raiseError('Failed transformation: crop()', IMAGE_TRANSFORM_ERROR_FAILED); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
$this->oldImage = $this->imageHandle; |
|
356
|
|
|
$this->imageHandle = $new_img; |
|
357
|
|
|
$this->resized = true; |
|
358
|
|
|
|
|
359
|
|
|
$this->new_x = $width; |
|
360
|
|
|
$this->new_y = $height; |
|
361
|
|
|
|
|
362
|
|
|
return true; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Converts the image to greyscale |
|
367
|
|
|
* |
|
368
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
369
|
|
|
* @access public |
|
370
|
|
|
*/ |
|
371
|
|
|
public function greyscale() |
|
372
|
|
|
{ |
|
373
|
|
|
imagecopymergegray($this->imageHandle, $this->imageHandle, 0, 0, 0, 0, $this->new_x, $this->new_y, 0); |
|
374
|
|
|
|
|
375
|
|
|
return true; |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* Resize Action |
|
380
|
|
|
* |
|
381
|
|
|
* For GD 2.01+ the new copyresampled function is used |
|
382
|
|
|
* It uses a bicubic interpolation algorithm to get far |
|
383
|
|
|
* better result. |
|
384
|
|
|
* |
|
385
|
|
|
* @param int $new_x New width |
|
386
|
|
|
* @param int $new_y New height |
|
387
|
|
|
* @param array $options Optional parameters |
|
388
|
|
|
* <ul> |
|
389
|
|
|
* <li>'scaleMethod': "pixel" or "smooth"</li> |
|
390
|
|
|
* </ul> |
|
391
|
|
|
* |
|
392
|
|
|
* @return bool|PEAR_Error TRUE on success or PEAR_Error object on error |
|
393
|
|
|
* @access protected |
|
394
|
|
|
*/ |
|
395
|
|
|
public function _resize($new_x = null, $new_y = null, $options = null) |
|
396
|
|
|
{ |
|
397
|
|
|
if (true === $this->resized) { |
|
398
|
|
|
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); |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
if ($this->new_x == $new_x && $this->new_y == $new_y) { |
|
402
|
|
|
return true; |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
$scaleMethod = $this->_getOption('scaleMethod', $options, 'smooth'); |
|
406
|
|
|
|
|
407
|
|
|
// Make sure to get a true color image if doing resampled resizing |
|
408
|
|
|
// otherwise get the same type of image |
|
409
|
|
|
$trueColor = ('pixel' == $scaleMethod) ? null : true; |
|
410
|
|
|
$new_img = $this->_createImage($new_x, $new_y, $trueColor); |
|
411
|
|
|
|
|
412
|
|
|
$icr_res = null; |
|
413
|
|
|
if ('pixel' != $scaleMethod && function_exists('ImageCopyResampled')) { |
|
414
|
|
|
$icr_res = imagecopyresampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); |
|
415
|
|
|
} |
|
416
|
|
|
if (!$icr_res) { |
|
|
|
|
|
|
417
|
|
|
imagecopyresized($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); |
|
418
|
|
|
} |
|
419
|
|
|
$this->oldImage = $this->imageHandle; |
|
420
|
|
|
$this->imageHandle = $new_img; |
|
421
|
|
|
$this->resized = true; |
|
422
|
|
|
|
|
423
|
|
|
$this->new_x = $new_x; |
|
424
|
|
|
$this->new_y = $new_y; |
|
425
|
|
|
|
|
426
|
|
|
return true; |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/** |
|
430
|
|
|
* Adjusts the image gamma |
|
431
|
|
|
* |
|
432
|
|
|
* @param float $outputgamma |
|
433
|
|
|
* |
|
434
|
|
|
* @return bool|PEAR_Error TRUE or a PEAR_Error object on error |
|
435
|
|
|
* @access public |
|
436
|
|
|
*/ |
|
437
|
|
|
public function gamma($outputgamma = 1.0) |
|
438
|
|
|
{ |
|
439
|
|
|
if (1.0 != $outputgamma) { |
|
440
|
|
|
imagegammacorrect($this->imageHandle, 1.0, $outputgamma); |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
return true; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Helper method to save to a file or output the image |
|
448
|
|
|
* |
|
449
|
|
|
* @param string $filename the name of the file to write to (blank to output) |
|
450
|
|
|
* @param string $type |
|
451
|
|
|
* @param int $quality output DPI, default is 75 |
|
452
|
|
|
* |
|
453
|
|
|
* @return bool|PEAR_Error TRUE on success or PEAR_Error object on error |
|
454
|
|
|
* @access protected |
|
455
|
|
|
*/ |
|
456
|
|
|
public function _generate($filename, $type = '', $quality = null) |
|
457
|
|
|
{ |
|
458
|
|
|
$type = mb_strtolower(('' == $type) ? $this->type : $type); |
|
459
|
|
|
$options = is_array($quality) ? $quality : []; |
|
|
|
|
|
|
460
|
|
|
switch ($type) { |
|
461
|
|
|
case 'jpg': |
|
462
|
|
|
$type = 'jpeg'; |
|
463
|
|
|
// no break |
|
464
|
|
|
case 'jpeg': |
|
465
|
|
|
if (is_numeric($quality)) { |
|
466
|
|
|
$options['quality'] = $quality; |
|
467
|
|
|
} |
|
468
|
|
|
$quality = $this->_getOption('quality', $options, 75); |
|
469
|
|
|
break; |
|
470
|
|
|
} |
|
471
|
|
|
if (!$this->supportsType($type, 'w')) { |
|
472
|
|
|
return PEAR::raiseError('Image type not supported for output', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
if ('' == $filename) { |
|
476
|
|
|
header('Content-type: ' . $this->getMimeType($type)); |
|
477
|
|
|
$action = 'output image'; |
|
478
|
|
|
} else { |
|
479
|
|
|
$action = 'save image to file'; |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
$functionName = 'image' . $type; |
|
483
|
|
|
switch ($type) { |
|
484
|
|
|
case 'jpeg': |
|
485
|
|
|
$result = $functionName($this->imageHandle, $filename, $quality); |
|
486
|
|
|
break; |
|
487
|
|
|
default: |
|
488
|
|
|
if ('' == $filename) { |
|
489
|
|
|
$result = $functionName($this->imageHandle); |
|
490
|
|
|
} else { |
|
491
|
|
|
$result = $functionName($this->imageHandle, $filename); |
|
492
|
|
|
} |
|
493
|
|
|
} |
|
494
|
|
|
if (!$result) { |
|
495
|
|
|
return PEAR::raiseError('Couldn\'t ' . $action, IMAGE_TRANSFORM_ERROR_IO); |
|
496
|
|
|
} |
|
497
|
|
|
$this->imageHandle = $this->oldImage; |
|
498
|
|
|
if (!$this->keep_settings_on_save) { |
|
499
|
|
|
$this->free(); |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
return true; |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
|
|
// End save |
|
506
|
|
|
|
|
507
|
|
|
/** |
|
508
|
|
|
* Displays image without saving and lose changes. |
|
509
|
|
|
* |
|
510
|
|
|
* This method adds the Content-type HTTP header |
|
511
|
|
|
* |
|
512
|
|
|
* @param string $type (JPEG, PNG...); |
|
513
|
|
|
* @param int $quality 75 |
|
514
|
|
|
* |
|
515
|
|
|
* @return bool|PEAR_Error TRUE or PEAR_Error object on error |
|
516
|
|
|
* @access public |
|
517
|
|
|
*/ |
|
518
|
|
|
public function display($type = '', $quality = null) |
|
519
|
|
|
{ |
|
520
|
|
|
return $this->_generate('', $type, $quality); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
/** |
|
524
|
|
|
* Saves the image to a file |
|
525
|
|
|
* |
|
526
|
|
|
* @param string $filename the name of the file to write to |
|
527
|
|
|
* @param string $type the output format, default |
|
528
|
|
|
* is the current used format |
|
529
|
|
|
* @param int $quality default is 75 |
|
530
|
|
|
* |
|
531
|
|
|
* @return bool|PEAR_Error TRUE on success or PEAR_Error object on error |
|
532
|
|
|
* @access public |
|
533
|
|
|
*/ |
|
534
|
|
|
public function save($filename, $type = '', $quality = null) |
|
535
|
|
|
{ |
|
536
|
|
|
if (!trim($filename)) { |
|
537
|
|
|
return PEAR::raiseError('Filename missing', IMAGE_TRANSFORM_ERROR_ARGUMENT); |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
return $this->_generate($filename, $type, $quality); |
|
541
|
|
|
} |
|
542
|
|
|
|
|
543
|
|
|
/** |
|
544
|
|
|
* Destroys image handle |
|
545
|
|
|
* |
|
546
|
|
|
* @access public |
|
547
|
|
|
*/ |
|
548
|
|
|
public function free() |
|
549
|
|
|
{ |
|
550
|
|
|
$this->resized = false; |
|
551
|
|
|
if (is_resource($this->imageHandle)) { |
|
552
|
|
|
imagedestroy($this->imageHandle); |
|
553
|
|
|
} |
|
554
|
|
|
$this->imageHandle = null; |
|
555
|
|
|
if (is_resource($this->oldImage)) { |
|
556
|
|
|
imagedestroy($this->oldImage); |
|
557
|
|
|
} |
|
558
|
|
|
$this->oldImage = null; |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
/** |
|
562
|
|
|
* Returns a new image for temporary processing |
|
563
|
|
|
* |
|
564
|
|
|
* @param int $width width of the new image |
|
565
|
|
|
* @param int $height height of the new image |
|
566
|
|
|
* @param bool $trueColor force which type of image to create |
|
567
|
|
|
* @return resource a GD image resource |
|
568
|
|
|
* @access protected |
|
569
|
|
|
*/ |
|
570
|
|
|
public function _createImage($width = -1, $height = -1, $trueColor = null) |
|
571
|
|
|
{ |
|
572
|
|
|
if (-1 == $width) { |
|
573
|
|
|
$width = $this->new_x; |
|
574
|
|
|
} |
|
575
|
|
|
if (-1 == $height) { |
|
576
|
|
|
$height = $this->new_y; |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
$new_img = null; |
|
580
|
|
|
if (null === $trueColor) { |
|
581
|
|
|
if (function_exists('imageistruecolor')) { |
|
582
|
|
|
$createtruecolor = imageistruecolor($this->imageHandle); |
|
583
|
|
|
} else { |
|
584
|
|
|
$createtruecolor = true; |
|
585
|
|
|
} |
|
586
|
|
|
} else { |
|
587
|
|
|
$createtruecolor = $trueColor; |
|
588
|
|
|
} |
|
589
|
|
|
if ($createtruecolor |
|
590
|
|
|
&& function_exists('ImageCreateTrueColor')) { |
|
591
|
|
|
$new_img = @imagecreatetruecolor($width, $height); |
|
592
|
|
|
//GIF Transparent Patch |
|
593
|
|
|
if ('gif' != $this->type) { |
|
594
|
|
|
imagealphablending($new_img, false); |
|
|
|
|
|
|
595
|
|
|
imagesavealpha($new_img, true); |
|
|
|
|
|
|
596
|
|
|
} |
|
597
|
|
|
//End GIF Transparent Patch |
|
598
|
|
|
} |
|
599
|
|
|
if (!$new_img) { |
|
600
|
|
|
$new_img = imagecreate($width, $height); |
|
601
|
|
|
imagepalettecopy($new_img, $this->imageHandle); |
|
602
|
|
|
$color = imagecolortransparent($this->imageHandle); |
|
603
|
|
|
if (-1 != $color) { |
|
604
|
|
|
imagecolortransparent($new_img, $color); |
|
605
|
|
|
imagefill($new_img, 0, 0, $color); |
|
606
|
|
|
} |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
//GIF Transparent Patch |
|
610
|
|
|
if ('gif' == $this->type) { |
|
611
|
|
|
$transparencyIndex = imagecolortransparent($this->imageHandle); |
|
612
|
|
|
$transparencyColor = ['red' => 255, 'green' => 255, 'blue' => 255]; |
|
613
|
|
|
|
|
614
|
|
|
if ($transparencyIndex >= 0) { |
|
615
|
|
|
$transparencyColor = imagecolorsforindex($this->imageHandle, $transparencyIndex); |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
$transparencyIndex = imagecolorallocate($new_img, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']); |
|
619
|
|
|
imagefill($new_img, 0, 0, $transparencyIndex); |
|
620
|
|
|
imagecolortransparent($new_img, $transparencyIndex); |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
//End GIF Transparent Patch |
|
624
|
|
|
|
|
625
|
|
|
return $new_img; |
|
|
|
|
|
|
626
|
|
|
} |
|
627
|
|
|
} |
|
628
|
|
|
|