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