|
1
|
|
|
<?php |
|
2
|
|
|
if(!defined('AJAX_INIT_DONE')) |
|
3
|
|
|
{ |
|
4
|
|
|
die('Permission denied'); |
|
5
|
|
|
} |
|
6
|
|
|
?><?php |
|
7
|
|
|
/** |
|
8
|
|
|
* this class provide functions to edit an image, e.g. resize, rotate, flip, crop |
|
9
|
|
|
* @author Logan Cai cailongqun [at] yahoo [dot] com [dot] cn |
|
10
|
|
|
* @link www.phpletter.com |
|
11
|
|
|
* @version 0.9 |
|
12
|
|
|
* @since 14/May/2007 |
|
13
|
|
|
* @name Image |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class Image |
|
21
|
|
|
{ |
|
22
|
|
|
var $_debug = false; |
|
23
|
|
|
var $_errors = array(); |
|
24
|
|
|
var $gdInfo = array(); //keep all information of GD extension |
|
25
|
|
|
var $_imgOrig = null; //the hanlder of original image |
|
26
|
|
|
var $_imgFinal = null; //the handler of final image |
|
27
|
|
|
var $imageFile = null; |
|
28
|
|
|
var $transparentColorRed = null; |
|
29
|
|
|
var $transparentColorGreen = null; |
|
30
|
|
|
var $transparentColorBlue = null; |
|
31
|
|
|
var $chmod = 0755; |
|
32
|
|
|
var $_imgInfoOrig = array( |
|
33
|
|
|
'name'=>'', |
|
34
|
|
|
'ext'=>'', |
|
35
|
|
|
'size'=>'', |
|
36
|
|
|
'width'=>'', |
|
37
|
|
|
'height'=>'', |
|
38
|
|
|
'type'=>'', |
|
39
|
|
|
'path'=>'', |
|
40
|
|
|
); |
|
41
|
|
|
var $_imgInfoFinal = array( |
|
42
|
|
|
'name'=>'', |
|
43
|
|
|
'ext'=>'', |
|
44
|
|
|
'size'=>'', |
|
45
|
|
|
'width'=>'', |
|
46
|
|
|
'height'=>'', |
|
47
|
|
|
'type'=>'', |
|
48
|
|
|
'path'=>'', |
|
49
|
|
|
); |
|
50
|
|
|
var $_imgQuality = 90; |
|
51
|
|
|
/** |
|
52
|
|
|
* constructor |
|
53
|
|
|
* |
|
54
|
|
|
* @param boolean $debug |
|
55
|
|
|
* @return Image |
|
56
|
|
|
*/ |
|
57
|
|
|
|
|
58
|
|
|
function __construct($debug = false) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->enableDebug($debug); |
|
61
|
|
|
$this->gdInfo = $this->getGDInfo(); |
|
62
|
|
|
} |
|
63
|
|
|
function Image($debug = false) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->__construct($debug); |
|
66
|
|
|
} |
|
67
|
|
|
/** |
|
68
|
|
|
* enable to debug |
|
69
|
|
|
* |
|
70
|
|
|
* @param boolean $value |
|
71
|
|
|
*/ |
|
72
|
|
|
function enableDebug($value) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->_debug = ($value?true:false); |
|
75
|
|
|
} |
|
76
|
|
|
/** |
|
77
|
|
|
* check if debug enable |
|
78
|
|
|
* @return boolean |
|
79
|
|
|
*/ |
|
80
|
|
|
function _isDebugEnable() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->_debug; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* append to errors array and shown the each error when the debug turned on |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $string |
|
89
|
|
|
* @return void |
|
90
|
|
|
* @access private |
|
91
|
|
|
* @copyright this function originally come from Andy's php |
|
92
|
|
|
*/ |
|
93
|
|
|
function _debug($value) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->_errors[] = $value; |
|
96
|
|
|
if ($this->_debug) |
|
97
|
|
|
{ |
|
98
|
|
|
echo $value . "<br />\n"; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
/** |
|
102
|
|
|
* show erros |
|
103
|
|
|
* |
|
104
|
|
|
*/ |
|
105
|
|
|
function showErrors() |
|
106
|
|
|
{ |
|
107
|
|
|
if(sizeof($this->_errors)) |
|
108
|
|
|
{ |
|
109
|
|
|
foreach($this->_errors as $error) |
|
110
|
|
|
{ |
|
111
|
|
|
echo $error . "<br />\n"; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
/** |
|
116
|
|
|
* Load an image from the file system. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $filename |
|
119
|
|
|
* @return bool |
|
120
|
|
|
* @access public |
|
121
|
|
|
* @copyright this function originally come from Andy's php |
|
122
|
|
|
*/ |
|
123
|
|
|
function loadImage($filename) |
|
124
|
|
|
{ |
|
125
|
|
|
$ext = strtolower($this->_getExtension($filename)); |
|
126
|
|
|
$func = 'imagecreatefrom' . ($ext == 'jpg' ? 'jpeg' : $ext); |
|
127
|
|
|
if (!$this->_isSupported($filename, $ext, $func, false)) { |
|
128
|
|
|
|
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
if($ext == "gif") |
|
132
|
|
|
{ |
|
133
|
|
|
// the following part gets the transparency color for a gif file |
|
134
|
|
|
// this code is from the PHP manual and is written by |
|
135
|
|
|
// fred at webblake dot net and webmaster at webnetwizard dotco dotuk, thanks! |
|
136
|
|
|
$fp = @fopen($filename, "rb"); |
|
137
|
|
|
$result = @fread($fp, 13); |
|
138
|
|
|
$colorFlag = ord(substr($result,10,1)) >> 7; |
|
139
|
|
|
$background = ord(substr($result,11)); |
|
140
|
|
|
if ($colorFlag) { |
|
141
|
|
|
$tableSizeNeeded = ($background + 1) * 3; |
|
142
|
|
|
$result = @fread($fp, $tableSizeNeeded); |
|
143
|
|
|
$this->transparentColorRed = ord(substr($result, $background * 3, 1)); |
|
144
|
|
|
$this->transparentColorGreen = ord(substr($result, $background * 3 + 1, 1)); |
|
145
|
|
|
$this->transparentColorBlue = ord(substr($result, $background * 3 + 2, 1)); |
|
146
|
|
|
} |
|
147
|
|
|
fclose($fp); |
|
148
|
|
|
// -- here ends the code related to transparency handling |
|
149
|
|
|
} |
|
150
|
|
|
$this->_imgOrig = @$func($filename); |
|
151
|
|
|
if ($this->_imgOrig == null) { |
|
152
|
|
|
$this->_debug("The image could not be created from the '$filename' file using the '$func' function."); |
|
153
|
|
|
return false; |
|
154
|
|
|
}else |
|
155
|
|
|
{ |
|
156
|
|
|
$this->imageFile = $filename; |
|
157
|
|
|
$this->_imgInfoOrig = array( |
|
158
|
|
|
'name'=>basename($filename), |
|
159
|
|
|
'ext'=>$ext, |
|
160
|
|
|
'size'=>filesize($filename), |
|
161
|
|
|
'path'=>$filename, |
|
162
|
|
|
); |
|
163
|
|
|
$imgInfo = $this->_getImageInfo($filename); |
|
164
|
|
|
if(sizeof($imgInfo)) |
|
165
|
|
|
{ |
|
166
|
|
|
foreach($imgInfo as $k=>$v) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->_imgInfoOrig[$k] = $v; |
|
169
|
|
|
$this->_imgInfoFinal[$k] = $v; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
return true; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Load an image from a string (eg. from a database table) |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $string |
|
181
|
|
|
* @return bool |
|
182
|
|
|
* @access public |
|
183
|
|
|
* @copyright this function originally come from Andy's php |
|
184
|
|
|
*/ |
|
185
|
|
|
function loadImageFromString($string) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->imageFile = $filename; |
|
188
|
|
|
$this->_imgOrig = imagecreatefromstring($string); |
|
189
|
|
|
if (!$this->_imgOrig) { |
|
190
|
|
|
$this->_debug('The image (supplied as a string) could not be created.'); |
|
191
|
|
|
return false; |
|
192
|
|
|
} |
|
193
|
|
|
return true; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Save the modified image |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $filename |
|
201
|
|
|
* @param int $quality |
|
202
|
|
|
* @param string $forcetype |
|
203
|
|
|
* @return bool |
|
204
|
|
|
* @access public |
|
205
|
|
|
* @copyright this function originally come from Andy's php |
|
206
|
|
|
*/ |
|
207
|
|
|
function saveImage($filename, $quality = 90, $forcetype = '') |
|
208
|
|
|
{ |
|
209
|
|
|
if ($this->_imgFinal == null) { |
|
210
|
|
|
$this->_debug('No changes intend to be made.'); |
|
211
|
|
|
return false; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
$ext = ($forcetype == '') ? $this->_getExtension($filename) : strtolower($forcetype); |
|
215
|
|
|
$func = 'image' . ($ext == 'jpg' ? 'jpeg' : $ext); |
|
216
|
|
|
if (!$this->_isSupported($filename, $ext, $func, true)) |
|
217
|
|
|
{ |
|
218
|
|
|
return false; |
|
219
|
|
|
} |
|
220
|
|
|
$saved = false; |
|
221
|
|
|
switch($ext) |
|
222
|
|
|
{ |
|
223
|
|
View Code Duplication |
case 'gif': |
|
224
|
|
|
if ($this->gdInfo['Truecolor Support'] && imageistruecolor($this->_imgFinal)) |
|
225
|
|
|
{ |
|
226
|
|
|
imagetruecolortopalette($this->_imgFinal, false, 255); |
|
227
|
|
|
} |
|
228
|
|
|
case 'png': |
|
229
|
|
|
$saved = $func($this->_imgFinal, $filename); |
|
230
|
|
|
break; |
|
231
|
|
|
case 'jpg': |
|
232
|
|
|
$saved = $func($this->_imgFinal, $filename, $quality); |
|
233
|
|
|
break; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
if ($saved === false) |
|
237
|
|
|
{ |
|
238
|
|
|
$this->_debug("The image could not be saved to the '$filename' file as the file type '$ext' using the '$func' function."); |
|
239
|
|
|
return false; |
|
240
|
|
|
}else |
|
241
|
|
|
{ |
|
242
|
|
|
$this->_imgInfoFinal['size'] = @filesize($filename); |
|
243
|
|
|
@chmod($filename, intval($this->chmod, 8)); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return true; |
|
247
|
|
|
} |
|
248
|
|
|
/** |
|
249
|
|
|
* Shows the masked image without any saving |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $type |
|
252
|
|
|
* @param int $quality |
|
253
|
|
|
* @return bool |
|
254
|
|
|
* @access public |
|
255
|
|
|
* @copyright this function originally come from Andy's php |
|
256
|
|
|
*/ |
|
257
|
|
|
function showImage($type = '', $quality = '') |
|
258
|
|
|
{ |
|
259
|
|
|
if ($this->_imgFinal == null) { |
|
260
|
|
|
$this->_debug('There is no cropped image to show.'); |
|
261
|
|
|
return false; |
|
262
|
|
|
} |
|
263
|
|
|
$type = (!empty($type)?$type:$this->_imgInfoOrig['ext']); |
|
264
|
|
|
$quality = (!empty($quality)?$quality:$this->_imgQuality); |
|
265
|
|
|
|
|
266
|
|
|
$type = strtolower($type); |
|
267
|
|
|
$func = 'image' . ($type == 'jpg' ? 'jpeg' : $type); |
|
268
|
|
|
$head = 'image/' . ($type == 'jpg' ? 'jpeg' : $type); |
|
269
|
|
|
|
|
270
|
|
|
if (!$this->_isSupported('[showing file]', $type, $func, false)) { |
|
271
|
|
|
return false; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
header("Content-type: $head"); |
|
275
|
|
|
switch($type) |
|
276
|
|
|
{ |
|
277
|
|
View Code Duplication |
case 'gif': |
|
278
|
|
|
if ($this->gdInfo['Truecolor Support'] && imageistruecolor($this->_imgFinal)) |
|
279
|
|
|
{ |
|
280
|
|
|
@imagetruecolortopalette($this->_imgFinal, false, 255); |
|
281
|
|
|
} |
|
282
|
|
|
case 'png': |
|
283
|
|
|
$func($this->_imgFinal); |
|
284
|
|
|
break; |
|
285
|
|
|
case 'jpg': |
|
286
|
|
|
$func($this->_imgFinal, '', $quality); |
|
287
|
|
|
break; |
|
288
|
|
|
} |
|
289
|
|
|
return true; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Used for cropping image |
|
294
|
|
|
* |
|
295
|
|
|
* @param int $dst_x |
|
296
|
|
|
* @param int $dst_y |
|
297
|
|
|
* @param int $dst_w |
|
298
|
|
|
* @param int $dst_h |
|
299
|
|
|
* @return bool |
|
300
|
|
|
* @access public |
|
301
|
|
|
* @copyright this function originally come from Andy's php |
|
302
|
|
|
*/ |
|
303
|
|
|
function crop($dst_x, $dst_y, $dst_w, $dst_h) |
|
304
|
|
|
{ |
|
305
|
|
|
if ($this->_imgOrig == null) { |
|
306
|
|
|
$this->_debug('The original image has not been loaded.'); |
|
307
|
|
|
return false; |
|
308
|
|
|
} |
|
309
|
|
|
if (($dst_w <= 0) || ($dst_h <= 0)) { |
|
310
|
|
|
$this->_debug('The image could not be cropped because the size given is not valid.'); |
|
311
|
|
|
return false; |
|
312
|
|
|
} |
|
313
|
|
|
if (($dst_w > imagesx($this->_imgOrig)) || ($dst_h > imagesy($this->_imgOrig))) { |
|
314
|
|
|
$this->_debug('The image could not be cropped because the size given is larger than the original image.'); |
|
315
|
|
|
return false; |
|
316
|
|
|
} |
|
317
|
|
|
$this->_createFinalImageHandler($dst_w, $dst_h); |
|
318
|
|
|
if ($this->gdInfo['Truecolor Support']) |
|
319
|
|
|
{ |
|
320
|
|
View Code Duplication |
if(!@imagecopyresampled($this->_imgFinal, $this->_imgOrig, 0, 0, $dst_x, $dst_y, $dst_w, $dst_h, $dst_w, $dst_h)) |
|
321
|
|
|
{ |
|
322
|
|
|
$this->_debug('Unable crop the image.'); |
|
323
|
|
|
return false; |
|
324
|
|
|
} |
|
325
|
|
View Code Duplication |
} else |
|
326
|
|
|
{ |
|
327
|
|
|
if(!@imagecopyresized($this->_imgFinal, $this->_imgOrig, 0, 0, $dst_x, $dst_y, $dst_w, $dst_h, $dst_w, $dst_h)) |
|
328
|
|
|
{ |
|
329
|
|
|
$this->_debug('Unable crop the image.'); |
|
330
|
|
|
return false; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
} |
|
334
|
|
|
$this->_imgInfoFinal['width'] = $dst_w; |
|
335
|
|
|
$this->_imgInfoFinal['height'] = $dst_h; |
|
336
|
|
|
return true; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Resize the Image in the X and/or Y direction |
|
342
|
|
|
* If either is 0 it will be scaled proportionally |
|
343
|
|
|
* |
|
344
|
|
|
* @access public |
|
345
|
|
|
* |
|
346
|
|
|
* @param mixed $new_x |
|
347
|
|
|
* @param mixed $new_y |
|
348
|
|
|
* @param boolean $constraint keep to resize the image proportionally |
|
349
|
|
|
* @param boolean $unchangeIfsmaller keep the orignial size if the orignial smaller than the new size |
|
350
|
|
|
* |
|
351
|
|
|
* |
|
352
|
|
|
* @return mixed none or PEAR_error |
|
353
|
|
|
*/ |
|
354
|
|
|
function resize( $new_x, $new_y, $constraint= false, $unchangeIfsmaller=false) |
|
355
|
|
|
{ |
|
356
|
|
|
if(!$this->_imgOrig) |
|
357
|
|
|
{ |
|
358
|
|
|
$this->_debug('No image fould.'); |
|
359
|
|
|
return false; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
$new_x = intval($new_x); |
|
363
|
|
|
$new_y = intval($new_y); |
|
364
|
|
|
if($new_x <=0 || $new_y <= 0) |
|
365
|
|
|
{ |
|
366
|
|
|
$this->_debug('either of new width or height can be zeor or less.'); |
|
367
|
|
|
}else |
|
368
|
|
|
{ |
|
369
|
|
|
|
|
370
|
|
|
if($constraint) |
|
371
|
|
|
{ |
|
372
|
|
|
if($new_x < 1 && $new_y < 1) |
|
373
|
|
|
{ |
|
374
|
|
|
$new_x = $this->_imgInfoOrig['width']; |
|
375
|
|
|
$new_y = $this->_imgInfoOrig['height']; |
|
376
|
|
|
}elseif($new_x < 1) |
|
377
|
|
|
{ |
|
378
|
|
|
$new_x = floor($new_y / $this->_imgInfoOrig['height'] * $this->_imgInfoOrig['width']); |
|
379
|
|
|
|
|
380
|
|
|
}elseif($new_y < 1) |
|
381
|
|
|
{ |
|
382
|
|
|
$new_y = floor($new_x / $this->_imgInfoOrig['width'] * $this->_imgInfoOrig['height']); |
|
383
|
|
|
}else |
|
384
|
|
|
{ |
|
385
|
|
|
$scale = min($new_x/$this->_imgInfoOrig['width'], $new_y/$this->_imgInfoOrig['height']) ; |
|
386
|
|
|
$new_x = floor($scale*$this->_imgInfoOrig['width']); |
|
387
|
|
|
$new_y = floor($scale*$this->_imgInfoOrig['height']); |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
if($unchangeIfsmaller) |
|
391
|
|
|
{ |
|
392
|
|
|
if($this->_imgInfoOrig['width'] < $new_x && $this->_imgInfoOrig['height'] < $new_y ) |
|
393
|
|
|
{ |
|
394
|
|
|
$new_x = $this->_imgInfoOrig['width']; |
|
395
|
|
|
$new_y = $this->_imgInfoOrig['height']; |
|
396
|
|
|
} |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
|
|
400
|
|
|
|
|
401
|
|
|
if(is_null($this->_imgOrig)) |
|
402
|
|
|
{ |
|
403
|
|
|
$this->loadImage($filePath); |
|
404
|
|
|
} |
|
405
|
|
|
if(sizeof($this->_errors) == 0) |
|
406
|
|
|
{ |
|
407
|
|
|
return $this->_resize($new_x, $new_y); |
|
408
|
|
|
} |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
return false; |
|
412
|
|
|
|
|
413
|
|
|
} // End resize |
|
414
|
|
|
/** |
|
415
|
|
|
* resize the image and return the thumbnail image details array("width"=>, "height"=>, "name") |
|
416
|
|
|
* |
|
417
|
|
|
* @param string $fileName |
|
418
|
|
|
* @param int $new_x the thumbnail width |
|
419
|
|
|
* @param int $new_y the thumbnail height |
|
420
|
|
|
* @param string $mode can be save, view and both |
|
421
|
|
|
* @return unknown |
|
422
|
|
|
*/ |
|
423
|
|
|
function _resize( $new_x, $new_y) |
|
424
|
|
|
{ |
|
425
|
|
|
$this->_createFinalImageHandler($new_x, $new_y); |
|
426
|
|
|
// hacks fot transparency of png24 files |
|
427
|
|
|
if ($this->_imgInfoOrig['type'] == 'png') |
|
428
|
|
|
{ |
|
429
|
|
|
@imagealphablending($this->_imgFinal, false); |
|
430
|
|
View Code Duplication |
if(function_exists('ImageCopyResampled')) |
|
431
|
|
|
{ |
|
432
|
|
|
@imagecopyresampled($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $new_x, $new_y, $this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']); |
|
433
|
|
|
} else { |
|
434
|
|
|
@imagecopyresized($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $new_x, $new_y, $this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']); |
|
435
|
|
|
} |
|
436
|
|
|
@imagesavealpha($this->_imgFinal, true); |
|
437
|
|
|
|
|
438
|
|
View Code Duplication |
}else |
|
439
|
|
|
{//for the rest image |
|
440
|
|
|
if(function_exists('ImageCopyResampled')) |
|
441
|
|
|
{ |
|
442
|
|
|
@imagecopyresampled($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $new_x, $new_y, $this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']); |
|
443
|
|
|
} else { |
|
444
|
|
|
@imagecopyresized($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $new_x, $new_y, $this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']); |
|
445
|
|
|
} |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
|
|
449
|
|
|
$this->_imgInfoFinal['width'] = $new_x; |
|
450
|
|
|
$this->_imgInfoFinal['height'] = $new_y; |
|
451
|
|
|
$this->_imgInfoFinal['name'] = basename($this->_imgInfoOrig['name']); |
|
452
|
|
|
$this->_imgInfoFinal['path'] = $this->_imgInfoOrig['path']; |
|
453
|
|
|
if($this->_imgFinal) |
|
454
|
|
|
{ |
|
455
|
|
|
return true; |
|
456
|
|
|
}else |
|
457
|
|
|
{ |
|
458
|
|
|
$this->_debug('Unable to resize the image on the fly.'); |
|
459
|
|
|
return false; |
|
460
|
|
|
|
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
} |
|
464
|
|
|
/** |
|
465
|
|
|
* Get the extension of a file name |
|
466
|
|
|
* |
|
467
|
|
|
* @param string $file |
|
468
|
|
|
* @return string |
|
469
|
|
|
* @copyright this function originally come from Andy's php |
|
470
|
|
|
*/ |
|
471
|
|
|
function _getExtension($file) |
|
472
|
|
|
{ |
|
473
|
|
|
$ext = ''; |
|
474
|
|
|
if (strrpos($file, '.')) { |
|
475
|
|
|
$ext = strtolower(substr($file, (strrpos($file, '.') ? strrpos($file, '.') + 1 : strlen($file)), strlen($file))); |
|
476
|
|
|
} |
|
477
|
|
|
return $ext; |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
/** |
|
481
|
|
|
* Validate whether image reading/writing routines are valid. |
|
482
|
|
|
* |
|
483
|
|
|
* @param string $filename |
|
484
|
|
|
* @param string $extension |
|
485
|
|
|
* @param string $function |
|
486
|
|
|
* @param bool $write |
|
487
|
|
|
* @return bool |
|
488
|
|
|
* @access private |
|
489
|
|
|
* @copyright this function originally come from Andy's php |
|
490
|
|
|
*/ |
|
491
|
|
|
function _isSupported($filename, $extension, $function, $write = false) |
|
492
|
|
|
{ |
|
493
|
|
|
|
|
494
|
|
|
$giftype = ($write) ? ' Create Support' : ' Read Support'; |
|
495
|
|
|
$support = strtoupper($extension) . ($extension == 'gif' ? $giftype : ' Support'); |
|
496
|
|
|
|
|
497
|
|
|
if (isset($this->gdInfo['JPG Support']) && ($extension=='jpg' || $extension=='jpeg')) |
|
498
|
|
|
{ |
|
499
|
|
|
$extension='jpg'; |
|
500
|
|
|
}else if (isset($this->gdInfo['JPEG Support']) && ($extension=='jpg' || $extension=='jpeg')) |
|
501
|
|
|
{ |
|
502
|
|
|
$extension='jpeg'; |
|
503
|
|
|
} |
|
504
|
|
|
if (!isset($this->gdInfo[$support]) || $this->gdInfo[$support] == false) { |
|
505
|
|
|
$request = ($write) ? 'saving' : 'reading'; |
|
506
|
|
|
$this->_debug("Support for $request the file type '$extension' cannot be found."); |
|
507
|
|
|
return false; |
|
508
|
|
|
} |
|
509
|
|
|
if (!function_exists($function)) { |
|
510
|
|
|
$request = ($write) ? 'save' : 'read'; |
|
511
|
|
|
$this->_debug("The '$function' function required to $request the '$filename' file cannot be found."); |
|
512
|
|
|
return false; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
return true; |
|
516
|
|
|
} |
|
517
|
|
|
/** |
|
518
|
|
|
* flip image horizotally or vertically |
|
519
|
|
|
* |
|
520
|
|
|
* @param string $direction |
|
521
|
|
|
* @return boolean |
|
522
|
|
|
*/ |
|
523
|
|
|
function flip($direction="horizontal") |
|
524
|
|
|
{ |
|
525
|
|
|
$this->_createFinalImageHandler($this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']); |
|
526
|
|
|
if($direction != "vertical") |
|
527
|
|
|
{ |
|
528
|
|
|
$dst_x = 0; |
|
529
|
|
|
$dst_y = 0; |
|
530
|
|
|
$src_x = $this->_imgInfoOrig['width'] -1; |
|
531
|
|
|
$src_y = 0; |
|
532
|
|
|
$dst_w = $this->_imgInfoOrig['width']; |
|
533
|
|
|
$dst_h = $this->_imgInfoOrig['height']; |
|
534
|
|
|
$src_w = 0 - $this->_imgInfoOrig['width']; |
|
535
|
|
|
$src_h = $this->_imgInfoOrig['height']; |
|
536
|
|
|
|
|
537
|
|
|
}else |
|
538
|
|
|
{ |
|
539
|
|
|
$dst_x = 0; |
|
540
|
|
|
$dst_y = 0; |
|
541
|
|
|
$src_x = 0; |
|
542
|
|
|
$src_y = $this->_imgInfoOrig['height'] - 1; |
|
543
|
|
|
$dst_w = $this->_imgInfoOrig['width']; |
|
544
|
|
|
$dst_h = $this->_imgInfoOrig['height']; |
|
545
|
|
|
$src_w = $this->_imgInfoOrig['width']; |
|
546
|
|
|
$src_h = 0 - $this->_imgInfoOrig['height']; |
|
547
|
|
|
} |
|
548
|
|
|
if(function_exists('ImageCopyResampled')){ |
|
549
|
|
|
imagecopyresampled($this->_imgFinal, $this->_imgOrig, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
|
550
|
|
|
} else { |
|
551
|
|
|
imagecopyresized($this->_imgFinal, $this->_imgOrig, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
|
552
|
|
|
} |
|
553
|
|
|
$this->_imgInfoFinal['width'] = $dst_w; |
|
554
|
|
|
$this->_imgInfoFinal['height'] = $dst_h; |
|
555
|
|
|
$this->_imgInfoFinal['name'] = basename($this->imageFile); |
|
556
|
|
|
$this->_imgInfoFinal['path'] = $this->imageFile; |
|
557
|
|
|
if($this->_imgFinal) |
|
558
|
|
|
{ |
|
559
|
|
|
return true; |
|
560
|
|
|
}else |
|
561
|
|
|
{ |
|
562
|
|
|
$this->_debug('Unable to resize the image on the fly.'); |
|
563
|
|
|
return false; |
|
564
|
|
|
|
|
565
|
|
|
} |
|
566
|
|
|
} |
|
567
|
|
|
/** |
|
568
|
|
|
* flip vertically |
|
569
|
|
|
* |
|
570
|
|
|
* @return boolean |
|
571
|
|
|
*/ |
|
572
|
|
|
function flipVertical() |
|
573
|
|
|
{ |
|
574
|
|
|
return $this->flip('vertical'); |
|
575
|
|
|
} |
|
576
|
|
|
/** |
|
577
|
|
|
* flip horizontal |
|
578
|
|
|
* |
|
579
|
|
|
* @return string |
|
580
|
|
|
*/ |
|
581
|
|
|
function flipHorizontal() |
|
582
|
|
|
{ |
|
583
|
|
|
return $this->flip('horizontal'); |
|
584
|
|
|
} |
|
585
|
|
|
|
|
586
|
|
|
|
|
587
|
|
|
/** |
|
588
|
|
|
* get the GD version information |
|
589
|
|
|
* |
|
590
|
|
|
* @param bool $versionOnly |
|
591
|
|
|
* @return array |
|
592
|
|
|
* @access private |
|
593
|
|
|
* @copyright this function originally come from Andy's php |
|
594
|
|
|
*/ |
|
595
|
|
|
function getGDInfo($versionOnly = false) |
|
596
|
|
|
{ |
|
597
|
|
|
$outputs = array(); |
|
598
|
|
|
if (function_exists('gd_info')) |
|
599
|
|
|
{ |
|
600
|
|
|
$outputs = gd_info(); |
|
601
|
|
|
if(isset($outputs['JPEG Support'])) |
|
602
|
|
|
{ |
|
603
|
|
|
$outputs['JPG Support'] = $outputs['JPEG Support']; |
|
604
|
|
|
}else |
|
605
|
|
|
{ |
|
606
|
|
|
if(isset($outputs['JPG Support'])) |
|
607
|
|
|
{ |
|
608
|
|
|
$outputs['JPEG Support'] = $outputs['JPG Support']; |
|
609
|
|
|
} |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
|
|
|
|
613
|
|
|
} else |
|
614
|
|
|
{ |
|
615
|
|
|
$gd = array( |
|
616
|
|
|
'GD Version' => '', |
|
617
|
|
|
'GIF Read Support' => false, |
|
618
|
|
|
'GIF Create Support' => false, |
|
619
|
|
|
'JPG Support' => false, |
|
620
|
|
|
'JPEG Suppor' => false, |
|
621
|
|
|
'PNG Support' => false, |
|
622
|
|
|
'FreeType Support' => false, |
|
623
|
|
|
'FreeType Linkage' => '', |
|
624
|
|
|
'T1Lib Support' => false, |
|
625
|
|
|
'WBMP Support' => false, |
|
626
|
|
|
'XBM Support' => false |
|
627
|
|
|
); |
|
628
|
|
|
ob_start(); |
|
629
|
|
|
phpinfo(); |
|
630
|
|
|
$buffer = ob_get_contents(); |
|
631
|
|
|
ob_end_clean(); |
|
632
|
|
|
foreach (explode("\n", $buffer) as $line) { |
|
633
|
|
|
$line = array_map('trim', (explode('|', strip_tags(str_replace('</td>', '|', $line))))); |
|
634
|
|
|
if (isset($gd[$line[0]])) { |
|
635
|
|
|
if (strtolower($line[1]) == 'enabled') { |
|
636
|
|
|
$gd[$line[0]] = true; |
|
637
|
|
|
} else { |
|
638
|
|
|
$gd[$line[0]] = $line[1]; |
|
639
|
|
|
} |
|
640
|
|
|
} |
|
641
|
|
|
} |
|
642
|
|
|
$outputs = $gd; |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
if (isset($outputs['JIS-mapped Japanese Font Support'])) { |
|
646
|
|
|
unset($outputs['JIS-mapped Japanese Font Support']); |
|
647
|
|
|
} |
|
648
|
|
|
if (function_exists('imagecreatefromgd')) { |
|
649
|
|
|
$outputs['GD Support'] = true; |
|
650
|
|
|
} |
|
651
|
|
|
if (function_exists('imagecreatefromgd2')) { |
|
652
|
|
|
$outputs['GD2 Support'] = true; |
|
653
|
|
|
} |
|
654
|
|
|
if (preg_match('/^(bundled|2)/', $outputs['GD Version'])) { |
|
655
|
|
|
$outputs['Truecolor Support'] = true; |
|
656
|
|
|
} else { |
|
657
|
|
|
$outputs['Truecolor Support'] = false; |
|
658
|
|
|
} |
|
659
|
|
|
if ($outputs['GD Version'] != '') { |
|
660
|
|
|
$match = array(); |
|
661
|
|
|
if (preg_match('/([0-9\.]+)/', $outputs['GD Version'], $match)) { |
|
662
|
|
|
$foo = explode('.', $match[0]); |
|
663
|
|
|
$outputs['Version'] = array('major' => isset($foo[0])?$foo[0]:'', 'minor' => isset($foo[1])?$foo[1]:'', 'patch' => isset($foo[2])?$foo:""); |
|
664
|
|
|
} |
|
665
|
|
|
} |
|
666
|
|
|
//print_r($outputs); |
|
667
|
|
|
return ($versionOnly) ? $outputs['Version'] : $outputs; |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
/** |
|
671
|
|
|
* Destroy the resources used by the images. |
|
672
|
|
|
* |
|
673
|
|
|
* @param bool $original |
|
674
|
|
|
* @return void |
|
675
|
|
|
* @access public |
|
676
|
|
|
* @copyright this function originally come from Andy's php |
|
677
|
|
|
*/ |
|
678
|
|
|
function DestroyImages($original = true) |
|
679
|
|
|
{ |
|
680
|
|
|
if(!is_null($this->_imgFinal)) |
|
681
|
|
|
{ |
|
682
|
|
|
@imagedestroy($this->_imgFinal); |
|
683
|
|
|
} |
|
684
|
|
|
$this->_imgFinal = null; |
|
685
|
|
|
if ($original && !is_null($this->_imgOrig)) { |
|
686
|
|
|
@imagedestroy($this->_imgOrig); |
|
687
|
|
|
$this->_imgOrig = null; |
|
688
|
|
|
} |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
function getImageInfo($imagePath) |
|
692
|
|
|
{ |
|
693
|
|
|
return $this->_getImageInfo($imagePath); |
|
694
|
|
|
} |
|
695
|
|
|
/** |
|
696
|
|
|
* get image information, e.g. width, height, type |
|
697
|
|
|
* @access public |
|
698
|
|
|
* @return array |
|
699
|
|
|
*/ |
|
700
|
|
|
function _getImageInfo($imagePath) |
|
701
|
|
|
{ |
|
702
|
|
|
$outputs = array(); |
|
703
|
|
|
$imageInfo = @getimagesize($imagePath); |
|
704
|
|
|
if ($imageInfo && is_array($imageInfo)) |
|
705
|
|
|
{ |
|
706
|
|
|
switch($imageInfo[2]){ |
|
707
|
|
|
case 1: |
|
708
|
|
|
$type = 'gif'; |
|
709
|
|
|
break; |
|
710
|
|
|
case 2: |
|
711
|
|
|
$type = 'jpeg'; |
|
712
|
|
|
break; |
|
713
|
|
|
case 3: |
|
714
|
|
|
$type = 'png'; |
|
715
|
|
|
break; |
|
716
|
|
|
case 4: |
|
717
|
|
|
$type = 'swf'; |
|
718
|
|
|
break; |
|
719
|
|
|
case 5: |
|
720
|
|
|
$type = 'psd'; |
|
721
|
|
|
case 6: |
|
722
|
|
|
$type = 'bmp'; |
|
723
|
|
|
case 7: |
|
724
|
|
|
case 8: |
|
725
|
|
|
$type = 'tiff'; |
|
726
|
|
|
default: |
|
727
|
|
|
$type = ''; |
|
728
|
|
|
} |
|
729
|
|
|
$outputs['width'] = $imageInfo[0]; |
|
730
|
|
|
$outputs['height'] = $imageInfo[1]; |
|
731
|
|
|
$outputs['type'] = $type; |
|
732
|
|
|
$outputs['ext'] = $this->_getExtension($imagePath); |
|
733
|
|
|
} else { |
|
734
|
|
|
$this->_debug('Unable locate the image or read images information.'); |
|
735
|
|
|
} |
|
736
|
|
|
return $outputs; |
|
737
|
|
|
|
|
738
|
|
|
} |
|
739
|
|
|
function rotate($angle, $bgColor=0) |
|
740
|
|
|
{ |
|
741
|
|
|
$angle = intval($angle) -360; |
|
742
|
|
|
while($angle <0) |
|
743
|
|
|
{ |
|
744
|
|
|
$angle += 360; |
|
745
|
|
|
} |
|
746
|
|
|
|
|
747
|
|
|
|
|
748
|
|
|
if($this->_imgFinal = imagerotate($this->_imgOrig, $angle, 0)) |
|
|
|
|
|
|
749
|
|
|
{ |
|
750
|
|
|
return true; |
|
751
|
|
|
}else |
|
752
|
|
|
{ |
|
753
|
|
|
return false; |
|
754
|
|
|
} |
|
755
|
|
|
|
|
756
|
|
|
|
|
757
|
|
|
} |
|
758
|
|
|
/** |
|
759
|
|
|
* get the original image info |
|
760
|
|
|
* |
|
761
|
|
|
* @return array |
|
762
|
|
|
*/ |
|
763
|
|
|
function getOriginalImageInfo() |
|
764
|
|
|
{ |
|
765
|
|
|
return $this->_imgInfoOrig; |
|
766
|
|
|
} |
|
767
|
|
|
/** |
|
768
|
|
|
* return the final image info |
|
769
|
|
|
* |
|
770
|
|
|
* @return array |
|
771
|
|
|
*/ |
|
772
|
|
|
function getFinalImageInfo() |
|
773
|
|
|
{ |
|
774
|
|
|
if($this->_imgInfoFinal['width'] == '') |
|
775
|
|
|
{ |
|
776
|
|
|
if(is_null($this->_imgFinal)) |
|
777
|
|
|
{ |
|
778
|
|
|
$this->_imgInfoFinal = $this->_imgInfoOrig; |
|
779
|
|
|
}else |
|
780
|
|
|
{ |
|
781
|
|
|
$this->_imgInfoFinal['width'] = @imagesx($this->_imgFinal); |
|
782
|
|
|
$this->_imgInfoFinal['height'] = @imagesy($this->_imgFinal); |
|
783
|
|
|
} |
|
784
|
|
|
} |
|
785
|
|
|
return $this->_imgInfoFinal; |
|
786
|
|
|
} |
|
787
|
|
|
|
|
788
|
|
|
/** |
|
789
|
|
|
* create final image handler |
|
790
|
|
|
* |
|
791
|
|
|
* @access private |
|
792
|
|
|
* @param $dst_w width |
|
793
|
|
|
* @param $dst_h height |
|
794
|
|
|
* @return boolean |
|
795
|
|
|
* @copyright original from noname at nivelzero dot ro |
|
796
|
|
|
*/ |
|
797
|
|
|
function _createFinalImageHandler($dst_w, $dst_h) |
|
798
|
|
|
{ |
|
799
|
|
|
if(function_exists('ImageCreateTrueColor')) |
|
800
|
|
|
{ |
|
801
|
|
|
$this->_imgFinal = @imagecreatetruecolor($dst_w,$dst_h); |
|
802
|
|
|
} else { |
|
803
|
|
|
$this->_imgFinal = @imagecreate($dst_w,$dst_h); |
|
804
|
|
|
} |
|
805
|
|
|
if (!is_null($this->transparentColorRed) && !is_null($this->transparentColorGreen) && !is_null($this->transparentColorBlue)) { |
|
806
|
|
|
|
|
807
|
|
|
$transparent = @imagecolorallocate($targetImageIdentifier, $this->transparentColorRed, $this->transparentColorGreen, $this->transparentColorBlue); |
|
808
|
|
|
@imagefilledrectangle($this->_imgFinal, 0, 0, $dst_w, $dst_h, $transparent); |
|
809
|
|
|
@imagecolortransparent($this->_imgFinal, $transparent); |
|
810
|
|
|
} |
|
811
|
|
|
|
|
812
|
|
|
} |
|
813
|
|
|
} |
|
814
|
|
|
|
|
815
|
|
|
?> |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: