Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/aimogen-pro/res/ImageResize/ImageResize.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace Eventviva;
4 +
5 + defined('ABSPATH') or die();
6 + /**
7 + * PHP class to resize and scale images
8 + */
9 + class ImageResize
10 + {
11 + const CROPTOP = 1;
12 + const CROPCENTRE = 2;
13 + const CROPCENTER = 2;
14 + const CROPBOTTOM = 3;
15 + const CROPLEFT = 4;
16 + const CROPRIGHT = 5;
17 + const CROPTOPCENTER = 6;
18 +
19 + public $quality_jpg = 85;
20 + public $quality_webp = 85;
21 + public $quality_png = 6;
22 + public $quality_truecolor = true;
23 +
24 + public $interlace = 1;
25 +
26 + public $source_type;
27 +
28 + protected $source_image;
29 +
30 + protected $original_w;
31 + protected $original_h;
32 +
33 + protected $dest_x = 0;
34 + protected $dest_y = 0;
35 +
36 + protected $source_x;
37 + protected $source_y;
38 +
39 + protected $dest_w;
40 + protected $dest_h;
41 +
42 + protected $source_w;
43 + protected $source_h;
44 +
45 + protected $source_info;
46 +
47 + /**
48 + * Create instance from a strng
49 + *
50 + * @param string $image_data
51 + * @return ImageResize
52 + * @throws ImageResizeException
53 + */
54 + public static function createFromString($image_data)
55 + {
56 + if (empty($image_data) || $image_data === null) {
57 + throw new ImageResizeException('image_data must not be empty');
58 + }
59 + $resize = new self('data://application/octet-stream;base64,' . base64_encode($image_data));
60 + return $resize;
61 + }
62 +
63 + /**
64 + * Loads image source and its properties to the instanciated object
65 + *
66 + * @param string $filename
67 + * @return ImageResize
68 + * @throws ImageResizeException
69 + */
70 + public function __construct($filename)
71 + {
72 + if (!defined('IMAGETYPE_WEBP')) {
73 + define('IMAGETYPE_WEBP', 18);
74 + }
75 + $isAscii = true;
76 + $len = strlen($filename);
77 + for ($i = 0; $i < $len; $i++)
78 + {
79 + if (ord($filename[$i]) > 127)
80 + {
81 + $isAscii = false;
82 + break;
83 + }
84 + }
85 + if($isAscii == true)
86 + {
87 + global $wp_filesystem;
88 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
89 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
90 + wp_filesystem($creds);
91 + }
92 + if ($filename === null || empty($filename) || (substr($filename, 0, 7) !== 'data://' && !$wp_filesystem->is_file($filename))) {
93 + throw new ImageResizeException('File does not exist');
94 + }
95 + if(!function_exists('finfo_open'))
96 + {
97 + throw new ImageResizeException('finfo_open does not exist, cannot resize image.');
98 + }
99 + $finfo = finfo_open(FILEINFO_MIME_TYPE);
100 + if (strstr(finfo_file($finfo, $filename), 'image') === false) {
101 + throw new ImageResizeException('Unsupported file type (' . $filename . '): ' . finfo_file($finfo, $filename));
102 + }
103 + }
104 +
105 + if (!$image_info = getimagesize($filename, $this->source_info)) {
106 + $image_info = getimagesize($filename);
107 + }
108 +
109 + if (!$image_info) {
110 + throw new ImageResizeException('Could not read file ' . $filename);
111 + }
112 +
113 + list(
114 + $this->original_w,
115 + $this->original_h,
116 + $this->source_type
117 + ) = $image_info;
118 +
119 + switch ($this->source_type) {
120 + case IMAGETYPE_GIF:
121 + if (!function_exists('imagecreatefromgif')) {
122 + throw new Exception('GD library is not available or GIF support is missing.');
123 + }
124 + $this->source_image = imagecreatefromgif($filename);
125 + break;
126 +
127 + case IMAGETYPE_JPEG:
128 + $this->source_image = $this->imageCreateJpegfromExif($filename);
129 +
130 + // set new width and height for image, maybe it has changed
131 + $this->original_w = ImageSX($this->source_image);
132 + $this->original_h = ImageSY($this->source_image);
133 +
134 + break;
135 +
136 + case IMAGETYPE_PNG:
137 + if (!function_exists('imagecreatefrompng')) {
138 + throw new Exception('GD library is not available or PNG support is missing.');
139 + }
140 + $this->source_image = imagecreatefrompng($filename);
141 + break;
142 +
143 + case IMAGETYPE_WEBP:
144 + if (version_compare(PHP_VERSION, '5.5.0', '<')) {
145 + throw new ImageResizeException('For WebP support PHP >= 5.5.0 is required');
146 + }
147 + if (!function_exists('imagecreatefromwebp')) {
148 + throw new Exception('GD library is not available or webp support is missing.');
149 + }
150 + $this->source_image = imagecreatefromwebp($filename);
151 + break;
152 +
153 + default:
154 + throw new ImageResizeException('Unsupported image type');
155 + break;
156 + }
157 +
158 + if (!$this->source_image) {
159 + throw new ImageResizeException('Could not load image');
160 + }
161 +
162 + return $this->resize($this->getSourceWidth(), $this->getSourceHeight());
163 + }
164 +
165 + // http://stackoverflow.com/a/28819866
166 + public function imageCreateJpegfromExif($filename)
167 + {
168 + $img = imagecreatefromjpeg($filename);
169 +
170 + if (!function_exists('exif_read_data') || !isset($this->source_info['APP1']) || strpos($this->source_info['APP1'], 'Exif') !== 0) {
171 + return $img;
172 + }
173 +
174 + $exif = exif_read_data($filename);
175 +
176 + if (!$exif || !isset($exif['Orientation'])) {
177 + return $img;
178 + }
179 +
180 + $orientation = $exif['Orientation'];
181 +
182 + if ($orientation === 6 || $orientation === 5) {
183 + $img = imagerotate($img, 270, 0);
184 + } elseif ($orientation === 3 || $orientation === 4) {
185 + $img = imagerotate($img, 180, 0);
186 + } elseif ($orientation === 8 || $orientation === 7) {
187 + $img = imagerotate($img, 90, 0);
188 + }
189 +
190 + if ($orientation === 5 || $orientation === 4 || $orientation === 7) {
191 + imageflip($img, IMG_FLIP_HORIZONTAL);
192 + }
193 +
194 + return $img;
195 + }
196 +
197 + /**
198 + * Saves new image
199 + *
200 + * @param string $filename
201 + * @param string $image_type
202 + * @param integer $quality
203 + * @param integer $permissions
204 + * @return \static
205 + */
206 + public function save($filename, $image_type = null, $quality = null, $permissions = null)
207 + {
208 + $image_type = $image_type ?: $this->source_type;
209 + $quality = is_numeric($quality) ? (int) abs($quality) : null;
210 +
211 + switch ($image_type) {
212 + case IMAGETYPE_GIF:
213 + $dest_image = imagecreatetruecolor(intval($this->getDestWidth()), intval($this->getDestHeight()));
214 +
215 + $background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1);
216 + imagecolortransparent($dest_image, $background);
217 + imagefill($dest_image, 0, 0, $background);
218 + imagesavealpha($dest_image, true);
219 + break;
220 +
221 + case IMAGETYPE_JPEG:
222 + $dest_image = imagecreatetruecolor(intval($this->getDestWidth()), intval($this->getDestHeight()));
223 +
224 + $background = imagecolorallocate($dest_image, 255, 255, 255);
225 + imagefilledrectangle($dest_image, 0, 0, intval($this->getDestWidth()), intval($this->getDestHeight()), $background);
226 + break;
227 +
228 + case IMAGETYPE_WEBP:
229 + if (version_compare(PHP_VERSION, '5.5.0', '<')) {
230 + throw new ImageResizeException('For WebP support PHP >= 5.5.0 is required');
231 + }
232 + $dest_image = imagecreatetruecolor(intval($this->getDestWidth()), intval($this->getDestHeight()));
233 +
234 + $background = imagecolorallocate($dest_image, 255, 255, 255);
235 + imagefilledrectangle($dest_image, 0, 0, intval($this->getDestWidth()), intval($this->getDestHeight()), $background);
236 + break;
237 +
238 + case IMAGETYPE_PNG:
239 + if (!$this->quality_truecolor && !imageistruecolor($this->source_image)) {
240 + $dest_image = imagecreate(intval($this->getDestWidth()), intval($this->getDestHeight()));
241 +
242 + $background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1);
243 + imagecolortransparent($dest_image, $background);
244 + imagefill($dest_image, 0, 0, $background);
245 + } else {
246 + $dest_image = imagecreatetruecolor(intval($this->getDestWidth()), intval($this->getDestHeight()));
247 + }
248 +
249 + imagealphablending($dest_image, false);
250 + imagesavealpha($dest_image, true);
251 + break;
252 + }
253 +
254 + imageinterlace($dest_image, $this->interlace);
255 +
256 + imagecopyresampled(
257 + $dest_image,
258 + $this->source_image,
259 + $this->dest_x,
260 + $this->dest_y,
261 + $this->source_x,
262 + $this->source_y,
263 + intval($this->getDestWidth()),
264 + intval($this->getDestHeight()),
265 + $this->source_w,
266 + $this->source_h
267 + );
268 +
269 + switch ($image_type) {
270 + case IMAGETYPE_GIF:
271 + imagegif($dest_image, $filename);
272 + break;
273 +
274 + case IMAGETYPE_JPEG:
275 + if ($quality === null || $quality > 100) {
276 + $quality = $this->quality_jpg;
277 + }
278 +
279 + imagejpeg($dest_image, $filename, $quality);
280 + break;
281 +
282 + case IMAGETYPE_WEBP:
283 + if (version_compare(PHP_VERSION, '5.5.0', '<')) {
284 + throw new ImageResizeException('For WebP support PHP >= 5.5.0 is required');
285 + }
286 + if ($quality === null) {
287 + $quality = $this->quality_webp;
288 + }
289 +
290 + imagewebp($dest_image, $filename, $quality);
291 + break;
292 +
293 + case IMAGETYPE_PNG:
294 + if ($quality === null || $quality > 9) {
295 + $quality = $this->quality_png;
296 + }
297 +
298 + imagepng($dest_image, $filename, $quality);
299 + break;
300 + }
301 +
302 + if ($permissions) {
303 + global $wp_filesystem;
304 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
305 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
306 + wp_filesystem($creds);
307 + }
308 + $wp_filesystem->chmod($filename, $permissions);
309 + }
310 +
311 + imagedestroy($dest_image);
312 + imagedestroy($this->source_image);
313 +
314 + return $this;
315 + }
316 +
317 + /**
318 + * Convert the image to string
319 + *
320 + * @param int $image_type
321 + * @param int $quality
322 + * @return string
323 + */
324 + public function getImageAsString($image_type = null, $quality = null)
325 + {
326 + $string_temp = tempnam(sys_get_temp_dir(), '');
327 +
328 + $this->save($string_temp, $image_type, $quality);
329 +
330 + global $wp_filesystem;
331 + if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
332 + include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
333 + wp_filesystem($creds);
334 + }
335 + $string = $wp_filesystem->get_contents($string_temp);
336 +
337 + $wp_filesystem->delete($string_temp);
338 +
339 + return $string;
340 + }
341 +
342 + /**
343 + * Convert the image to string with the current settings
344 + *
345 + * @return string
346 + */
347 + public function __toString()
348 + {
349 + return $this->getImageAsString();
350 + }
351 +
352 + /**
353 + * Outputs image to browser
354 + * @param string $image_type
355 + * @param integer $quality
356 + */
357 + public function output($image_type = null, $quality = null)
358 + {
359 + $image_type = $image_type ?: $this->source_type;
360 +
361 + header('Content-Type: ' . image_type_to_mime_type($image_type));
362 +
363 + $this->save(null, $image_type, $quality);
364 + }
365 +
366 + /**
367 + * Resizes image according to the given short side (short side proportional)
368 + *
369 + * @param integer $max_short
370 + * @param boolean $allow_enlarge
371 + * @return \static
372 + */
373 + public function resizeToShortSide($max_short, $allow_enlarge = false)
374 + {
375 + if ($this->getSourceHeight() < $this->getSourceWidth()) {
376 + $ratio = $max_short / $this->getSourceHeight();
377 + $long = $this->getSourceWidth() * $ratio;
378 +
379 + $this->resize($long, $max_short, $allow_enlarge);
380 + } else {
381 + $ratio = $max_short / $this->getSourceWidth();
382 + $long = $this->getSourceHeight() * $ratio;
383 +
384 + $this->resize($max_short, $long, $allow_enlarge);
385 + }
386 +
387 + return $this;
388 + }
389 +
390 + /**
391 + * Resizes image according to the given long side (short side proportional)
392 + *
393 + * @param integer $max_long
394 + * @param boolean $allow_enlarge
395 + * @return \static
396 + */
397 + public function resizeToLongSide($max_long, $allow_enlarge = false)
398 + {
399 + if ($this->getSourceHeight() > $this->getSourceWidth()) {
400 + $ratio = $max_long / $this->getSourceHeight();
401 + $short = $this->getSourceWidth() * $ratio;
402 +
403 + $this->resize($short, $max_long, $allow_enlarge);
404 + } else {
405 + $ratio = $max_long / $this->getSourceWidth();
406 + $short = $this->getSourceHeight() * $ratio;
407 +
408 + $this->resize($max_long, $short, $allow_enlarge);
409 + }
410 +
411 + return $this;
412 + }
413 +
414 + /**
415 + * Resizes image according to the given height (width proportional)
416 + *
417 + * @param integer $height
418 + * @param boolean $allow_enlarge
419 + * @return \static
420 + */
421 + public function resizeToHeight($height, $allow_enlarge = false)
422 + {
423 + $ratio = $height / $this->getSourceHeight();
424 + $width = $this->getSourceWidth() * $ratio;
425 +
426 + $this->resize($width, $height, $allow_enlarge);
427 +
428 + return $this;
429 + }
430 +
431 + /**
432 + * Resizes image according to the given width (height proportional)
433 + *
434 + * @param integer $width
435 + * @param boolean $allow_enlarge
436 + * @return \static
437 + */
438 + public function resizeToWidth($width, $allow_enlarge = false)
439 + {
440 + $ratio = $width / $this->getSourceWidth();
441 + $height = $this->getSourceHeight() * $ratio;
442 +
443 + $this->resize($width, $height, $allow_enlarge);
444 +
445 + return $this;
446 + }
447 +
448 + /**
449 + * Resizes image to best fit inside the given dimensions
450 + *
451 + * @param integer $max_width
452 + * @param integer $max_height
453 + * @param boolean $allow_enlarge
454 + * @return \static
455 + */
456 + public function resizeToBestFit($max_width, $max_height, $allow_enlarge = false)
457 + {
458 + if ($this->getSourceWidth() <= $max_width && $this->getSourceHeight() <= $max_height && $allow_enlarge === false) {
459 + return $this;
460 + }
461 +
462 + $ratio = $this->getSourceHeight() / $this->getSourceWidth();
463 + $width = $max_width;
464 + $height = $width * $ratio;
465 +
466 + if ($height > $max_height) {
467 + $height = $max_height;
468 + $width = $height / $ratio;
469 + }
470 +
471 + return $this->resize($width, $height, $allow_enlarge);
472 + }
473 +
474 + /**
475 + * Resizes image according to given scale (proportionally)
476 + *
477 + * @param integer|float $scale
478 + * @return \static
479 + */
480 + public function scale($scale)
481 + {
482 + $width = $this->getSourceWidth() * $scale / 100;
483 + $height = $this->getSourceHeight() * $scale / 100;
484 +
485 + $this->resize($width, $height, true);
486 +
487 + return $this;
488 + }
489 +
490 + /**
491 + * Resizes image according to the given width and height
492 + *
493 + * @param integer $width
494 + * @param integer $height
495 + * @param boolean $allow_enlarge
496 + * @return \static
497 + */
498 + public function resize($width, $height, $allow_enlarge = false)
499 + {
500 + if (!$allow_enlarge) {
501 + // if the user hasn't explicitly allowed enlarging,
502 + // but either of the dimensions are larger then the original,
503 + // then just use original dimensions - this logic may need rethinking
504 +
505 + if ($width > $this->getSourceWidth() || $height > $this->getSourceHeight()) {
506 + $width = $this->getSourceWidth();
507 + $height = $this->getSourceHeight();
508 + }
509 + }
510 +
511 + $this->source_x = 0;
512 + $this->source_y = 0;
513 +
514 + $this->dest_w = $width;
515 + $this->dest_h = $height;
516 +
517 + $this->source_w = $this->getSourceWidth();
518 + $this->source_h = $this->getSourceHeight();
519 +
520 + return $this;
521 + }
522 +
523 + /**
524 + * Crops image according to the given width, height and crop position
525 + *
526 + * @param integer $width
527 + * @param integer $height
528 + * @param boolean $allow_enlarge
529 + * @param integer $position
530 + * @return \static
531 + */
532 + public function crop($width, $height, $allow_enlarge = false, $position = self::CROPCENTER)
533 + {
534 + if (!$allow_enlarge) {
535 + // this logic is slightly different to resize(),
536 + // it will only reset dimensions to the original
537 + // if that particular dimenstion is larger
538 +
539 + if ($width > $this->getSourceWidth()) {
540 + $width = $this->getSourceWidth();
541 + }
542 +
543 + if ($height > $this->getSourceHeight()) {
544 + $height = $this->getSourceHeight();
545 + }
546 + }
547 +
548 + $ratio_source = $this->getSourceWidth() / $this->getSourceHeight();
549 + $ratio_dest = $width / $height;
550 +
551 + if ($ratio_dest < $ratio_source) {
552 + $this->resizeToHeight($height, $allow_enlarge);
553 +
554 + $excess_width = ($this->getDestWidth() - $width) / $this->getDestWidth() * $this->getSourceWidth();
555 +
556 + $this->source_w = $this->getSourceWidth() - $excess_width;
557 + $this->source_x = $this->getCropPosition($excess_width, $position);
558 +
559 + $this->dest_w = $width;
560 + } else {
561 + $this->resizeToWidth($width, $allow_enlarge);
562 +
563 + $excess_height = ($this->getDestHeight() - $height) / $this->getDestHeight() * $this->getSourceHeight();
564 +
565 + $this->source_h = $this->getSourceHeight() - $excess_height;
566 + $this->source_y = $this->getCropPosition($excess_height, $position);
567 +
568 + $this->dest_h = $height;
569 + }
570 +
571 + return $this;
572 + }
573 +
574 + /**
575 + * Crops image according to the given width, height, x and y
576 + *
577 + * @param integer $width
578 + * @param integer $height
579 + * @param integer $x
580 + * @param integer $y
581 + * @return \static
582 + */
583 + public function freecrop($width, $height, $x = false, $y = false)
584 + {
585 + if ($x === false or $y === false) {
586 + return $this->crop($width, $height);
587 + }
588 + $this->source_x = $x;
589 + $this->source_y = $y;
590 + if ($width > $this->getSourceWidth() - $x) {
591 + $this->source_w = $this->getSourceWidth() - $x;
592 + } else {
593 + $this->source_w = $width;
594 + }
595 +
596 + if ($height > $this->getSourceHeight() - $y) {
597 + $this->source_h = $this->getSourceHeight() - $y;
598 + } else {
599 + $this->source_h = $height;
600 + }
601 +
602 + $this->dest_w = $width;
603 + $this->dest_h = $height;
604 +
605 + return $this;
606 + }
607 +
608 + /**
609 + * Gets source width
610 + *
611 + * @return integer
612 + */
613 + public function getSourceWidth()
614 + {
615 + return $this->original_w;
616 + }
617 +
618 + /**
619 + * Gets source height
620 + *
621 + * @return integer
622 + */
623 + public function getSourceHeight()
624 + {
625 + return $this->original_h;
626 + }
627 +
628 + /**
629 + * Gets width of the destination image
630 + *
631 + * @return integer
632 + */
633 + public function getDestWidth()
634 + {
635 + return $this->dest_w;
636 + }
637 +
638 + /**
639 + * Gets height of the destination image
640 + * @return integer
641 + */
642 + public function getDestHeight()
643 + {
644 + return $this->dest_h;
645 + }
646 +
647 + /**
648 + * Gets crop position (X or Y) according to the given position
649 + *
650 + * @param integer $expectedSize
651 + * @param integer $position
652 + * @return integer
653 + */
654 + protected function getCropPosition($expectedSize, $position = self::CROPCENTER)
655 + {
656 + $size = 0;
657 + switch ($position) {
658 + case self::CROPBOTTOM:
659 + case self::CROPRIGHT:
660 + $size = $expectedSize;
661 + break;
662 + case self::CROPCENTER:
663 + case self::CROPCENTRE:
664 + $size = $expectedSize / 2;
665 + break;
666 + case self::CROPTOPCENTER:
667 + $size = $expectedSize / 4;
668 + break;
669 + }
670 + return $size;
671 + }
672 + }
673 +
674 + // imageflip definition for PHP < 5.5
675 + if (!function_exists('imageflip')) {
676 + define('IMG_FLIP_HORIZONTAL', 0);
677 + define('IMG_FLIP_VERTICAL', 1);
678 + define('IMG_FLIP_BOTH', 2);
679 +
680 + function imageflip($image, $mode)
681 + {
682 + switch ($mode) {
683 + case IMG_FLIP_HORIZONTAL: {
684 + $max_x = imagesx($image) - 1;
685 + $half_x = $max_x / 2;
686 + $sy = imagesy($image);
687 + $temp_image = imageistruecolor($image)? imagecreatetruecolor(1, $sy): imagecreate(1, $sy);
688 + for ($x = 0; $x < $half_x; ++$x) {
689 + imagecopy($temp_image, $image, 0, 0, $x, 0, 1, $sy);
690 + imagecopy($image, $image, $x, 0, $max_x - $x, 0, 1, $sy);
691 + imagecopy($image, $temp_image, $max_x - $x, 0, 0, 0, 1, $sy);
692 + }
693 + break;
694 + }
695 + case IMG_FLIP_VERTICAL: {
696 + $sx = imagesx($image);
697 + $max_y = imagesy($image) - 1;
698 + $half_y = $max_y / 2;
699 + $temp_image = imageistruecolor($image)? imagecreatetruecolor($sx, 1): imagecreate($sx, 1);
700 + for ($y = 0; $y < $half_y; ++$y) {
701 + imagecopy($temp_image, $image, 0, 0, 0, $y, $sx, 1);
702 + imagecopy($image, $image, 0, $y, 0, $max_y - $y, $sx, 1);
703 + imagecopy($image, $temp_image, 0, $max_y - $y, 0, 0, $sx, 1);
704 + }
705 + break;
706 + }
707 + case IMG_FLIP_BOTH: {
708 + $sx = imagesx($image);
709 + $sy = imagesy($image);
710 + $temp_image = imagerotate($image, 180, 0);
711 + imagecopy($image, $temp_image, 0, 0, 0, 0, $sx, $sy);
712 + break;
713 + }
714 + default: {
715 + return;
716 + }
717 + }
718 + imagedestroy($temp_image);
719 + }
720 + }
721 +
722 + /**
723 + * PHP Exception used in the ImageResize class
724 + */
725 + class ImageResizeException extends \Exception
726 + {
727 + }