| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Cecil. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Arnaud Ligny <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Cecil\Asset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Cecil\Asset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Cecil\Exception\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Intervention\Image\Drivers\Gd\Driver as GdDriver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Intervention\Image\Encoders\AutoEncoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Intervention\Image\ImageManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * Image Asset class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * Provides methods to manipulate images, such as resizing, cropping, converting, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * and generating data URLs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * This class uses the Intervention Image library to handle image processing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * It supports both GD and Imagick drivers, depending on the available PHP extensions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | class Image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Create new manager instance with available driver. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private static function manager(): ImageManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $driver = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         // ImageMagick is available? (for a future quality option) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if (\extension_loaded('imagick') && class_exists('Imagick')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $driver = ImagickDriver::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         // Use GD, because it's the faster driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (\extension_loaded('gd') && \function_exists('gd_info')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $driver = GdDriver::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if ($driver) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             return ImageManager::withDriver( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 $driver, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     'autoOrientation' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     'decodeAnimation' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     'blendingColor' => 'ffffff', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     'strip' => true, // remove metadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         throw new RuntimeException('PHP GD (or Imagick) extension is required.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Scales down an image Asset to the given width, keeping the aspect ratio. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public static function resize(Asset $asset, int $width, int $quality): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             // creates image object from source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $image = self::manager()->read($asset['content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             // resizes to $width with constraint the aspect-ratio and unwanted upsizing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $image->scaleDown(width: $width); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             // return image data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return (string) $image->encodeByMediaType( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 $asset['subtype'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 /** @scrutinizer ignore-type */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 progressive: true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 /** @scrutinizer ignore-type */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 interlaced: false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 quality: $quality | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             throw new RuntimeException(\sprintf('Asset "%s" can\'t be resized: %s', $asset['path'], $e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Crops an image Asset to the given width and height, keeping the aspect ratio. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public static function cover(Asset $asset, int $width, int $height, int $quality): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             // creates image object from source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $image = self::manager()->read($asset['content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             // turns an animated image (i.e GIF) into a static image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if ($image->isAnimated()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $image = $image->removeAnimation('25%'); // use 25% to avoid an "empty" frame | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             // crops the image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $image->cover( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 width: $width, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 height: $height, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 position: 'center' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             // return image data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             return (string) $image->encodeByMediaType( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $asset['subtype'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 /** @scrutinizer ignore-type */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 progressive: true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 /** @scrutinizer ignore-type */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 interlaced: false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 quality: $quality | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             throw new RuntimeException(\sprintf('Asset "%s" can\'t be cropped: %s', $asset['path'], $e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * Makes an image Asset maskable, meaning it can be used as a PWA icon. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public static function maskable(Asset $asset, int $quality, int $padding): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             // creates image object from source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $source = self::manager()->read($asset['content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             // creates a new image with the dominant color as background | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             // and the size of the original image plus the padding | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $image = self::manager()->create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 width: (int) round($asset['width'] * (1 + $padding / 100), 0), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 height: (int) round($asset['height'] * (1 + $padding / 100), 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             )->fill(self::getDominantColor($asset)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             // inserts the original image in the center | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             $image->place( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 $source, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 position: 'center' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             // scales down the new image to the original image size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             $image->scaleDown(width: $asset['width']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             // return image data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             return (string) $image->encodeByMediaType( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 $asset['subtype'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 /** @scrutinizer ignore-type */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 progressive: true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 /** @scrutinizer ignore-type */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 interlaced: false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                 quality: $quality | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             throw new RuntimeException(\sprintf('Unable to make Asset "%s" maskable: %s', $asset['path'], $e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * Converts an image Asset to the target format. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     public static function convert(Asset $asset, string $format, int $quality): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             $image = self::manager()->read($asset['content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             if (!\function_exists("image$format")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                 throw new RuntimeException(\sprintf('Function "image%s" is not available.', $format)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             return (string) $image->encodeByExtension( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                 $format, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 /** @scrutinizer ignore-type */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |                 progressive: true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |                 /** @scrutinizer ignore-type */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |                 interlaced: false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |                 quality: $quality | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             throw new RuntimeException(\sprintf('Unable to convert "%s" to %s: %s', $asset['path'], $format, $e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * Returns the Data URL (encoded in Base64). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     public static function getDataUrl(Asset $asset, int $quality): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             $image = self::manager()->read($asset['content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             return (string) $image->encode(new AutoEncoder(quality: $quality))->toDataUri(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             throw new RuntimeException(\sprintf('Unable to get Data URL of "%s": %s', $asset['path'], $e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      * Returns the dominant RGB color of an image asset. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     public static function getDominantColor(Asset $asset): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             $image = self::manager()->read(self::resize($asset, 100, 50)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |             return $image->reduceColors(1)->pickColor(0, 0)->toString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |             throw new RuntimeException(\sprintf('Unable to get dominant color of "%s": %s', $asset['path'], $e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |      * Returns a Low Quality Image Placeholder (LQIP) as data URL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |     public static function getLqip(Asset $asset): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             $image = self::manager()->read(self::resize($asset, 100, 50)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |             return (string) $image->blur(50)->encode()->toDataUri(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |             throw new RuntimeException(\sprintf('Unable to create LQIP of "%s": %s', $asset['path'], $e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      * Build the `srcset` HTML attribute for responsive images. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |      * e.g.: `srcset="/img-480.jpg 480w, /img-800.jpg 800w"`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |      * $widths is an array of widths to include in the `srcset`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |      * If $notEmpty is true, the source image is always added to the `srcset`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 245 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 246 |  |  |     public static function buildHtmlSrcset(Asset $asset, array $widths, $notEmpty = false): string | 
            
                                                                        
                            
            
                                    
            
            
                | 247 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 248 |  |  |         if (!self::isImage($asset)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 249 |  |  |             throw new RuntimeException(\sprintf('Unable to build "srcset" of "%s": it\'s not an image file.', $asset['path'])); | 
            
                                                                        
                            
            
                                    
            
            
                | 250 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 251 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 252 |  |  |         $srcset = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 253 |  |  |         $widthMax = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 254 |  |  |         sort($widths, SORT_NUMERIC); | 
            
                                                                        
                            
            
                                    
            
            
                | 255 |  |  |         $widths = array_reverse($widths); | 
            
                                                                        
                            
            
                                    
            
            
                | 256 |  |  |         foreach ($widths as $width) { | 
            
                                                                        
                            
            
                                    
            
            
                | 257 |  |  |             if ($asset['width'] < $width) { | 
            
                                                                        
                            
            
                                    
            
            
                | 258 |  |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 259 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 260 |  |  |             $img = $asset->resize($width); | 
            
                                                                        
                            
            
                                    
            
            
                | 261 |  |  |             $srcset .= \sprintf('%s %sw, ', (string) $img, $width); | 
            
                                                                        
                            
            
                                    
            
            
                | 262 |  |  |             $widthMax = $width; | 
            
                                                                        
                            
            
                                    
            
            
                | 263 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 264 |  |  |         // adds source image | 
            
                                                                        
                            
            
                                    
            
            
                | 265 |  |  |         if ((!empty($srcset) || $notEmpty) && ($asset['width'] < max($widths) && $asset['width'] != $widthMax)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 266 |  |  |             $srcset .= \sprintf('%s %sw', (string) $asset, $asset['width']); | 
            
                                                                        
                            
            
                                    
            
            
                | 267 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 268 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 269 |  |  |         return rtrim($srcset, ', '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |      * Returns the value from the `$sizes` array if the class exists, otherwise returns the default size. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |     public static function getHtmlSizes(string $class, array $sizes = []): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |         $result = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         $classArray = explode(' ', $class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |         foreach ($classArray as $class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |             if (\array_key_exists($class, $sizes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |                 $result = $sizes[$class] . ', '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |         if (!empty($result)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |             return trim($result, ', '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         return $sizes['default'] ?? '100vw'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |      * Checks if an asset is an animated GIF. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     public static function isAnimatedGif(Asset $asset): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |         // an animated GIF contains multiple "frames", with each frame having a header made up of: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |         // 1. a static 4-byte sequence (\x00\x21\xF9\x04) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |         // 2. 4 variable bytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |         // 3. a static 2-byte sequence (\x00\x2C) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |         $count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', (string) $asset['content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |         return $count > 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |      * Returns true if asset is a SVG. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |     public static function isSVG(Asset $asset): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |         return \in_array($asset['subtype'], ['image/svg', 'image/svg+xml']) || $asset['ext'] == 'svg'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |      * Returns true if asset is an ICO. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |     public static function isIco(Asset $asset): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |         return \in_array($asset['subtype'], ['image/x-icon', 'image/vnd.microsoft.icon']) || $asset['ext'] == 'ico'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |      * Asset is a valid image? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |     public static function isImage(Asset $asset): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |         if ($asset['type'] !== 'image' || self::isSVG($asset) || self::isIco($asset)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |      * Returns SVG attributes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |      * @return \SimpleXMLElement|false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |     public static function getSvgAttributes(Asset $asset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |         if (!self::isSVG($asset)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |         if (false === $xml = simplexml_load_string($asset['content'] ?? '')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |         return $xml->attributes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 350 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 351 |  |  |  |