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
|
|
|
protected $temporaryDirectory = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $pathToImage |
25
|
|
|
* |
26
|
|
|
* @return static |
27
|
|
|
*/ |
28
|
|
|
public static function load(string $pathToImage) |
29
|
|
|
{ |
30
|
|
|
return new static($pathToImage); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setTemporaryDirectory($tempDir) |
34
|
|
|
{ |
35
|
|
|
$this->temporaryDirectory = $tempDir; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function __construct(string $pathToImage) |
41
|
|
|
{ |
42
|
|
|
$this->pathToImage = $pathToImage; |
43
|
|
|
$this->manipulations = new Manipulations(); |
44
|
|
|
if (! isset($this->temporaryDirectory)) { |
45
|
|
|
$this->setTemporaryDirectory(sys_get_temp_dir()); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $imageDriver |
51
|
|
|
* |
52
|
|
|
* @return $this |
53
|
|
|
* |
54
|
|
|
* @throws InvalidImageDriver |
55
|
|
|
*/ |
56
|
|
|
public function useImageDriver(string $imageDriver) |
57
|
|
|
{ |
58
|
|
|
if (! in_array($imageDriver, ['gd', 'imagick'])) { |
59
|
|
|
throw InvalidImageDriver::driver($imageDriver); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->imageDriver = $imageDriver; |
63
|
|
|
|
64
|
|
|
InterventionImage::configure([ |
65
|
|
|
'driver' => $this->imageDriver, |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param callable|$manipulations |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function manipulate($manipulations) |
77
|
|
|
{ |
78
|
|
|
if (is_callable($manipulations)) { |
79
|
|
|
$manipulations($this->manipulations); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($manipulations instanceof Manipulations) { |
83
|
|
|
$this->manipulations->mergeManipulations($manipulations); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function __call($name, $arguments) |
90
|
|
|
{ |
91
|
|
|
if (! method_exists($this->manipulations, $name)) { |
92
|
|
|
throw new BadMethodCallException("Manipulation `{$name}` does not exist"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->manipulations->$name(...$arguments); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getWidth(): int |
101
|
|
|
{ |
102
|
|
|
return InterventionImage::make($this->pathToImage)->width(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getHeight(): int |
106
|
|
|
{ |
107
|
|
|
return InterventionImage::make($this->pathToImage)->height(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getManipulationSequence(): ManipulationSequence |
111
|
|
|
{ |
112
|
|
|
return $this->manipulations->getManipulationSequence(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function save($outputPath = '') |
116
|
|
|
{ |
117
|
|
|
if ($outputPath == '') { |
118
|
|
|
$outputPath = $this->pathToImage; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->addFormatManipulation($outputPath); |
122
|
|
|
|
123
|
|
|
GlideConversion::create($this->pathToImage) |
124
|
|
|
->setTemporaryDirectory($this->temporaryDirectory) |
125
|
|
|
->useImageDriver($this->imageDriver) |
126
|
|
|
->performManipulations($this->manipulations) |
127
|
|
|
->save($outputPath); |
128
|
|
|
|
129
|
|
|
if ($this->shouldOptimize()) { |
130
|
|
|
$optimizerChainConfiguration = $this->manipulations->getFirstManipulationArgument('optimize'); |
131
|
|
|
|
132
|
|
|
$optimizerChainConfiguration = json_decode($optimizerChainConfiguration, true); |
133
|
|
|
|
134
|
|
|
$this->performOptimization($outputPath, $optimizerChainConfiguration); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function shouldOptimize(): bool |
139
|
|
|
{ |
140
|
|
|
return ! is_null($this->manipulations->getFirstManipulationArgument('optimize')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function performOptimization($path, array $optimizerChainConfiguration) |
144
|
|
|
{ |
145
|
|
|
$optimizerChain = OptimizerChainFactory::create(); |
146
|
|
|
|
147
|
|
|
if (count($optimizerChainConfiguration)) { |
148
|
|
|
$optimizers = array_map(function (array $optimizerOptions, string $optimizerClassName) { |
149
|
|
|
return (new $optimizerClassName)->setOptions($optimizerOptions); |
150
|
|
|
}, $optimizerChainConfiguration, array_keys($optimizerChainConfiguration)); |
151
|
|
|
|
152
|
|
|
$optimizerChain->setOptimizers($optimizers); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$optimizerChain->optimize($path); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function addFormatManipulation($outputPath) |
159
|
|
|
{ |
160
|
|
|
if ($this->manipulations->hasManipulation('format')) { |
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$inputExtension = strtolower(pathinfo($this->pathToImage, PATHINFO_EXTENSION)); |
165
|
|
|
$outputExtension = strtolower(pathinfo($outputPath, PATHINFO_EXTENSION)); |
166
|
|
|
|
167
|
|
|
if ($inputExtension === $outputExtension) { |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$supportedFormats = ['jpg', 'png', 'gif']; |
172
|
|
|
|
173
|
|
|
if (in_array($outputExtension, $supportedFormats)) { |
174
|
|
|
$this->manipulations->format($outputExtension); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: