1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Image; |
4
|
|
|
|
5
|
|
|
use FilesystemIterator; |
6
|
|
|
use League\Glide\Server; |
7
|
|
|
use League\Glide\ServerFactory; |
8
|
|
|
use Spatie\Image\Exceptions\CouldNotConvert; |
9
|
|
|
|
10
|
|
|
/** @private */ |
11
|
|
|
final class GlideConversion |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
private $inputImage; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $imageDriver = 'gd'; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $conversionResult = null; |
21
|
|
|
|
22
|
|
|
public static function create(string $inputImage): self |
23
|
|
|
{ |
24
|
|
|
return new self($inputImage); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function __construct(string $inputImage) |
28
|
|
|
{ |
29
|
|
|
$this->inputImage = $inputImage; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function useImageDriver(string $imageDriver): self |
33
|
|
|
{ |
34
|
|
|
$this->imageDriver = $imageDriver; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function performManipulations(Manipulations $manipulations) |
40
|
|
|
{ |
41
|
|
|
foreach ($manipulations->getManipulationSequence() as $manipulationGroup) { |
42
|
|
|
$inputFile = $this->conversionResult ?? $this->inputImage; |
43
|
|
|
|
44
|
|
|
$watermarkPath = $this->extractWatermarkPath($manipulationGroup); |
45
|
|
|
|
46
|
|
|
$glideServer = $this->createGlideServer($inputFile, $watermarkPath); |
47
|
|
|
|
48
|
|
|
$this->conversionResult = sys_get_temp_dir().DIRECTORY_SEPARATOR.$glideServer->makeImage( |
49
|
|
|
pathinfo($inputFile, PATHINFO_BASENAME), |
50
|
|
|
$this->prepareManipulations($manipulationGroup) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Removes the watermark path from the manipulationGroup and returns it. This way it can be injected into the Glide |
59
|
|
|
* server as the `watermarks` path. |
60
|
|
|
* |
61
|
|
|
* @param $manipulationGroup |
62
|
|
|
* |
63
|
|
|
* @return null|string |
64
|
|
|
*/ |
65
|
|
|
private function extractWatermarkPath(&$manipulationGroup) |
66
|
|
|
{ |
67
|
|
|
if (array_key_exists('watermark', $manipulationGroup)) { |
68
|
|
|
$watermarkPath = dirname($manipulationGroup['watermark']); |
69
|
|
|
|
70
|
|
|
$manipulationGroup['watermark'] = basename($manipulationGroup['watermark']); |
71
|
|
|
|
72
|
|
|
return $watermarkPath; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function createGlideServer($inputFile, string $watermarkPath = null): Server |
77
|
|
|
{ |
78
|
|
|
$config = [ |
79
|
|
|
'source' => dirname($inputFile), |
80
|
|
|
'cache' => sys_get_temp_dir(), |
81
|
|
|
'driver' => $this->imageDriver, |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
if ($watermarkPath) { |
|
|
|
|
85
|
|
|
$config['watermarks'] = $watermarkPath; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return ServerFactory::create($config); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function save(string $outputFile) |
92
|
|
|
{ |
93
|
|
|
if ($this->conversionResult == '') { |
94
|
|
|
copy($this->inputImage, $outputFile); |
95
|
|
|
|
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$conversionResultDirectory = pathinfo($this->conversionResult, PATHINFO_DIRNAME); |
100
|
|
|
|
101
|
|
|
rename($this->conversionResult, $outputFile); |
102
|
|
|
|
103
|
|
|
if ($this->directoryIsEmpty($conversionResultDirectory)) { |
104
|
|
|
rmdir($conversionResultDirectory); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function prepareManipulations(array $manipulationGroup): array |
109
|
|
|
{ |
110
|
|
|
$glideManipulations = []; |
111
|
|
|
|
112
|
|
|
foreach ($manipulationGroup as $name => $argument) { |
113
|
|
|
$glideManipulations[$this->convertToGlideParameter($name)] = $argument; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $glideManipulations; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function convertToGlideParameter(string $manipulationName): string |
120
|
|
|
{ |
121
|
|
|
$conversions = [ |
122
|
|
|
'width' => 'w', |
123
|
|
|
'height' => 'h', |
124
|
|
|
'blur' => 'blur', |
125
|
|
|
'pixelate' => 'pixel', |
126
|
|
|
'crop' => 'fit', |
127
|
|
|
'manualCrop' => 'crop', |
128
|
|
|
'orientation' => 'or', |
129
|
|
|
'fit' => 'fit', |
130
|
|
|
'devicePixelRatio' => 'dpr', |
131
|
|
|
'brightness' => 'bri', |
132
|
|
|
'contrast' => 'con', |
133
|
|
|
'gamma' => 'gam', |
134
|
|
|
'sharpen' => 'sharp', |
135
|
|
|
'filter' => 'filt', |
136
|
|
|
'background' => 'bg', |
137
|
|
|
'border' => 'border', |
138
|
|
|
'quality' => 'q', |
139
|
|
|
'format' => 'fm', |
140
|
|
|
'watermark' => 'mark', |
141
|
|
|
'watermarkWidth' => 'markw', |
142
|
|
|
'watermarkHeight' => 'markh', |
143
|
|
|
'watermarkFit' => 'markfit', |
144
|
|
|
'watermarkPaddingX' => 'markx', |
145
|
|
|
'watermarkPaddingY' => 'marky', |
146
|
|
|
'watermarkPosition' => 'markpos', |
147
|
|
|
'watermarkOpacity' => 'markalpha', |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
if (! isset($conversions[$manipulationName])) { |
151
|
|
|
throw CouldNotConvert::unknownManipulation($manipulationName); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $conversions[$manipulationName]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function directoryIsEmpty(string $directory): bool |
158
|
|
|
{ |
159
|
|
|
$iterator = new FilesystemIterator($directory); |
160
|
|
|
|
161
|
|
|
return ! $iterator->valid(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: