1 | <?php |
||
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 | 4 | public function __construct( |
|
48 | |||
49 | 4 | public static function make( |
|
57 | |||
58 | public function enableCaching(bool $cache): ImageFactory |
||
59 | { |
||
60 | $this->cache = $cache; |
||
61 | |||
62 | return $this; |
||
63 | } |
||
64 | |||
65 | 4 | public function create($src): Image |
|
66 | { |
||
67 | 4 | $srcPath = ltrim($src, '/'); |
|
68 | |||
69 | 4 | if ($this->cache && file_exists("{$this->publicDirectory}/{$srcPath}")) { |
|
70 | return $this->createCachedImage($srcPath); |
||
71 | } |
||
72 | |||
73 | 4 | $image = Image::make($srcPath); |
|
74 | |||
75 | 4 | $this->copySourceImageToDestination($srcPath); |
|
76 | |||
77 | 4 | $scaleableImage = $this->imageManager->make("{$this->publicDirectory}/{$srcPath}"); |
|
78 | |||
79 | 4 | $this->optimize($scaleableImage->basePath()); |
|
80 | |||
81 | 4 | $variations = $this->scaler->getVariations($scaleableImage); |
|
82 | |||
83 | 4 | $image->addSrcset($image->src(), $scaleableImage->getWidth()); |
|
84 | |||
85 | 4 | foreach ($variations as $width => $height) { |
|
86 | 4 | if (!$width) { |
|
87 | continue; |
||
88 | } |
||
89 | |||
90 | 4 | $this->createScaledImage($image, $width, $height, $scaleableImage); |
|
91 | } |
||
92 | |||
93 | 4 | return $image; |
|
94 | } |
||
95 | |||
96 | 4 | private function createScaledImage( |
|
118 | |||
119 | 4 | private function createScaledFileName(Image $image, int $width, int $height): string |
|
127 | |||
128 | 4 | private function copySourceImageToDestination(string $srcPath): void |
|
134 | |||
135 | private function createCachedImage(string $path): Image |
||
136 | { |
||
159 | |||
160 | 4 | private function optimize(string $path): void |
|
168 | } |
||
169 |