| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Pageon\Html\Image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Intervention\Image\ImageManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Intervention\Image\Image as ScaleableImage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Spatie\ImageOptimizer\OptimizerChain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Stitcher\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class ImageFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var \Pageon\Html\Image\Scaler */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $scaler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $sourceDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $publicDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var \Intervention\Image\ImageManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $imageManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var \Spatie\ImageOptimizer\OptimizerChain */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $optimizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** @var bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $cache = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 41 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         string $sourceDirectory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         string $publicDirectory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         Scaler $scaler, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         OptimizerChain $optimizer = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 41 |  |         $this->sourceDirectory = rtrim($sourceDirectory, '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 41 |  |         $this->publicDirectory = rtrim($publicDirectory, '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 41 |  |         $this->scaler = $scaler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 41 |  |         $this->imageManager = new ImageManager([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 41 |  |             'driver' => 'gd', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 41 |  |         $this->optimizer = $optimizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 41 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 36 |  |     public static function make( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         string $sourceDirectory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         string $publicDirectory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         Scaler $scaler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     ): ImageFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 36 |  |         return new self($sourceDirectory, $publicDirectory, $scaler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 5 |  |     public function enableCaching(bool $cache): ImageFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |         $this->cache = $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 5 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 19 |  |     public function create($src): Image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 19 |  |         $srcPath = ltrim($src, '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 19 |  |         if ($this->cache && file_exists("{$this->publicDirectory}/{$srcPath}")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |             return $this->createCachedImage($srcPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 19 |  |         $image = Image::make($srcPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 19 |  |         $this->copySourceImageToDestination($srcPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 19 |  |         $scaleableImage = $this->imageManager->make("{$this->publicDirectory}/{$srcPath}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 19 |  |         $this->optimize($scaleableImage->basePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 19 |  |         $variations = $this->scaler->getVariations($scaleableImage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 19 |  |         $image->addSrcset($image->src(), $scaleableImage->getWidth()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 19 |  |         foreach ($variations as $width => $height) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 19 |  |             if (!$width) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 19 |  |             $this->createScaledImage($image, $width, $height, $scaleableImage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 19 |  |         return $image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 19 |  |     private function createScaledImage( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         Image $image, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         int $width, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         int $height, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         ScaleableImage $scaleableImage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     ): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 19 |  |         $scaleableImageClone = clone $scaleableImage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 19 |  |         $scaledFileName = $this->createScaledFileName($image, $width, $height); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 19 |  |         $image->addSrcset($scaledFileName, $width); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 19 |  |         if ($this->cache && file_exists("{$this->publicDirectory}/{$scaledFileName}")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $scaleableImageClone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 19 |  |             ->resize($width, $height) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 19 |  |             ->save("{$this->publicDirectory}/{$scaledFileName}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 19 |  |         $this->optimize("{$this->publicDirectory}/{$scaledFileName}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 19 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 19 |  |     private function createScaledFileName(Image $image, int $width, int $height): string | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 19 |  |         $srcPath = ltrim($image->src(), '/'); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 19 |  |         $extension = pathinfo($srcPath, PATHINFO_EXTENSION); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 19 |  |         return str_replace(".{$extension}", "-{$width}x{$height}.{$extension}", $srcPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 19 |  |     private function copySourceImageToDestination(string $srcPath): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 19 |  |         $fs = new Filesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 19 |  |         $fs->copy(File::path($srcPath), "{$this->publicDirectory}/{$srcPath}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 19 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 2 |  |     private function createCachedImage(string $path): Image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 2 |  |         $image = Image::make($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 2 |  |         $extension = pathinfo($path, PATHINFO_EXTENSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 2 |  |         $imageFilePath = pathinfo($image->src(), PATHINFO_DIRNAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  |         $imageFileName = pathinfo($image->src(), PATHINFO_FILENAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 2 |  |         $srcsetFiles = Finder::create()->files() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 2 |  |             ->in($this->publicDirectory . $imageFilePath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 2 |  |             ->name("{$imageFileName}-*.{$extension}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 2 |  |         foreach ($srcsetFiles->getIterator() as $srcsetFile) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 2 |  |             $cachedFilename = $srcsetFile->getFilename(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 2 |  |             $size = (int) str_replace(".{$extension}", '', str_replace("{$imageFileName}-", '', $cachedFilename)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 2 |  |             $image->addSrcset("{$imageFilePath}/{$cachedFilename}", $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 2 |  |         return $image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 19 |  |     private function optimize(string $path): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 19 |  |         if (! $this->optimizer) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 17 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 2 |  |         $this->optimizer->optimize($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |  |