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