-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
ImageMagick.php
246 lines (212 loc) · 5.99 KB
/
ImageMagick.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
namespace Kirby\Image\Darkroom;
use Exception;
use Kirby\Filesystem\F;
use Kirby\Image\Darkroom;
use Kirby\Image\Focus;
/**
* ImageMagick
*
* @package Kirby Image
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://opensource.org/licenses/MIT
*/
class ImageMagick extends Darkroom
{
/**
* Activates imagemagick's auto-orient feature unless
* it is deactivated via the options
*/
protected function autoOrient(string $file, array $options): string|null
{
if ($options['autoOrient'] === true) {
return '-auto-orient';
}
return null;
}
/**
* Applies the blur settings
*/
protected function blur(string $file, array $options): string|null
{
if ($options['blur'] !== false) {
return '-blur ' . escapeshellarg('0x' . $options['blur']);
}
return null;
}
/**
* Keep animated gifs
*/
protected function coalesce(string $file, array $options): string|null
{
if (F::extension($file) === 'gif') {
return '-coalesce';
}
return null;
}
/**
* Creates the convert command with the right path to the binary file
*/
protected function convert(string $file, array $options): string
{
$command = escapeshellarg($options['bin']);
// default is limiting to single-threading to keep CPU usage sane
$command .= ' -limit thread ' . escapeshellarg($options['threads']);
// append input file
return $command . ' ' . escapeshellarg($file);
}
/**
* Returns additional default parameters for imagemagick
*/
protected function defaults(): array
{
return parent::defaults() + [
'bin' => 'convert',
'interlace' => false,
'threads' => 1,
];
}
/**
* Applies the correct settings for grayscale images
*/
protected function grayscale(string $file, array $options): string|null
{
if ($options['grayscale'] === true) {
return '-colorspace gray';
}
return null;
}
/**
* Applies sharpening if activated in the options.
*/
protected function sharpen(string $file, array $options): string|null
{
if (is_int($options['sharpen']) === false) {
return null;
}
$amount = max(1, min(100, $options['sharpen'])) / 100;
return '-sharpen ' . escapeshellarg('0x' . $amount);
}
/**
* Applies the correct settings for interlaced JPEGs if
* activated via options
*/
protected function interlace(string $file, array $options): string|null
{
if ($options['interlace'] === true) {
return '-interlace line';
}
return null;
}
/**
* Creates and runs the full imagemagick command
* to process the image
*
* @throws \Exception
*/
public function process(string $file, array $options = []): array
{
$options = $this->preprocess($file, $options);
$command = [];
$command[] = $this->convert($file, $options);
$command[] = $this->strip($file, $options);
$command[] = $this->interlace($file, $options);
$command[] = $this->coalesce($file, $options);
$command[] = $this->grayscale($file, $options);
$command[] = $this->autoOrient($file, $options);
$command[] = $this->resize($file, $options);
$command[] = $this->quality($file, $options);
$command[] = $this->blur($file, $options);
$command[] = $this->sharpen($file, $options);
$command[] = $this->save($file, $options);
// remove all null values and join the parts
$command = implode(' ', array_filter($command));
// try to execute the command
exec($command, $output, $return);
// log broken commands
if ($return !== 0) {
throw new Exception('The imagemagick convert command could not be executed: ' . $command);
}
return $options;
}
/**
* Applies the correct JPEG compression quality settings
*/
protected function quality(string $file, array $options): string
{
return '-quality ' . escapeshellarg($options['quality']);
}
/**
* Creates the correct options to crop or resize the image
* and translates the crop positions for imagemagick
*/
protected function resize(string $file, array $options): string
{
// simple resize
if ($options['crop'] === false) {
return '-thumbnail ' . escapeshellarg(sprintf('%sx%s!', $options['width'], $options['height']));
}
// crop based on focus point
if (Focus::isFocalPoint($options['crop']) === true) {
if ($focus = Focus::coords(
$options['crop'],
$options['sourceWidth'],
$options['sourceHeight'],
$options['width'],
$options['height']
)) {
return sprintf(
'-crop %sx%s+%s+%s -resize %sx%s^',
$focus['width'],
$focus['height'],
$focus['x1'],
$focus['y1'],
$options['width'],
$options['height']
);
}
}
// translate the gravity option into something imagemagick understands
$gravity = match ($options['crop'] ?? null) {
'top left' => 'NorthWest',
'top' => 'North',
'top right' => 'NorthEast',
'left' => 'West',
'right' => 'East',
'bottom left' => 'SouthWest',
'bottom' => 'South',
'bottom right' => 'SouthEast',
default => 'Center'
};
$command = '-thumbnail ' . escapeshellarg(sprintf('%sx%s^', $options['width'], $options['height']));
$command .= ' -gravity ' . escapeshellarg($gravity);
$command .= ' -crop ' . escapeshellarg(sprintf('%sx%s+0+0', $options['width'], $options['height']));
return $command;
}
/**
* Creates the option for the output file
*/
protected function save(string $file, array $options): string
{
if ($options['format'] !== null) {
$file = pathinfo($file, PATHINFO_DIRNAME) . '/' . pathinfo($file, PATHINFO_FILENAME) . '.' . $options['format'];
}
return escapeshellarg($file);
}
/**
* Removes all metadata from the image
*/
protected function strip(string $file, array $options): string
{
if (F::extension($file) === 'png') {
// ImageMagick does not support keeping ICC profiles while
// stripping other privacy- and security-related information,
// such as GPS data; so discard all color profiles for PNG files
// (tested with ImageMagick 7.0.11-14 Q16 x86_64 2021-05-31)
return '-strip';
}
return '';
}
}