1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Nip Framework |
5
|
|
|
* |
6
|
|
|
* @category Nip |
7
|
|
|
* @copyright 2009 Nip Framework |
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
9
|
|
|
* @version SVN: $Id: Image.php 193 2009-06-23 23:11:53Z victorstanciu $ |
10
|
|
|
*/ |
11
|
|
View Code Duplication |
class Nip_File_Image extends Nip_File_Handler |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public $extensions = ["jpg", "jpeg", "gif", "png"]; |
15
|
|
|
public $quality = 90; |
16
|
|
|
public $type = 'jpg'; |
17
|
|
|
public $max_width = false; |
18
|
|
|
public $errors = []; |
19
|
|
|
|
20
|
|
|
protected $_resource; |
21
|
|
|
protected $_file; |
22
|
|
|
protected $_upload; |
23
|
|
|
protected $_width; |
24
|
|
|
protected $_height; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array $upload |
28
|
|
|
*/ |
29
|
|
|
public function setResourceFromUpload($upload) |
30
|
|
|
{ |
31
|
|
|
$this->_upload = $upload; |
32
|
|
|
$this->setResourceFromFile($upload['tmp_name']); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $path |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public function setResourceFromFile($path) |
40
|
|
|
{ |
41
|
|
|
$this->_file = $path; |
42
|
|
|
if (file_exists($path)) { |
43
|
|
|
$details = getimagesize($path); |
44
|
|
|
|
45
|
|
|
switch ($details['mime']) { |
46
|
|
|
case 'image/gif': |
47
|
|
|
$this->type = 'gif'; |
48
|
|
|
if (imagetypes() & IMG_GIF) { |
49
|
|
|
$this->_resource = imagecreatefromgif($path); |
50
|
|
|
} |
51
|
|
|
break; |
52
|
|
|
case 'image/jpeg': |
53
|
|
|
$this->type = 'jpg'; |
54
|
|
|
if (imagetypes() & IMG_JPG) { |
55
|
|
|
$this->_resource = imagecreatefromjpeg($path); |
56
|
|
|
} |
57
|
|
|
break; |
58
|
|
|
case 'image/png': |
59
|
|
|
$this->type = 'png'; |
60
|
|
|
if (imagetypes() & IMG_PNG) { |
61
|
|
|
$this->_resource = imagecreatefrompng($path); |
62
|
|
|
} |
63
|
|
|
break; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->getWidth(); |
67
|
|
|
$this->getHeight(); |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} else { |
71
|
|
|
trigger_error("Cannot find file $path", E_USER_ERROR); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
|
|
public function getWidth() |
81
|
|
|
{ |
82
|
|
|
if (!$this->_width && $this->_resource) { |
83
|
|
|
$this->setWidth(imagesx($this->_resource)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->_width; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $width |
91
|
|
|
*/ |
92
|
|
|
public function setWidth($width) |
93
|
|
|
{ |
94
|
|
|
$this->_width = $width; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
public function getHeight() |
101
|
|
|
{ |
102
|
|
|
if (!$this->_height && $this->_resource) { |
103
|
|
|
$this->setHeight(imagesy($this->_resource)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->_height; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param int $height |
111
|
|
|
*/ |
112
|
|
|
public function setHeight($height) |
113
|
|
|
{ |
114
|
|
|
$this->_height = $height; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
*/ |
120
|
|
|
public function setBaseName($name) |
121
|
|
|
{ |
122
|
|
|
$name = $name . '.' . $this->type; |
123
|
|
|
$this->setName($name); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
*/ |
129
|
|
|
public function setName($name) |
130
|
|
|
{ |
131
|
|
|
$this->name = $name; |
132
|
|
|
$this->url = dirname($this->url) . '/' . $this->name; |
133
|
|
|
$this->path = dirname($this->path) . '/' . $this->name; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function save() |
140
|
|
|
{ |
141
|
|
|
if (Nip_File_System::instance()->createDirectory(dirname($this->path))) { |
142
|
|
|
|
143
|
|
|
switch ($this->type) { |
144
|
|
|
case 'png': |
145
|
|
|
if ($this->quality > 9) { |
146
|
|
|
if ($this->quality < 100) { |
147
|
|
|
$this->quality = $this->quality / 10; |
148
|
|
|
} else { |
149
|
|
|
$this->quality = 9; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
$this->quality = abs($this->quality - 9); |
|
|
|
|
153
|
|
|
$this->quality = 0; |
154
|
|
|
|
155
|
|
|
$newImg = imagecreatetruecolor($this->_width, $this->_height); |
156
|
|
|
imagealphablending($newImg, false); |
157
|
|
|
imagesavealpha($newImg, true); |
158
|
|
|
|
159
|
|
|
imagecopyresampled($newImg, $this->_resource, 0, 0, 0, 0, $this->_width, $this->_height, $this->_width, $this->_height); |
160
|
|
|
|
161
|
|
|
$return = imagepng($newImg, $this->path, $this->quality); |
162
|
|
|
break; |
163
|
|
|
case 'jpg': |
164
|
|
|
default: |
165
|
|
|
$return = imagejpeg($this->_resource, $this->path, $this->quality); |
166
|
|
|
break; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($return) { |
170
|
|
|
chmod($this->path, 0777); |
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
$this->errors[] = 'Error saving file'; |
174
|
|
|
} else { |
175
|
|
|
$this->errors[] = 'Error creating directory'; |
176
|
|
|
} |
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function grayscaleFade() |
181
|
|
|
{ |
182
|
|
|
$this->grayscaleFilter(); |
183
|
|
|
imagefilter($this->_resource, IMG_FILTER_BRIGHTNESS, 50); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function grayscaleFilter() |
187
|
|
|
{ |
188
|
|
|
imagefilter($this->_resource, IMG_FILTER_GRAYSCALE); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function resize($max_width = false, $max_height = false) |
192
|
|
|
{ |
193
|
|
|
if (!$max_width) { |
194
|
|
|
if ($this->max_width) { |
195
|
|
|
$max_width = $this->max_width; |
196
|
|
|
} else { |
197
|
|
|
$max_width = $this->getWidth(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (!$max_height) { |
202
|
|
|
if ($this->max_height) { |
|
|
|
|
203
|
|
|
$max_height = $this->max_height; |
|
|
|
|
204
|
|
|
} else { |
205
|
|
|
$max_height = $this->getHeight(); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$ratio = $this->getRatio(); |
210
|
|
|
$target_ratio = $max_width / $max_height; |
211
|
|
|
|
212
|
|
|
if ($ratio > $target_ratio) { |
213
|
|
|
$new_width = $max_width; |
214
|
|
|
$new_height = round($max_width / $ratio); |
215
|
|
|
} else { |
216
|
|
|
$new_height = $max_height; |
217
|
|
|
$new_width = round($max_height * $ratio); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$image = imagecreatetruecolor($new_width, $new_height); |
221
|
|
|
imagealphablending($image, false); |
222
|
|
|
imagesavealpha($image, true); |
223
|
|
|
|
224
|
|
|
imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight()); |
225
|
|
|
|
226
|
|
|
$this->_width = $new_width; |
227
|
|
|
$this->_height = $new_height; |
228
|
|
|
$this->_resource = $image; |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getRatio() |
234
|
|
|
{ |
235
|
|
|
return $this->getWidth() / $this->getHeight(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function cropToCenter($cWidth, $cHeight) |
239
|
|
|
{ |
240
|
|
|
|
241
|
|
|
$this->resizeToLarge($cWidth, $cHeight); |
242
|
|
|
|
243
|
|
|
$width = $this->getWidth(); |
244
|
|
|
$height = $this->getHeight(); |
245
|
|
|
|
246
|
|
|
$x0 = round(abs(($width - $cWidth) / 2), 0); |
247
|
|
|
$y0 = round(abs(($height - $cHeight) / 2), 0); |
248
|
|
|
|
249
|
|
|
$this->crop($x0, $y0, $cWidth, $cHeight, $cWidth, $cHeight); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param bool|int $max_width |
254
|
|
|
* @param bool|int $max_height |
255
|
|
|
* @return $this |
256
|
|
|
*/ |
257
|
|
|
public function resizeToLarge($max_width = false, $max_height = false) |
258
|
|
|
{ |
259
|
|
|
if (!$max_width) { |
260
|
|
|
$max_width = $this->getWidth(); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
if (!$max_height) { |
264
|
|
|
$max_height = $this->getHeight(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
$sourceRatio = $this->getRatio(); |
268
|
|
|
$target_ratio = $max_width / $max_height; |
269
|
|
|
|
270
|
|
|
if ($sourceRatio > $target_ratio) { |
271
|
|
|
$new_height = $max_height; |
272
|
|
|
$new_width = ( int )($max_height * $sourceRatio); |
273
|
|
|
} else { |
274
|
|
|
$new_width = $max_width; |
275
|
|
|
$new_height = ( int )($max_width / $sourceRatio); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$image = imagecreatetruecolor($new_width, $new_height); |
279
|
|
|
imagealphablending($image, false); |
280
|
|
|
imagesavealpha($image, true); |
281
|
|
|
|
282
|
|
|
imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight()); |
283
|
|
|
|
284
|
|
|
$this->_width = $new_width; |
285
|
|
|
$this->_height = $new_height; |
286
|
|
|
$this->_resource = $image; |
287
|
|
|
|
288
|
|
|
return $this; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param $x |
293
|
|
|
* @param $y |
294
|
|
|
* @param $dwidth |
295
|
|
|
* @param $dheight |
296
|
|
|
* @param $swidth |
297
|
|
|
* @param $sheight |
298
|
|
|
*/ |
299
|
|
|
public function crop($x, $y, $dwidth, $dheight, $swidth, $sheight) |
300
|
|
|
{ |
301
|
|
|
$image = imagecreatetruecolor($dwidth, $dheight); |
302
|
|
|
imagealphablending($image, false); |
303
|
|
|
imagesavealpha($image, true); |
304
|
|
|
|
305
|
|
|
imagecopyresampled($image, $this->_resource, |
306
|
|
|
0, 0, |
307
|
|
|
$x, $y, |
308
|
|
|
$dwidth, $dheight, |
309
|
|
|
$swidth, $sheight); |
310
|
|
|
|
311
|
|
|
$this->_width = $dwidth; |
312
|
|
|
$this->_height = $dheight; |
313
|
|
|
$this->_resource = $image; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param int $amount |
318
|
|
|
* @param float $radius |
319
|
|
|
* @param int $threshold |
320
|
|
|
* @return $this |
321
|
|
|
*/ |
322
|
|
|
public function unsharpMask($amount = 80, $radius = 0.5, $threshold = 3) |
323
|
|
|
{ |
324
|
|
|
$img = &$this->_resource; |
325
|
|
|
|
326
|
|
|
if ($amount > 500) { |
327
|
|
|
$amount = 500; |
328
|
|
|
} |
329
|
|
|
$amount = $amount * 0.016; |
330
|
|
|
if ($radius > 50) { |
331
|
|
|
$radius = 50; |
332
|
|
|
} |
333
|
|
|
$radius = $radius * 2; |
334
|
|
|
if ($threshold > 255) { |
335
|
|
|
$threshold = 255; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
$radius = abs(round($radius)); |
339
|
|
|
if ($radius == 0) { |
340
|
|
|
return; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
$w = $this->_width; |
344
|
|
|
$h = $this->_height; |
345
|
|
|
|
346
|
|
|
$imgCanvas = imagecreatetruecolor($w, $h); |
347
|
|
|
$imgBlur = imagecreatetruecolor($w, $h); |
348
|
|
|
|
349
|
|
|
if (function_exists('imageconvolution')) { |
350
|
|
|
$matrix = [[1, 2, 1], [2, 4, 2], [1, 2, 1]]; |
351
|
|
|
imagecopy($imgBlur, $img, 0, 0, 0, 0, $w, $h); |
352
|
|
|
imageconvolution($imgBlur, $matrix, 16, 0); |
353
|
|
|
} else { |
354
|
|
|
for ($i = 0; $i < $radius; $i++) { |
355
|
|
|
imagecopy($imgBlur, $img, 0, 0, 1, 0, $w - 1, $h); |
356
|
|
|
imagecopymerge($imgBlur, $img, 1, 0, 0, 0, $w, $h, 50); |
357
|
|
|
imagecopymerge($imgBlur, $img, 0, 0, 0, 0, $w, $h, 50); |
358
|
|
|
imagecopy($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h); |
359
|
|
|
|
360
|
|
|
imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333); |
361
|
|
|
imagecopymerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 25); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
if ($threshold > 0) { |
366
|
|
|
for ($x = 0; $x < $w - 1; $x++) { |
367
|
|
|
for ($y = 0; $y < $h; $y++) { |
368
|
|
|
|
369
|
|
|
$rgbOrig = ImageColorAt($img, $x, $y); |
370
|
|
|
$rOrig = (($rgbOrig >> 16) & 0xFF); |
371
|
|
|
$gOrig = (($rgbOrig >> 8) & 0xFF); |
372
|
|
|
$bOrig = ($rgbOrig & 0xFF); |
373
|
|
|
|
374
|
|
|
$rgbBlur = ImageColorAt($imgBlur, $x, $y); |
375
|
|
|
|
376
|
|
|
$rBlur = (($rgbBlur >> 16) & 0xFF); |
377
|
|
|
$gBlur = (($rgbBlur >> 8) & 0xFF); |
378
|
|
|
$bBlur = ($rgbBlur & 0xFF); |
379
|
|
|
|
380
|
|
|
$rNew = (abs($rOrig - $rBlur) >= $threshold) ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig; |
381
|
|
|
$gNew = (abs($gOrig - $gBlur) >= $threshold) ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig; |
382
|
|
|
$bNew = (abs($bOrig - $bBlur) >= $threshold) ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig; |
383
|
|
|
|
384
|
|
|
if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) { |
385
|
|
|
$pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew); |
386
|
|
|
ImageSetPixel($img, $x, $y, $pixCol); |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
} else { |
391
|
|
|
for ($x = 0; $x < $w; $x++) { |
392
|
|
|
for ($y = 0; $y < $h; $y++) { |
393
|
|
|
$rgbOrig = ImageColorAt($img, $x, $y); |
394
|
|
|
$rOrig = (($rgbOrig >> 16) & 0xFF); |
395
|
|
|
$gOrig = (($rgbOrig >> 8) & 0xFF); |
396
|
|
|
$bOrig = ($rgbOrig & 0xFF); |
397
|
|
|
|
398
|
|
|
$rgbBlur = ImageColorAt($imgBlur, $x, $y); |
399
|
|
|
|
400
|
|
|
$rBlur = (($rgbBlur >> 16) & 0xFF); |
401
|
|
|
$gBlur = (($rgbBlur >> 8) & 0xFF); |
402
|
|
|
$bBlur = ($rgbBlur & 0xFF); |
403
|
|
|
|
404
|
|
|
$rNew = ($amount * ($rOrig - $rBlur)) + $rOrig; |
405
|
|
|
if ($rNew > 255) { |
406
|
|
|
$rNew = 255; |
407
|
|
|
} elseif ($rNew < 0) { |
408
|
|
|
$rNew = 0; |
409
|
|
|
} |
410
|
|
|
$gNew = ($amount * ($gOrig - $gBlur)) + $gOrig; |
411
|
|
|
if ($gNew > 255) { |
412
|
|
|
$gNew = 255; |
413
|
|
|
} elseif ($gNew < 0) { |
414
|
|
|
$gNew = 0; |
415
|
|
|
} |
416
|
|
|
$bNew = ($amount * ($bOrig - $bBlur)) + $bOrig; |
417
|
|
|
if ($bNew > 255) { |
418
|
|
|
$bNew = 255; |
419
|
|
|
} elseif ($bNew < 0) { |
420
|
|
|
$bNew = 0; |
421
|
|
|
} |
422
|
|
|
$rgbNew = ($rNew << 16) + ($gNew << 8) + $bNew; |
423
|
|
|
ImageSetPixel($img, $x, $y, $rgbNew); |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
imagedestroy($imgCanvas); |
429
|
|
|
imagedestroy($imgBlur); |
430
|
|
|
|
431
|
|
|
return $this; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
/** |
435
|
|
|
* @param Nip_File_Image $image |
436
|
|
|
* @return $this |
437
|
|
|
*/ |
438
|
|
|
public function copyResource(Nip_File_Image $image) |
439
|
|
|
{ |
440
|
|
|
$this->_width = $image->getWidth(); |
441
|
|
|
$this->_height = $image->getHeight(); |
442
|
|
|
$this->_resource = $image->getResource(); |
443
|
|
|
|
444
|
|
|
return $this; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
public function getResource() |
448
|
|
|
{ |
449
|
|
|
return $this->_resource; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
public function setResource($gdImage) |
453
|
|
|
{ |
454
|
|
|
$this->_resource = $gdImage; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* @return mixed |
459
|
|
|
*/ |
460
|
|
|
public function getFile() |
461
|
|
|
{ |
462
|
|
|
return $this->_file; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* @return string |
467
|
|
|
*/ |
468
|
|
|
public function getExtension() |
469
|
|
|
{ |
470
|
|
|
return Nip_File_System::instance()->getExtension($this->path); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.