|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebPConvert; |
|
4
|
|
|
|
|
5
|
|
|
//use WebPConvert\Convert\Converters\ConverterHelper; |
|
6
|
|
|
use WebPConvert\Convert\Converters\Stack; |
|
7
|
|
|
//use WebPConvert\Serve\ServeExistingOrHandOver; |
|
8
|
|
|
use WebPConvert\Serve\ServeConvertedWebP; |
|
9
|
|
|
use WebPConvert\Serve\ServeConvertedWebPWithErrorHandling; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Convert images to webp and/or serve them. |
|
13
|
|
|
* |
|
14
|
|
|
* This class is just a couple of convenience methods for doing conversion and/or |
|
15
|
|
|
* serving. |
|
16
|
|
|
* |
|
17
|
|
|
* @package WebPConvert |
|
18
|
|
|
* @author Bjørn Rosell <[email protected]> |
|
19
|
|
|
* @since Class available since Release 2.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class WebPConvert |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Convert jpeg or png into webp |
|
26
|
|
|
* |
|
27
|
|
|
* Convenience method for calling Stack::convert. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $source The image to convert (absolute,no backslashes) |
|
30
|
|
|
* Image must be jpeg or png. |
|
31
|
|
|
* @param string $destination Where to store the converted file (absolute path, no backslashes). |
|
32
|
|
|
* @param array $options (optional) Array of named options |
|
33
|
|
|
* The following options are generally supported (individual converters provides more options): |
|
34
|
|
|
* 'quality' => (integer|"auto") Quality. If set to auto and source image is jpeg, the quality will |
|
35
|
|
|
* be set to same as source - if detectable. The detection requires |
|
36
|
|
|
* imagick or gmagick. Default: "auto". |
|
37
|
|
|
* 'max-quality' => (integer) Limit quality (relevant only if "quality" is set to "auto"). |
|
38
|
6 |
|
* Default: 85. |
|
39
|
|
|
* 'default-quality' => (integer) Default quality (used when auto detection fails). Default: 75 |
|
40
|
|
|
* 'metadata' => (string) Valid values: 'all', 'none', 'exif', 'icc', 'xmp'. |
|
41
|
|
|
* Note: Only *cwebp* supports all values. *gd* will always remove all |
|
42
|
6 |
|
* metadata. The rest can either strip all or keep all (they will keep |
|
43
|
2 |
|
* all, unless metadata is set to *none*). Default: 'none'. |
|
44
|
|
|
* 'lossless' => (boolean|"auto") Whether to convert into the lossless webp format or the lossy. |
|
45
|
|
|
* If "auto" is selected, the format that results in the smallest file |
|
46
|
|
|
* is selected (two actual conversions are made and the smallest file |
|
47
|
|
|
* wins). Note that only *cwebp* and *vips* converters supports |
|
48
|
|
|
* the lossless encoding. Converters that does not support lossless |
|
49
|
|
|
* simply always converts to lossy encoding (and "auto" will not trigger |
|
50
|
|
|
* two conversions for these). Default is "auto" when converting PNGs and |
|
51
|
|
|
* false when converting JPEGs. The reason for this default is that it is |
|
52
|
|
|
* probably rare that a JPEG is compressed better with lossless encoding |
|
53
|
|
|
* (as the jpeg format typically have been choosen only for photos and |
|
54
|
|
|
* photos almost always is best encoding with the lossy encoding. On the |
|
55
|
|
|
* other hand, graphics and icons are sometimes compressed best with |
|
56
|
|
|
* lossy encoding and sometimes best with lossless encoding). Note that |
|
57
|
|
|
* you can use the 'png' and 'jpeg' options to set this option different |
|
58
|
|
|
* for png and jpegs. Ie: ['png' => ['lossless' => 'auto'], 'jpeg' => |
|
59
|
|
|
* 'lossless' => false]]. |
|
60
|
|
|
* 'skip' => (boolean) If set to true, conversion is skipped entirely. Can for example be used |
|
61
|
|
|
* to skip pngs for certain converters. You might for example want to use |
|
62
|
|
|
* Gd for jpegs and ewww for pngs. |
|
63
|
|
|
* @param \WebPConvert\Loggers\BaseLogger $logger (optional) |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \WebPConvert\Convert\Exceptions\ConversionFailedException in case conversion fails |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function convert($source, $destination, $options = [], $logger = null) |
|
69
|
|
|
{ |
|
70
|
|
|
//return ConverterHelper::runConverterStack($source, $destination, $options, $logger); |
|
71
|
|
|
//return Convert::runConverterStack($source, $destination, $options, $logger); |
|
72
|
|
|
Stack::convert($source, $destination, $options, $logger); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Serve webp image, converting first if neccessary. |
|
77
|
|
|
* |
|
78
|
|
|
* If an image already exists, it will be served, unless it is older or larger than the source. (If it is larger, |
|
79
|
|
|
* the original is served, if it is older, the existing webp will be deleted and a fresh conversion will be made |
|
80
|
|
|
* and served). In case of error, the action indicated in the 'fail' option will be triggered (default is to serve |
|
81
|
|
|
* the original). Look up the ServeConvertedWebP:serve() and the ServeConvertedWebPWithErrorHandling::serve() |
|
82
|
|
|
* methods to learn more. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $source path to source file |
|
85
|
|
|
* @param string $destination path to destination |
|
86
|
|
|
* @param array $options (optional) options for serving/converting. The options are documented in the |
|
87
|
|
|
* ServeConvertedWebPWithErrorHandling::serve() method |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function serveConverted($source, $destination, $options = []) |
|
90
|
|
|
{ |
|
91
|
|
|
//return ServeExistingOrHandOver::serveConverted($source, $destination, $options); |
|
92
|
|
|
//if (isset($options['handle-errors']) && $options['handle-errors'] === true) { |
|
93
|
|
|
if (isset($options['fail']) && ($options['fail'] != 'throw')) { |
|
94
|
|
|
ServeConvertedWebPWithErrorHandling::serve($source, $destination, $options); |
|
95
|
|
|
} else { |
|
96
|
|
|
ServeConvertedWebP::serve($source, $destination, $options); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|