]> git.mxchange.org Git - friendica.git/blob - src/Object/Image.php
Use the blurhash when the remote picture doesn't load
[friendica.git] / src / Object / Image.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Object;
23
24 use Exception;
25 use Friendica\DI;
26 use Friendica\Util\Images;
27 use Imagick;
28 use ImagickPixel;
29 use GDImage;
30 use kornrunner\Blurhash\Blurhash;
31
32 /**
33  * Class to handle images
34  */
35 class Image
36 {
37         /** @var GDImage|Imagick|resource */
38         private $image;
39
40         /*
41          * Put back gd stuff, not everybody have Imagick
42          */
43         private $imagick;
44         private $width;
45         private $height;
46         private $valid;
47         private $type;
48         private $types;
49
50         /**
51          * Constructor
52          *
53          * @param string $data Image data
54          * @param string $type optional, default null
55          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
56          * @throws \ImagickException
57          */
58         public function __construct(string $data, string $type = null)
59         {
60                 $this->imagick = class_exists('Imagick');
61                 $this->types = Images::supportedTypes();
62                 if (!array_key_exists($type, $this->types)) {
63                         $type = 'image/jpeg';
64                 }
65                 $this->type = $type;
66
67                 if ($this->isImagick() && $this->loadData($data)) {
68                         return;
69                 } else {
70                         // Failed to load with Imagick, fallback
71                         $this->imagick = false;
72                 }
73                 $this->loadData($data);
74         }
75
76         /**
77          * Destructor
78          *
79          * @return void
80          */
81         public function __destruct()
82         {
83                 if ($this->image) {
84                         if ($this->isImagick()) {
85                                 $this->image->clear();
86                                 $this->image->destroy();
87                                 return;
88                         }
89                         if (is_resource($this->image)) {
90                                 imagedestroy($this->image);
91                         }
92                 }
93         }
94
95         /**
96          * @return boolean
97          */
98         public function isImagick()
99         {
100                 return $this->imagick;
101         }
102
103         /**
104          * Loads image data into handler class
105          *
106          * @param string $data Image data
107          * @return boolean Success
108          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
109          * @throws \ImagickException
110          */
111         private function loadData(string $data): bool
112         {
113                 if ($this->isImagick()) {
114                         $this->image = new Imagick();
115                         try {
116                                 $this->image->readImageBlob($data);
117                         } catch (Exception $e) {
118                                 // Imagick couldn't use the data
119                                 return false;
120                         }
121
122                         /*
123                          * Setup the image to the format it will be saved to
124                          */
125                         $map = Images::getFormatsMap();
126                         $format = $map[$this->type];
127                         $this->image->setFormat($format);
128
129                         // Always coalesce, if it is not a multi-frame image it won't hurt anyway
130                         try {
131                                 $this->image = $this->image->coalesceImages();
132                         } catch (Exception $e) {
133                                 return false;
134                         }
135
136                         /*
137                          * setup the compression here, so we'll do it only once
138                          */
139                         switch ($this->getType()) {
140                                 case 'image/png':
141                                         $quality = DI::config()->get('system', 'png_quality');
142                                         /*
143                                          * From http://www.imagemagick.org/script/command-line-options.php#quality:
144                                          *
145                                          * 'For the MNG and PNG image formats, the quality value sets
146                                          * the zlib compression level (quality / 10) and filter-type (quality % 10).
147                                          * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering,
148                                          * unless the image has a color map, in which case it means compression level 7 with no PNG filtering'
149                                          */
150                                         $quality = $quality * 10;
151                                         $this->image->setCompressionQuality($quality);
152                                         break;
153
154                                 case 'image/jpg':
155                                 case 'image/jpeg':
156                                         $quality = DI::config()->get('system', 'jpeg_quality');
157                                         $this->image->setCompressionQuality($quality);
158                         }
159
160                         // The 'width' and 'height' properties are only used by non-Imagick routines.
161                         $this->width  = $this->image->getImageWidth();
162                         $this->height = $this->image->getImageHeight();
163                         $this->valid  = true;
164
165                         return true;
166                 }
167
168                 $this->valid = false;
169                 try {
170                         $this->image = @imagecreatefromstring($data);
171                         if ($this->image !== false) {
172                                 $this->width  = imagesx($this->image);
173                                 $this->height = imagesy($this->image);
174                                 $this->valid  = true;
175                                 imagealphablending($this->image, false);
176                                 imagesavealpha($this->image, true);
177
178                                 return true;
179                         }
180                 } catch (\Throwable $error) {
181                         /** @see https://github.com/php/doc-en/commit/d09a881a8e9059d11e756ee59d75bf404d6941ed */
182                         if (strstr($error->getMessage(), "gd-webp cannot allocate temporary buffer")) {
183                                 DI::logger()->notice('Image is probably animated and therefore unsupported', ['error' => $error]);
184                         } else {
185                                 DI::logger()->warning('Unexpected throwable.', ['error' => $error]);
186                         }
187                 }
188
189                 return false;
190         }
191
192         /**
193          * @return boolean
194          */
195         public function isValid(): bool
196         {
197                 if ($this->isImagick()) {
198                         return ($this->image !== false);
199                 }
200                 return $this->valid;
201         }
202
203         /**
204          * @return mixed
205          */
206         public function getWidth()
207         {
208                 if (!$this->isValid()) {
209                         return false;
210                 }
211
212                 if ($this->isImagick()) {
213                         return $this->image->getImageWidth();
214                 }
215                 return $this->width;
216         }
217
218         /**
219          * @return mixed
220          */
221         public function getHeight()
222         {
223                 if (!$this->isValid()) {
224                         return false;
225                 }
226
227                 if ($this->isImagick()) {
228                         return $this->image->getImageHeight();
229                 }
230                 return $this->height;
231         }
232
233         /**
234          * @return mixed
235          */
236         public function getImage()
237         {
238                 if (!$this->isValid()) {
239                         return false;
240                 }
241
242                 if ($this->isImagick()) {
243                         try {
244                                 /* Clean it */
245                                 $this->image = $this->image->deconstructImages();
246                                 return $this->image;
247                         } catch (Exception $e) {
248                                 return false;
249                         }
250                 }
251                 return $this->image;
252         }
253
254         /**
255          * @return mixed
256          */
257         public function getType()
258         {
259                 if (!$this->isValid()) {
260                         return false;
261                 }
262
263                 return $this->type;
264         }
265
266         /**
267          * @return mixed
268          */
269         public function getExt()
270         {
271                 if (!$this->isValid()) {
272                         return false;
273                 }
274
275                 return $this->types[$this->getType()];
276         }
277
278         /**
279          * Scales image down
280          *
281          * @param integer $max max dimension
282          * @return mixed
283          */
284         public function scaleDown(int $max)
285         {
286                 if (!$this->isValid()) {
287                         return false;
288                 }
289
290                 $width = $this->getWidth();
291                 $height = $this->getHeight();
292
293                 if ((! $width)|| (! $height)) {
294                         return false;
295                 }
296
297                 if ($width > $max && $height > $max) {
298                         // very tall image (greater than 16:9)
299                         // constrain the width - let the height float.
300
301                         if ((($height * 9) / 16) > $width) {
302                                 $dest_width = $max;
303                                 $dest_height = intval(($height * $max) / $width);
304                         } elseif ($width > $height) {
305                                 // else constrain both dimensions
306                                 $dest_width = $max;
307                                 $dest_height = intval(($height * $max) / $width);
308                         } else {
309                                 $dest_width = intval(($width * $max) / $height);
310                                 $dest_height = $max;
311                         }
312                 } else {
313                         if ($width > $max) {
314                                 $dest_width = $max;
315                                 $dest_height = intval(($height * $max) / $width);
316                         } else {
317                                 if ($height > $max) {
318                                         // very tall image (greater than 16:9)
319                                         // but width is OK - don't do anything
320
321                                         if ((($height * 9) / 16) > $width) {
322                                                 $dest_width = $width;
323                                                 $dest_height = $height;
324                                         } else {
325                                                 $dest_width = intval(($width * $max) / $height);
326                                                 $dest_height = $max;
327                                         }
328                                 } else {
329                                         $dest_width = $width;
330                                         $dest_height = $height;
331                                 }
332                         }
333                 }
334
335                 return $this->scale($dest_width, $dest_height);
336         }
337
338         /**
339          * Rotates image
340          *
341          * @param integer $degrees degrees to rotate image
342          * @return mixed
343          */
344         public function rotate(int $degrees)
345         {
346                 if (!$this->isValid()) {
347                         return false;
348                 }
349
350                 if ($this->isImagick()) {
351                         $this->image->setFirstIterator();
352                         do {
353                                 $this->image->rotateImage(new ImagickPixel(), -$degrees); // ImageMagick rotates in the opposite direction of imagerotate()
354                         } while ($this->image->nextImage());
355                         return;
356                 }
357
358                 // if script dies at this point check memory_limit setting in php.ini
359                 $this->image  = imagerotate($this->image, $degrees, 0);
360                 $this->width  = imagesx($this->image);
361                 $this->height = imagesy($this->image);
362         }
363
364         /**
365          * Flips image
366          *
367          * @param boolean $horiz optional, default true
368          * @param boolean $vert  optional, default false
369          * @return mixed
370          */
371         public function flip(bool $horiz = true, bool $vert = false)
372         {
373                 if (!$this->isValid()) {
374                         return false;
375                 }
376
377                 if ($this->isImagick()) {
378                         $this->image->setFirstIterator();
379                         do {
380                                 if ($horiz) {
381                                         $this->image->flipImage();
382                                 }
383                                 if ($vert) {
384                                         $this->image->flopImage();
385                                 }
386                         } while ($this->image->nextImage());
387                         return;
388                 }
389
390                 $w = imagesx($this->image);
391                 $h = imagesy($this->image);
392                 $flipped = imagecreate($w, $h);
393                 if ($horiz) {
394                         for ($x = 0; $x < $w; $x++) {
395                                 imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
396                         }
397                 }
398                 if ($vert) {
399                         for ($y = 0; $y < $h; $y++) {
400                                 imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
401                         }
402                 }
403                 $this->image = $flipped;
404         }
405
406         /**
407          * Fixes orientation and maybe returns EXIF data (?)
408          *
409          * @param string $filename Filename
410          * @return mixed
411          */
412         public function orient(string $filename)
413         {
414                 if ($this->isImagick()) {
415                         // based off comment on http://php.net/manual/en/imagick.getimageorientation.php
416                         $orientation = $this->image->getImageOrientation();
417                         switch ($orientation) {
418                                 case Imagick::ORIENTATION_BOTTOMRIGHT:
419                                         $this->image->rotateimage("#000", 180);
420                                         break;
421                                 case Imagick::ORIENTATION_RIGHTTOP:
422                                         $this->image->rotateimage("#000", 90);
423                                         break;
424                                 case Imagick::ORIENTATION_LEFTBOTTOM:
425                                         $this->image->rotateimage("#000", -90);
426                                         break;
427                         }
428
429                         $this->image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
430                         return true;
431                 }
432                 // based off comment on http://php.net/manual/en/function.imagerotate.php
433
434                 if (!$this->isValid()) {
435                         return false;
436                 }
437
438                 if ((!function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg')) {
439                         return;
440                 }
441
442                 $exif = @exif_read_data($filename, null, true);
443                 if (!$exif) {
444                         return;
445                 }
446
447                 $ort = isset($exif['IFD0']['Orientation']) ? $exif['IFD0']['Orientation'] : 1;
448
449                 switch ($ort) {
450                         case 1: // nothing
451                                 break;
452
453                         case 2: // horizontal flip
454                                 $this->flip();
455                                 break;
456
457                         case 3: // 180 rotate left
458                                 $this->rotate(180);
459                                 break;
460
461                         case 4: // vertical flip
462                                 $this->flip(false, true);
463                                 break;
464
465                         case 5: // vertical flip + 90 rotate right
466                                 $this->flip(false, true);
467                                 $this->rotate(-90);
468                                 break;
469
470                         case 6: // 90 rotate right
471                                 $this->rotate(-90);
472                                 break;
473
474                         case 7: // horizontal flip + 90 rotate right
475                                 $this->flip();
476                                 $this->rotate(-90);
477                                 break;
478
479                         case 8: // 90 rotate left
480                                 $this->rotate(90);
481                                 break;
482                 }
483
484                 return $exif;
485         }
486
487         /**
488          * Rescales image to minimum size
489          *
490          * @param integer $min Minimum dimension
491          * @return mixed
492          */
493         public function scaleUp(int $min)
494         {
495                 if (!$this->isValid()) {
496                         return false;
497                 }
498
499                 $width = $this->getWidth();
500                 $height = $this->getHeight();
501
502                 if ((!$width)|| (!$height)) {
503                         return false;
504                 }
505
506                 if ($width < $min && $height < $min) {
507                         if ($width > $height) {
508                                 $dest_width = $min;
509                                 $dest_height = intval(($height * $min) / $width);
510                         } else {
511                                 $dest_width = intval(($width * $min) / $height);
512                                 $dest_height = $min;
513                         }
514                 } else {
515                         if ($width < $min) {
516                                 $dest_width = $min;
517                                 $dest_height = intval(($height * $min) / $width);
518                         } else {
519                                 if ($height < $min) {
520                                         $dest_width = intval(($width * $min) / $height);
521                                         $dest_height = $min;
522                                 } else {
523                                         $dest_width = $width;
524                                         $dest_height = $height;
525                                 }
526                         }
527                 }
528
529                 return $this->scale($dest_width, $dest_height);
530         }
531
532         /**
533          * Scales image to square
534          *
535          * @param integer $dim Dimension
536          * @return mixed
537          */
538         public function scaleToSquare(int $dim)
539         {
540                 if (!$this->isValid()) {
541                         return false;
542                 }
543
544                 return $this->scale($dim, $dim);
545         }
546
547         /**
548          * Scale image to target dimensions
549          *
550          * @param int $dest_width Destination width
551          * @param int $dest_height Destination height
552          * @return boolean Success
553          */
554         private function scale(int $dest_width, int $dest_height): bool
555         {
556                 if (!$this->isValid()) {
557                         return false;
558                 }
559
560                 if ($this->isImagick()) {
561                         /*
562                          * If it is not animated, there will be only one iteration here,
563                          * so don't bother checking
564                          */
565                         // Don't forget to go back to the first frame
566                         $this->image->setFirstIterator();
567                         do {
568                                 // FIXME - implement horizontal bias for scaling as in following GD functions
569                                 // to allow very tall images to be constrained only horizontally.
570                                 try {
571                                         $this->image->scaleImage($dest_width, $dest_height);
572                                 } catch (Exception $e) {
573                                         // Imagick couldn't use the data
574                                         return false;
575                                 }
576                         } while ($this->image->nextImage());
577
578                         // These may not be necessary anymore
579                         $this->width  = $this->image->getImageWidth();
580                         $this->height = $this->image->getImageHeight();
581                 } else {
582                         $dest = imagecreatetruecolor($dest_width, $dest_height);
583                         imagealphablending($dest, false);
584                         imagesavealpha($dest, true);
585
586                         if ($this->type=='image/png') {
587                                 imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
588                         }
589
590                         imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $this->width, $this->height);
591
592                         if ($this->image) {
593                                 imagedestroy($this->image);
594                         }
595
596                         $this->image = $dest;
597                         $this->width  = imagesx($this->image);
598                         $this->height = imagesy($this->image);
599                 }
600
601                 return true;
602         }
603
604         /**
605          * Convert a GIF to a PNG to make it static
606          *
607          * @return void
608          */
609         public function toStatic()
610         {
611                 if ($this->type != 'image/gif') {
612                         return;
613                 }
614
615                 if ($this->isImagick()) {
616                         $this->type == 'image/png';
617                         $this->image->setFormat('png');
618                 }
619         }
620
621         /**
622          * Crops image
623          *
624          * @param integer $max maximum
625          * @param integer $x   x coordinate
626          * @param integer $y   y coordinate
627          * @param integer $w   width
628          * @param integer $h   height
629          * @return mixed
630          */
631         public function crop(int $max, int $x, int $y, int $w, int $h)
632         {
633                 if (!$this->isValid()) {
634                         return false;
635                 }
636
637                 if ($this->isImagick()) {
638                         $this->image->setFirstIterator();
639                         do {
640                                 $this->image->cropImage($w, $h, $x, $y);
641                                 /*
642                                  * We need to remove the canva,
643                                  * or the image is not resized to the crop:
644                                  * http://php.net/manual/en/imagick.cropimage.php#97232
645                                  */
646                                 $this->image->setImagePage(0, 0, 0, 0);
647                         } while ($this->image->nextImage());
648                         return $this->scaleDown($max);
649                 }
650
651                 $dest = imagecreatetruecolor($max, $max);
652                 imagealphablending($dest, false);
653                 imagesavealpha($dest, true);
654                 if ($this->type=='image/png') {
655                         imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
656                 }
657                 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
658                 if ($this->image) {
659                         imagedestroy($this->image);
660                 }
661                 $this->image = $dest;
662                 $this->width  = imagesx($this->image);
663                 $this->height = imagesy($this->image);
664
665                 // All successful
666                 return true;
667         }
668
669         /**
670          * Magic method allowing string casting of an Image object
671          *
672          * Ex: $data = $Image->asString();
673          * can be replaced by
674          * $data = (string) $Image;
675          *
676          * @return string
677          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
678          */
679         public function __toString(): string
680         {
681                 return (string) $this->asString();
682         }
683
684         /**
685          * Returns image as string or false on failure
686          *
687          * @return mixed
688          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
689          */
690         public function asString()
691         {
692                 if (!$this->isValid()) {
693                         return false;
694                 }
695
696                 if ($this->isImagick()) {
697                         try {
698                                 /* Clean it */
699                                 $this->image = $this->image->deconstructImages();
700                                 return $this->image->getImagesBlob();
701                         } catch (Exception $e) {
702                                 return false;
703                         }
704                 }
705
706                 $stream = fopen('php://memory','r+');
707
708                 // Enable interlacing
709                 imageinterlace($this->image, true);
710
711                 switch ($this->getType()) {
712                         case 'image/png':
713                                 $quality = DI::config()->get('system', 'png_quality');
714                                 imagepng($this->image, $stream, $quality);
715                                 break;
716
717                         case 'image/jpeg':
718                         case 'image/jpg':
719                                 $quality = DI::config()->get('system', 'jpeg_quality');
720                                 imagejpeg($this->image, $stream, $quality);
721                                 break;
722                 }
723                 rewind($stream);
724                 return stream_get_contents($stream);
725         }
726
727         /**
728          * Create a blurhash out of a given image string
729          *
730          * @param string $img_str
731          * @return string
732          */
733         public function getBlurHash(): string
734         {
735                 if ($this->isImagick()) {
736                         // Imagick is not supported
737                         return '';
738                 }
739
740                 $width = $this->getWidth();
741                 $height = $this->getHeight();
742
743                 if (max($width, $height) > 90) {
744                         $this->scaleDown(90);
745                         $width = $this->getWidth();
746                         $height = $this->getHeight();
747                 }
748
749                 $pixels = [];
750                 for ($y = 0; $y < $height; ++$y) {
751                         $row = [];
752                         for ($x = 0; $x < $width; ++$x) {
753                                 $index = imagecolorat($this->image, $x, $y);
754                                 $colors = imagecolorsforindex($this->image, $index);
755
756                                 $row[] = [$colors['red'], $colors['green'], $colors['blue']];
757                         }
758                         $pixels[] = $row;
759                 }
760
761                 // The components define the amount of details (1 to 9).
762                 $components_x = 9;
763                 $components_y = 9;
764
765                 return Blurhash::encode($pixels, $components_x, $components_y);
766         }
767
768         /**
769          * Create an image out of a blurhash
770          *
771          * @param string $blurhash
772          * @param integer $width
773          * @param integer $height
774          * @return void
775          */
776         public function getFromBlurHash(string $blurhash, int $width, int $height)
777         {
778                 if ($this->isImagick()) {
779                         // Imagick is not supported
780                         return;
781                 }
782
783                 $scaled = Images::getScalingDimensions($width, $height, 90);
784                 $pixels = Blurhash::decode($blurhash, $scaled['width'], $scaled['height']);
785
786                 $this->image = imagecreatetruecolor($scaled['width'], $scaled['height']);
787                 for ($y = 0; $y < $scaled['height']; ++$y) {
788                         for ($x = 0; $x < $scaled['width']; ++$x) {
789                                 [$r, $g, $b] = $pixels[$y][$x];
790                                 imagesetpixel($this->image, $x, $y, imagecolorallocate($this->image, $r, $g, $b));
791                         }
792                 }
793
794                 $this->width  = imagesx($this->image);
795                 $this->height = imagesy($this->image);
796                 $this->valid  = true;
797
798                 $this->scaleUp(min($width, $height));
799         }
800 }