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