|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pageon\Html\Image; |
|
4
|
|
|
|
|
5
|
|
|
use Intervention\Image\ImageManager; |
|
6
|
|
|
use Intervention\Image\Image as ScaleableImage; |
|
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
8
|
|
|
|
|
9
|
|
|
class ImageFactory |
|
10
|
|
|
{ |
|
11
|
|
|
private $scaler; |
|
12
|
|
|
private $sourceDirectory; |
|
13
|
|
|
private $publicDirectory; |
|
14
|
|
|
private $imageManager; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct( |
|
17
|
|
|
string $sourceDirectory, |
|
18
|
|
|
string $publicDirectory, |
|
19
|
|
|
Scaler $scaler |
|
20
|
|
|
) { |
|
21
|
|
|
$this->sourceDirectory = rtrim($sourceDirectory, '/'); |
|
22
|
|
|
$this->publicDirectory = rtrim($publicDirectory, '/'); |
|
23
|
|
|
$this->scaler = $scaler; |
|
24
|
|
|
$this->imageManager = new ImageManager([ |
|
25
|
|
|
'driver' => 'gd', |
|
26
|
|
|
]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function make( |
|
30
|
|
|
string $sourceDirectory, |
|
31
|
|
|
string $publicDirectory, |
|
32
|
|
|
Scaler $scaler |
|
33
|
|
|
): ImageFactory |
|
34
|
|
|
{ |
|
35
|
|
|
return new self($sourceDirectory, $publicDirectory, $scaler); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function create($src): Image |
|
39
|
|
|
{ |
|
40
|
|
|
$srcPath = ltrim($src, '/'); |
|
41
|
|
|
$image = Image::make($srcPath); |
|
42
|
|
|
|
|
43
|
|
|
$this->copySourceImageToDestination($srcPath); |
|
44
|
|
|
$scaleableImage = $this->imageManager->make("{$this->publicDirectory}/{$srcPath}"); |
|
45
|
|
|
|
|
46
|
|
|
$variations = $this->scaler->getVariations($scaleableImage); |
|
47
|
|
|
$image->addSrcset($image->src(), $scaleableImage->getWidth()); |
|
48
|
|
|
|
|
49
|
|
|
foreach ($variations as $width => $height) { |
|
50
|
|
|
$this->createScaledImage($image, $width, $height, $scaleableImage); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $image; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function createScaledImage( |
|
57
|
|
|
Image $image, |
|
58
|
|
|
int $width, |
|
59
|
|
|
int $height, |
|
60
|
|
|
ScaleableImage $scaleableImage |
|
61
|
|
|
) { |
|
62
|
|
|
$scaleableImageClone = clone $scaleableImage; |
|
63
|
|
|
$scaledFileName = $this->createScaledFileName($image, $width, $height); |
|
64
|
|
|
|
|
65
|
|
|
$scaleableImageClone |
|
66
|
|
|
->resize($width, $height) |
|
67
|
|
|
->save("{$this->publicDirectory}/{$scaledFileName}"); |
|
68
|
|
|
|
|
69
|
|
|
$image->addSrcset($scaledFileName, $width); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function createScaledFileName(Image $image, int $width, int $height): string |
|
73
|
|
|
{ |
|
74
|
|
|
$srcPath = ltrim($image->src(), '/'); |
|
75
|
|
|
$extension = pathinfo($srcPath, PATHINFO_EXTENSION); |
|
76
|
|
|
|
|
77
|
|
|
return str_replace(".{$extension}", "-{$width}x{$height}.{$extension}", $srcPath); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function copySourceImageToDestination(string $srcPath): void |
|
81
|
|
|
{ |
|
82
|
|
|
$fs = new Filesystem(); |
|
83
|
|
|
$fs->copy("{$this->sourceDirectory}/{$srcPath}", "{$this->publicDirectory}/{$srcPath}"); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|