1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2011 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | // +-----------------------------------------------------------------------+ |
---|
25 | // | Image Interface | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | |
---|
28 | // Define all needed methods for image class |
---|
29 | interface imageInterface |
---|
30 | { |
---|
31 | function get_width(); |
---|
32 | |
---|
33 | function get_height(); |
---|
34 | |
---|
35 | function set_compression_quality($quality); |
---|
36 | |
---|
37 | function crop($width, $height, $x, $y); |
---|
38 | |
---|
39 | function strip(); |
---|
40 | |
---|
41 | function rotate($rotation); |
---|
42 | |
---|
43 | function resize($width, $height); |
---|
44 | |
---|
45 | function write($destination_filepath); |
---|
46 | } |
---|
47 | |
---|
48 | // +-----------------------------------------------------------------------+ |
---|
49 | // | Main Image Class | |
---|
50 | // +-----------------------------------------------------------------------+ |
---|
51 | |
---|
52 | class pwg_image |
---|
53 | { |
---|
54 | var $image; |
---|
55 | var $library = ''; |
---|
56 | var $source_filepath = ''; |
---|
57 | |
---|
58 | function __construct($source_filepath, $library=null) |
---|
59 | { |
---|
60 | $this->source_filepath = $source_filepath; |
---|
61 | |
---|
62 | trigger_action('load_image_library', array(&$this) ); |
---|
63 | |
---|
64 | if (is_object($this->image)) |
---|
65 | { |
---|
66 | return; // A plugin may have load its own library |
---|
67 | } |
---|
68 | |
---|
69 | $extension = strtolower(get_extension($source_filepath)); |
---|
70 | |
---|
71 | if (!in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) |
---|
72 | { |
---|
73 | die('[Image] unsupported file extension'); |
---|
74 | } |
---|
75 | |
---|
76 | if (!($this->library = self::get_library($library, $extension))) |
---|
77 | { |
---|
78 | die('No image library available on your server.'); |
---|
79 | } |
---|
80 | |
---|
81 | $class = 'image_'.$this->library; |
---|
82 | $this->image = new $class($source_filepath); |
---|
83 | } |
---|
84 | |
---|
85 | // Unknow methods will be redirected to image object |
---|
86 | function __call($method, $arguments) |
---|
87 | { |
---|
88 | return call_user_func_array(array($this->image, $method), $arguments); |
---|
89 | } |
---|
90 | |
---|
91 | // Piwigo resize function |
---|
92 | function pwg_resize($destination_filepath, $max_width, $max_height, $quality, $automatic_rotation=true, $strip_metadata=false, $crop=false, $follow_orientation=true) |
---|
93 | { |
---|
94 | $starttime = get_moment(); |
---|
95 | |
---|
96 | // width/height |
---|
97 | $source_width = $this->image->get_width(); |
---|
98 | $source_height = $this->image->get_height(); |
---|
99 | |
---|
100 | // Crop image |
---|
101 | if ($crop) |
---|
102 | { |
---|
103 | $x = 0; |
---|
104 | $y = 0; |
---|
105 | |
---|
106 | if ($source_width < $source_height and $follow_orientation) |
---|
107 | { |
---|
108 | list($max_width, $max_height) = array($max_height, $max_width); |
---|
109 | } |
---|
110 | |
---|
111 | $img_ratio = $source_width / $source_height; |
---|
112 | $dest_ratio = $max_width / $max_height; |
---|
113 | |
---|
114 | if($dest_ratio > $img_ratio) |
---|
115 | { |
---|
116 | $destHeight = round($source_width * $max_height / $max_width); |
---|
117 | $y = round(($source_height - $destHeight) / 2 ); |
---|
118 | $source_height = $destHeight; |
---|
119 | } |
---|
120 | elseif ($dest_ratio < $img_ratio) |
---|
121 | { |
---|
122 | $destWidth = round($source_height * $max_width / $max_height); |
---|
123 | $x = round(($source_width - $destWidth) / 2 ); |
---|
124 | $source_width = $destWidth; |
---|
125 | } |
---|
126 | |
---|
127 | $this->image->crop($source_width, $source_height, $x, $y); |
---|
128 | } |
---|
129 | |
---|
130 | $rotation = null; |
---|
131 | if ($automatic_rotation) |
---|
132 | { |
---|
133 | $rotation = self::get_rotation_angle($this->source_filepath); |
---|
134 | } |
---|
135 | $resize_dimensions = self::get_resize_dimensions($source_width, $source_height, $max_width, $max_height, $rotation); |
---|
136 | |
---|
137 | // testing on height is useless in theory: if width is unchanged, there |
---|
138 | // should be no resize, because width/height ratio is not modified. |
---|
139 | if ($resize_dimensions['width'] == $source_width and $resize_dimensions['height'] == $source_height) |
---|
140 | { |
---|
141 | // the image doesn't need any resize! We just copy it to the destination |
---|
142 | copy($this->source_filepath, $destination_filepath); |
---|
143 | return $this->get_resize_result($destination_filepath, $resize_dimensions['width'], $resize_dimensions['height'], $starttime); |
---|
144 | } |
---|
145 | |
---|
146 | $this->image->set_compression_quality($quality); |
---|
147 | |
---|
148 | if ($strip_metadata) |
---|
149 | { |
---|
150 | // we save a few kilobytes. For example a thumbnail with metadata weights 25KB, without metadata 7KB. |
---|
151 | $this->image->strip(); |
---|
152 | } |
---|
153 | |
---|
154 | $this->image->resize($resize_dimensions['width'], $resize_dimensions['height']); |
---|
155 | |
---|
156 | if (isset($rotation)) |
---|
157 | { |
---|
158 | $this->image->rotate($rotation); |
---|
159 | } |
---|
160 | |
---|
161 | $this->image->write($destination_filepath); |
---|
162 | |
---|
163 | // everything should be OK if we are here! |
---|
164 | return $this->get_resize_result($destination_filepath, $resize_dimensions['width'], $resize_dimensions['height'], $starttime); |
---|
165 | } |
---|
166 | |
---|
167 | static function get_resize_dimensions($width, $height, $max_width, $max_height, $rotation=null) |
---|
168 | { |
---|
169 | $rotate_for_dimensions = false; |
---|
170 | if (isset($rotation) and in_array(abs($rotation), array(90, 270))) |
---|
171 | { |
---|
172 | $rotate_for_dimensions = true; |
---|
173 | } |
---|
174 | |
---|
175 | if ($rotate_for_dimensions) |
---|
176 | { |
---|
177 | list($width, $height) = array($height, $width); |
---|
178 | } |
---|
179 | |
---|
180 | $ratio_width = $width / $max_width; |
---|
181 | $ratio_height = $height / $max_height; |
---|
182 | $destination_width = $width; |
---|
183 | $destination_height = $height; |
---|
184 | |
---|
185 | // maximal size exceeded ? |
---|
186 | if ($ratio_width > 1 or $ratio_height > 1) |
---|
187 | { |
---|
188 | if ($ratio_width < $ratio_height) |
---|
189 | { |
---|
190 | $destination_width = round($width / $ratio_height); |
---|
191 | $destination_height = $max_height; |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | $destination_width = $max_width; |
---|
196 | $destination_height = round($height / $ratio_width); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | if ($rotate_for_dimensions) |
---|
201 | { |
---|
202 | list($destination_width, $destination_height) = array($destination_height, $destination_width); |
---|
203 | } |
---|
204 | |
---|
205 | return array( |
---|
206 | 'width' => $destination_width, |
---|
207 | 'height'=> $destination_height, |
---|
208 | ); |
---|
209 | } |
---|
210 | |
---|
211 | static function get_rotation_angle($source_filepath) |
---|
212 | { |
---|
213 | list($width, $height, $type) = getimagesize($source_filepath); |
---|
214 | if (IMAGETYPE_JPEG != $type) |
---|
215 | { |
---|
216 | return null; |
---|
217 | } |
---|
218 | |
---|
219 | if (!function_exists('exif_read_data')) |
---|
220 | { |
---|
221 | return null; |
---|
222 | } |
---|
223 | |
---|
224 | $rotation = null; |
---|
225 | |
---|
226 | $exif = exif_read_data($source_filepath); |
---|
227 | |
---|
228 | if (isset($exif['Orientation']) and preg_match('/^\s*(\d)/', $exif['Orientation'], $matches)) |
---|
229 | { |
---|
230 | $orientation = $matches[1]; |
---|
231 | if (in_array($orientation, array(3, 4))) |
---|
232 | { |
---|
233 | $rotation = 180; |
---|
234 | } |
---|
235 | elseif (in_array($orientation, array(5, 6))) |
---|
236 | { |
---|
237 | $rotation = 270; |
---|
238 | } |
---|
239 | elseif (in_array($orientation, array(7, 8))) |
---|
240 | { |
---|
241 | $rotation = 90; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | return $rotation; |
---|
246 | } |
---|
247 | |
---|
248 | private function get_resize_result($destination_filepath, $width, $height, $time=null) |
---|
249 | { |
---|
250 | return array( |
---|
251 | 'source' => $this->source_filepath, |
---|
252 | 'destination' => $destination_filepath, |
---|
253 | 'width' => $width, |
---|
254 | 'height' => $height, |
---|
255 | 'size' => floor(filesize($destination_filepath) / 1024).' KB', |
---|
256 | 'time' => $time ? number_format((get_moment() - $time) * 1000, 2, '.', ' ').' ms' : null, |
---|
257 | 'library' => $this->library, |
---|
258 | ); |
---|
259 | } |
---|
260 | |
---|
261 | static function is_imagick() |
---|
262 | { |
---|
263 | return extension_loaded('imagick'); |
---|
264 | } |
---|
265 | |
---|
266 | static function is_ext_imagick() |
---|
267 | { |
---|
268 | global $conf; |
---|
269 | |
---|
270 | if (!function_exists('exec')) |
---|
271 | { |
---|
272 | return false; |
---|
273 | } |
---|
274 | @exec($conf['ext_imagick_dir'].'convert -version', $returnarray); |
---|
275 | if (is_array($returnarray) and !empty($returnarray[0]) and preg_match('/ImageMagick/i', $returnarray[0])) |
---|
276 | { |
---|
277 | return true; |
---|
278 | } |
---|
279 | return false; |
---|
280 | } |
---|
281 | |
---|
282 | static function is_gd() |
---|
283 | { |
---|
284 | return function_exists('gd_info'); |
---|
285 | } |
---|
286 | |
---|
287 | static function get_library($library=null, $extension=null) |
---|
288 | { |
---|
289 | global $conf; |
---|
290 | |
---|
291 | if (is_null($library)) |
---|
292 | { |
---|
293 | $library = $conf['graphics_library']; |
---|
294 | } |
---|
295 | |
---|
296 | // Choose image library |
---|
297 | switch (strtolower($library)) |
---|
298 | { |
---|
299 | case 'auto': |
---|
300 | case 'imagick': |
---|
301 | if ($extension != 'gif' and self::is_imagick()) |
---|
302 | { |
---|
303 | return 'imagick'; |
---|
304 | } |
---|
305 | case 'ext_imagick': |
---|
306 | if ($extension != 'gif' and self::is_ext_imagick()) |
---|
307 | { |
---|
308 | return 'ext_imagick'; |
---|
309 | } |
---|
310 | case 'gd': |
---|
311 | if (self::is_gd()) |
---|
312 | { |
---|
313 | return 'gd'; |
---|
314 | } |
---|
315 | default: |
---|
316 | if ($library != 'auto') |
---|
317 | { |
---|
318 | // Requested library not available. Try another library |
---|
319 | return self::get_library('auto', $extension); |
---|
320 | } |
---|
321 | } |
---|
322 | return false; |
---|
323 | } |
---|
324 | |
---|
325 | function destroy() |
---|
326 | { |
---|
327 | if (method_exists($this->image, 'destroy')) |
---|
328 | { |
---|
329 | return $this->image->destroy(); |
---|
330 | } |
---|
331 | return true; |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | // +-----------------------------------------------------------------------+ |
---|
336 | // | Class for Imagick extension | |
---|
337 | // +-----------------------------------------------------------------------+ |
---|
338 | |
---|
339 | class image_imagick implements imageInterface |
---|
340 | { |
---|
341 | var $image; |
---|
342 | |
---|
343 | function __construct($source_filepath) |
---|
344 | { |
---|
345 | // A bug cause that Imagick class can not be extended |
---|
346 | $this->image = new Imagick($source_filepath); |
---|
347 | } |
---|
348 | |
---|
349 | function get_width() |
---|
350 | { |
---|
351 | return $this->image->getImageWidth(); |
---|
352 | } |
---|
353 | |
---|
354 | function get_height() |
---|
355 | { |
---|
356 | return $this->image->getImageHeight(); |
---|
357 | } |
---|
358 | |
---|
359 | function set_compression_quality($quality) |
---|
360 | { |
---|
361 | return $this->image->setImageCompressionQuality($quality); |
---|
362 | } |
---|
363 | |
---|
364 | function crop($width, $height, $x, $y) |
---|
365 | { |
---|
366 | return $this->image->cropImage($width, $height, $x, $y); |
---|
367 | } |
---|
368 | |
---|
369 | function strip() |
---|
370 | { |
---|
371 | return $this->image->stripImage(); |
---|
372 | } |
---|
373 | |
---|
374 | function rotate($rotation) |
---|
375 | { |
---|
376 | $this->image->rotateImage(new ImagickPixel(), -$rotation); |
---|
377 | $this->image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); |
---|
378 | return true; |
---|
379 | } |
---|
380 | |
---|
381 | function resize($width, $height) |
---|
382 | { |
---|
383 | $this->image->setInterlaceScheme(Imagick::INTERLACE_LINE); |
---|
384 | return $this->image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 0.9); |
---|
385 | } |
---|
386 | |
---|
387 | function write($destination_filepath) |
---|
388 | { |
---|
389 | return $this->image->writeImage($destination_filepath); |
---|
390 | } |
---|
391 | } |
---|
392 | |
---|
393 | // +-----------------------------------------------------------------------+ |
---|
394 | // | Class for ImageMagick external installation | |
---|
395 | // +-----------------------------------------------------------------------+ |
---|
396 | |
---|
397 | class image_ext_imagick implements imageInterface |
---|
398 | { |
---|
399 | var $imagickdir = ''; |
---|
400 | var $source_filepath = ''; |
---|
401 | var $width = ''; |
---|
402 | var $height = ''; |
---|
403 | var $commands = array(); |
---|
404 | |
---|
405 | function __construct($source_filepath, $imagickdir='') |
---|
406 | { |
---|
407 | $this->source_filepath = $source_filepath; |
---|
408 | $this->imagickdir = $imagickdir; |
---|
409 | |
---|
410 | $command = $imagickdir.'identify -format "%wx%h" "'.realpath($source_filepath).'"'; |
---|
411 | @exec($command, $returnarray, $returnvalue); |
---|
412 | if($returnvalue or !preg_match('/^(\d+)x(\d+)$/', $returnarray[0], $match)) |
---|
413 | { |
---|
414 | die("[External ImageMagick] Corrupt image"); |
---|
415 | } |
---|
416 | |
---|
417 | $this->width = $match[1]; |
---|
418 | $this->height = $match[2]; |
---|
419 | } |
---|
420 | |
---|
421 | function add_command($command, $params=null) |
---|
422 | { |
---|
423 | $this->commands[$command] = $params; |
---|
424 | } |
---|
425 | |
---|
426 | function get_width() |
---|
427 | { |
---|
428 | return $this->width; |
---|
429 | } |
---|
430 | |
---|
431 | function get_height() |
---|
432 | { |
---|
433 | return $this->height; |
---|
434 | } |
---|
435 | |
---|
436 | function crop($width, $height, $x, $y) |
---|
437 | { |
---|
438 | $this->add_command('crop', $width.'x'.$height.'+'.$x.'+'.$y); |
---|
439 | return true; |
---|
440 | } |
---|
441 | |
---|
442 | function strip() |
---|
443 | { |
---|
444 | $this->add_command('strip'); |
---|
445 | return true; |
---|
446 | } |
---|
447 | |
---|
448 | function rotate($rotation) |
---|
449 | { |
---|
450 | $this->add_command('rotate', -$rotation); |
---|
451 | $this->add_command('orient', 'top-left'); |
---|
452 | return true; |
---|
453 | } |
---|
454 | |
---|
455 | function set_compression_quality($quality) |
---|
456 | { |
---|
457 | $this->add_command('quality', $quality); |
---|
458 | return true; |
---|
459 | } |
---|
460 | |
---|
461 | function resize($width, $height) |
---|
462 | { |
---|
463 | $this->add_command('interlace', 'line'); |
---|
464 | $this->add_command('filter', 'Lanczos'); |
---|
465 | $this->add_command('resize', $width.'x'.$height.'!'); |
---|
466 | return true; |
---|
467 | } |
---|
468 | |
---|
469 | function write($destination_filepath) |
---|
470 | { |
---|
471 | $exec = $this->imagickdir.'convert'; |
---|
472 | $exec .= ' "'.realpath($this->source_filepath).'"'; |
---|
473 | |
---|
474 | foreach ($this->commands as $command => $params) |
---|
475 | { |
---|
476 | $exec .= ' -'.$command; |
---|
477 | if (!empty($params)) |
---|
478 | { |
---|
479 | $exec .= ' '.$params; |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | $dest = pathinfo($destination_filepath); |
---|
484 | $exec .= ' "'.realpath($dest['dirname']).'/'.$dest['basename'].'"'; |
---|
485 | @exec($exec, $returnarray, $returnvalue); |
---|
486 | return !$returnvalue; |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | // +-----------------------------------------------------------------------+ |
---|
491 | // | Class for GD library | |
---|
492 | // +-----------------------------------------------------------------------+ |
---|
493 | |
---|
494 | class image_gd implements imageInterface |
---|
495 | { |
---|
496 | var $image; |
---|
497 | var $quality = 95; |
---|
498 | |
---|
499 | function __construct($source_filepath) |
---|
500 | { |
---|
501 | $gd_info = gd_info(); |
---|
502 | $extension = strtolower(get_extension($source_filepath)); |
---|
503 | |
---|
504 | if (in_array($extension, array('jpg', 'jpeg'))) |
---|
505 | { |
---|
506 | $this->image = imagecreatefromjpeg($source_filepath); |
---|
507 | } |
---|
508 | else if ($extension == 'png') |
---|
509 | { |
---|
510 | $this->image = imagecreatefrompng($source_filepath); |
---|
511 | } |
---|
512 | elseif ($extension == 'gif' and $gd_info['GIF Read Support'] and $gd_info['GIF Create Support']) |
---|
513 | { |
---|
514 | $this->image = imagecreatefromgif($source_filepath); |
---|
515 | } |
---|
516 | else |
---|
517 | { |
---|
518 | die('[Image GD] unsupported file extension'); |
---|
519 | } |
---|
520 | } |
---|
521 | |
---|
522 | function get_width() |
---|
523 | { |
---|
524 | return imagesx($this->image); |
---|
525 | } |
---|
526 | |
---|
527 | function get_height() |
---|
528 | { |
---|
529 | return imagesy($this->image); |
---|
530 | } |
---|
531 | |
---|
532 | function crop($width, $height, $x, $y) |
---|
533 | { |
---|
534 | $dest = imagecreatetruecolor($width, $height); |
---|
535 | |
---|
536 | imagealphablending($dest, false); |
---|
537 | imagesavealpha($dest, true); |
---|
538 | if (function_exists('imageantialias')) |
---|
539 | { |
---|
540 | imageantialias($dest, true); |
---|
541 | } |
---|
542 | |
---|
543 | $result = imagecopymerge($dest, $this->image, 0, 0, $x, $y, $width, $height, 100); |
---|
544 | |
---|
545 | if ($result !== false) |
---|
546 | { |
---|
547 | imagedestroy($this->image); |
---|
548 | $this->image = $dest; |
---|
549 | } |
---|
550 | else |
---|
551 | { |
---|
552 | imagedestroy($dest); |
---|
553 | } |
---|
554 | return $result; |
---|
555 | } |
---|
556 | |
---|
557 | function strip() |
---|
558 | { |
---|
559 | return true; |
---|
560 | } |
---|
561 | |
---|
562 | function rotate($rotation) |
---|
563 | { |
---|
564 | $dest = imagerotate($this->image, $rotation, 0); |
---|
565 | imagedestroy($this->image); |
---|
566 | $this->image = $dest; |
---|
567 | return true; |
---|
568 | } |
---|
569 | |
---|
570 | function set_compression_quality($quality) |
---|
571 | { |
---|
572 | $this->quality = $quality; |
---|
573 | return true; |
---|
574 | } |
---|
575 | |
---|
576 | function resize($width, $height) |
---|
577 | { |
---|
578 | $dest = imagecreatetruecolor($width, $height); |
---|
579 | |
---|
580 | imagealphablending($dest, false); |
---|
581 | imagesavealpha($dest, true); |
---|
582 | if (function_exists('imageantialias')) |
---|
583 | { |
---|
584 | imageantialias($dest, true); |
---|
585 | } |
---|
586 | |
---|
587 | $result = imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $width, $height, $this->get_width(), $this->get_height()); |
---|
588 | |
---|
589 | if ($result !== false) |
---|
590 | { |
---|
591 | imagedestroy($this->image); |
---|
592 | $this->image = $dest; |
---|
593 | } |
---|
594 | else |
---|
595 | { |
---|
596 | imagedestroy($dest); |
---|
597 | } |
---|
598 | return $result; |
---|
599 | } |
---|
600 | |
---|
601 | function write($destination_filepath) |
---|
602 | { |
---|
603 | $extension = strtolower(get_extension($destination_filepath)); |
---|
604 | |
---|
605 | if ($extension == 'png') |
---|
606 | { |
---|
607 | imagepng($this->image, $destination_filepath); |
---|
608 | } |
---|
609 | elseif ($extension == 'gif') |
---|
610 | { |
---|
611 | imagegif($this->image, $destination_filepath); |
---|
612 | } |
---|
613 | else |
---|
614 | { |
---|
615 | imagejpeg($this->image, $destination_filepath, $this->quality); |
---|
616 | } |
---|
617 | } |
---|
618 | |
---|
619 | function destroy() |
---|
620 | { |
---|
621 | imagedestroy($this->image); |
---|
622 | } |
---|
623 | } |
---|
624 | |
---|
625 | ?> |
---|