1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Image; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Spatie\Image\Exceptions\InvalidImageDriver; |
7
|
|
|
use Spatie\ImageOptimizer\OptimizerChainFactory; |
8
|
|
|
use Intervention\Image\ImageManagerStatic as InterventionImage; |
9
|
|
|
|
10
|
|
|
/** @mixin \Spatie\Image\Manipulations */ |
11
|
|
|
class Image |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
protected $pathToImage; |
15
|
|
|
|
16
|
|
|
/** @var \Spatie\Image\Manipulations */ |
17
|
|
|
protected $manipulations; |
18
|
|
|
|
19
|
|
|
protected $imageDriver = 'gd'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $pathToImage |
23
|
|
|
* |
24
|
|
|
* @return static |
25
|
|
|
*/ |
26
|
|
|
public static function load(string $pathToImage) |
27
|
|
|
{ |
28
|
|
|
return new static($pathToImage); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function __construct(string $pathToImage) |
32
|
|
|
{ |
33
|
|
|
$this->pathToImage = $pathToImage; |
34
|
|
|
|
35
|
|
|
$this->manipulations = new Manipulations(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $imageDriver |
40
|
|
|
* |
41
|
|
|
* @return $this |
42
|
|
|
* |
43
|
|
|
* @throws InvalidImageDriver |
44
|
|
|
*/ |
45
|
|
|
public function useImageDriver(string $imageDriver) |
46
|
|
|
{ |
47
|
|
|
if (! in_array($imageDriver, ['gd', 'imagick'])) { |
48
|
|
|
throw InvalidImageDriver::driver($imageDriver); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->imageDriver = $imageDriver; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param callable|$manipulations |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function manipulate($manipulations) |
62
|
|
|
{ |
63
|
|
|
if (is_callable($manipulations)) { |
64
|
|
|
$manipulations($this->manipulations); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($manipulations instanceof Manipulations) { |
68
|
|
|
$this->manipulations->mergeManipulations($manipulations); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function __call($name, $arguments) |
75
|
|
|
{ |
76
|
|
|
if (! method_exists($this->manipulations, $name)) { |
77
|
|
|
throw new BadMethodCallException("Manipulation `{$name}` does not exist"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->manipulations->$name(...$arguments); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getWidth(): int |
86
|
|
|
{ |
87
|
|
|
return InterventionImage::make($this->pathToImage)->width(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getHeight(): int |
91
|
|
|
{ |
92
|
|
|
return InterventionImage::make($this->pathToImage)->height(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getManipulationSequence(): ManipulationSequence |
96
|
|
|
{ |
97
|
|
|
return $this->manipulations->getManipulationSequence(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function save($outputPath = '') |
101
|
|
|
{ |
102
|
|
|
if ($outputPath == '') { |
103
|
|
|
$outputPath = $this->pathToImage; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->addFormatManipulation($outputPath); |
107
|
|
|
|
108
|
|
|
GlideConversion::create($this->pathToImage) |
109
|
|
|
->useImageDriver($this->imageDriver) |
110
|
|
|
->performManipulations($this->manipulations) |
111
|
|
|
->save($outputPath); |
112
|
|
|
|
113
|
|
|
if ($this->shouldOptimize()) { |
114
|
|
|
$optimizerChainConfiguration = $this->manipulations->getFirstManipulationArgument('optimize'); |
115
|
|
|
|
116
|
|
|
$optimizerChainConfiguration = json_decode($optimizerChainConfiguration, true); |
117
|
|
|
|
118
|
|
|
$this->performOptimization($outputPath, $optimizerChainConfiguration); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function shouldOptimize(): bool |
123
|
|
|
{ |
124
|
|
|
return ! is_null($this->manipulations->getFirstManipulationArgument('optimize')); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function performOptimization($path, array $optimizerChainConfiguration) |
128
|
|
|
{ |
129
|
|
|
$optimizerChain = OptimizerChainFactory::create(); |
130
|
|
|
|
131
|
|
|
if (count($optimizerChainConfiguration)) { |
132
|
|
|
$optimizers = array_map(function (array $optimizerOptions, string $optimizerClassName) { |
133
|
|
|
return (new $optimizerClassName)->setOptions($optimizerOptions); |
134
|
|
|
}, $optimizerChainConfiguration, array_keys($optimizerChainConfiguration)); |
135
|
|
|
|
136
|
|
|
$optimizerChain->setOptimizers($optimizers); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$optimizerChain->optimize($path); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function addFormatManipulation($outputPath) |
143
|
|
|
{ |
144
|
|
|
if ($this->manipulations->hasManipulation('format')) { |
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$inputExtension = strtolower(pathinfo($this->pathToImage, PATHINFO_EXTENSION)); |
149
|
|
|
$outputExtension = strtolower(pathinfo($outputPath, PATHINFO_EXTENSION)); |
150
|
|
|
|
151
|
|
|
if ($inputExtension === $outputExtension) { |
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$supportedFormats = ['jpg', 'png', 'gif']; |
156
|
|
|
|
157
|
|
|
if (in_array($outputExtension, $supportedFormats)) { |
158
|
|
|
$this->manipulations->format($outputExtension); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|