|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebPConvert\Convert\Converters; |
|
4
|
|
|
|
|
5
|
|
|
use WebPConvert\Convert\BaseConverters\AbstractConverter; |
|
6
|
|
|
use WebPConvert\Convert\Exceptions\ConversionFailedException; |
|
7
|
|
|
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException; |
|
8
|
|
|
|
|
9
|
|
|
//use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\TargetNotFoundException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Convert images to webp using Gmagick extension. |
|
13
|
|
|
* |
|
14
|
|
|
* @package WebPConvert |
|
15
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
16
|
|
|
* @since Class available since Release 2.0.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
class Gmagick extends AbstractConverter |
|
19
|
|
|
{ |
|
20
|
|
|
public function supportsLossless() |
|
21
|
|
|
{ |
|
22
|
|
|
return false; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
protected function getOptionDefinitionsExtra() |
|
26
|
|
|
{ |
|
27
|
1 |
|
return []; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Check (general) operationality of Gmagick converter. |
|
32
|
|
|
* |
|
33
|
|
|
* Note: |
|
34
|
|
|
* It may be that Gd has been compiled without jpeg support or png support. |
|
35
|
|
|
* We do not check for this here, as the converter could still be used for the other. |
|
36
|
|
|
* |
|
37
|
|
|
* @throws SystemRequirementsNotMetException if system requirements are not met |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function checkOperationality() |
|
40
|
|
|
{ |
|
41
|
1 |
|
if (!extension_loaded('Gmagick')) { |
|
42
|
1 |
|
throw new SystemRequirementsNotMetException('Required Gmagick extension is not available.'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (!class_exists('Gmagick')) { |
|
46
|
|
|
throw new SystemRequirementsNotMetException( |
|
47
|
|
|
'Gmagick is installed, but not correctly. The class Gmagick is not available' |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$im = new \Gmagick($this->source); |
|
52
|
|
|
|
|
53
|
|
|
if (!in_array('WEBP', $im->queryformats())) { |
|
54
|
|
|
throw new SystemRequirementsNotMetException('Gmagick was compiled without WebP support.'); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check if specific file is convertable with current converter / converter settings. |
|
60
|
|
|
* |
|
61
|
|
|
* @throws SystemRequirementsNotMetException if Gmagick does not support image type |
|
62
|
|
|
*/ |
|
63
|
|
|
public function checkConvertability() |
|
64
|
|
|
{ |
|
65
|
|
|
$im = new \Gmagick(); |
|
66
|
|
|
$mimeType = $this->getMimeTypeOfSource(); |
|
67
|
|
|
switch ($mimeType) { |
|
68
|
|
|
case 'image/png': |
|
69
|
|
|
if (!in_array('PNG', $im->queryFormats())) { |
|
70
|
|
|
throw new SystemRequirementsNotMetException( |
|
71
|
|
|
'Imagick has been compiled without PNG support and can therefore not convert this PNG image.' |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
break; |
|
75
|
|
|
case 'image/jpeg': |
|
76
|
|
|
if (!in_array('JPEG', $im->queryFormats())) { |
|
77
|
|
|
throw new SystemRequirementsNotMetException( |
|
78
|
|
|
'Imagick has been compiled without Jpeg support and can therefore not convert this Jpeg image.' |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
break; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// Although this method is public, do not call directly. |
|
86
|
|
|
// You should rather call the static convert() function, defined in AbstractConverter, which |
|
87
|
|
|
// takes care of preparing stuff before calling doConvert, and validating after. |
|
88
|
|
|
protected function doActualConvert() |
|
89
|
|
|
{ |
|
90
|
|
|
|
|
91
|
|
|
$options = $this->options; |
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
|
|
$im = new \Gmagick($this->source); |
|
95
|
|
|
} catch (\Exception $e) { |
|
96
|
|
|
throw new ConversionFailedException( |
|
97
|
|
|
'Failed creating Gmagick object of file', |
|
98
|
|
|
'Failed creating Gmagick object of file: "' . $this->source . '" - Gmagick threw an exception.', |
|
99
|
|
|
$e |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/* |
|
104
|
|
|
Seems there are currently no way to set webp options |
|
105
|
|
|
As noted in the following link, it should probably be done with a $im->addDefinition() method |
|
106
|
|
|
- but that isn't exposed (yet) |
|
107
|
|
|
(TODO: see if anyone has answered...) |
|
108
|
|
|
https://stackoverflow.com/questions/47294962/how-to-write-lossless-webp-files-with-perlmagick |
|
109
|
|
|
*/ |
|
110
|
|
|
// The following two does not have any effect... How to set WebP options? |
|
111
|
|
|
//$im->setimageoption('webp', 'webp:lossless', $options['lossless'] ? 'true' : 'false'); |
|
112
|
|
|
//$im->setimageoption('WEBP', 'method', strval($options['method'])); |
|
113
|
|
|
|
|
114
|
|
|
// It seems there is no COMPRESSION_WEBP... |
|
115
|
|
|
// http://php.net/manual/en/imagick.setimagecompression.php |
|
116
|
|
|
//$im->setImageCompression(Imagick::COMPRESSION_JPEG); |
|
117
|
|
|
//$im->setImageCompression(Imagick::COMPRESSION_UNDEFINED); |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
$im->setimageformat('WEBP'); |
|
122
|
|
|
|
|
123
|
|
|
if ($options['metadata'] == 'none') { |
|
124
|
|
|
// Strip metadata and profiles |
|
125
|
|
|
$im->stripImage(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// Ps: Imagick automatically uses same quality as source, when no quality is set |
|
129
|
|
|
// This feature is however not present in Gmagick |
|
130
|
|
|
// TODO: However, it might be possible after all - see #91 |
|
131
|
|
|
$im->setcompressionquality($this->getCalculatedQuality()); |
|
132
|
|
|
|
|
133
|
|
|
try { |
|
134
|
|
|
// We call getImageBlob(). |
|
135
|
|
|
// That method is undocumented, but it is there! |
|
136
|
|
|
// - just like it is in imagick, as pointed out here: |
|
137
|
|
|
// https://www.php.net/manual/ru/gmagick.readimageblob.php |
|
138
|
|
|
|
|
139
|
|
|
/** @scrutinizer ignore-call */ |
|
140
|
|
|
$imageBlob = $im->getImageBlob(); |
|
141
|
|
|
} catch (\ImagickException $e) { |
|
142
|
|
|
throw new ConversionFailedException( |
|
143
|
|
|
'Gmagick failed converting - getImageBlob() threw an exception)', |
|
144
|
|
|
0, |
|
145
|
|
|
$e |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
//$success = $im->writeimagefile(fopen($destination, 'wb')); |
|
151
|
|
|
$success = @file_put_contents($this->destination, $imageBlob); |
|
152
|
|
|
|
|
153
|
|
|
if (!$success) { |
|
154
|
|
|
throw new ConversionFailedException('Failed writing file'); |
|
155
|
|
|
} else { |
|
156
|
|
|
//$logger->logLn('sooms we made it!'); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|