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