Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
5.88% |
2 / 34 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
WebPConvert | |
5.88% |
2 / 34 |
|
0.00% |
0 / 4 |
93.37 | |
0.00% |
0 / 1 |
convert | |
33.33% |
2 / 6 |
|
0.00% |
0 / 1 |
3.19 | |||
serveConverted | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
getConverterIds | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getConverterOptionDefinitions | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 |
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\Convert\ConverterFactory; |
9 | use WebPConvert\Options\OptionFactory; |
10 | use WebPConvert\Serve\ServeConvertedWebP; |
11 | use WebPConvert\Serve\ServeConvertedWebPWithErrorHandling; |
12 | |
13 | /** |
14 | * Convert images to webp and/or serve them. |
15 | * |
16 | * This class is just a couple of convenience methods for doing conversion and/or |
17 | * serving. |
18 | * |
19 | * @package WebPConvert |
20 | * @author Bjørn Rosell <it@rosell.dk> |
21 | * @since Class available since Release 2.0.0 |
22 | */ |
23 | class WebPConvert |
24 | { |
25 | |
26 | /** |
27 | * Convert jpeg or png into webp |
28 | * |
29 | * Convenience method for calling Stack::convert. |
30 | * |
31 | * @param string $source The image to convert (absolute,no backslashes) |
32 | * Image must be jpeg or png. |
33 | * @param string $destination Where to store the converted file (absolute path, no backslashes). |
34 | * @param array $options (optional) Array of named options |
35 | * The options are documented here: |
36 | * https://github.com/rosell-dk/webp-convert/blob/master/docs/v2.0/converting/options.md |
37 | * @param \WebPConvert\Loggers\BaseLogger $logger (optional) |
38 | * |
39 | * @throws \WebPConvert\Convert\Exceptions\ConversionFailedException in case conversion fails |
40 | * @return void |
41 | */ |
42 | public static function convert($source, $destination, $options = [], $logger = null) |
43 | { |
44 | if (isset($options['converter'])) { |
45 | $converter = $options['converter']; |
46 | unset($options['converter']); |
47 | $c = ConverterFactory::makeConverter($converter, $source, $destination, $options, $logger); |
48 | $c->doConvert(); |
49 | } else { |
50 | Stack::convert($source, $destination, $options, $logger); |
51 | } |
52 | } |
53 | |
54 | /** |
55 | * Serve webp image, converting first if neccessary. |
56 | * |
57 | * If an image already exists, it will be served, unless it is older or larger than the source. (If it is larger, |
58 | * the original is served, if it is older, the existing webp will be deleted and a fresh conversion will be made |
59 | * and served). In case of error, the action indicated in the 'fail' option will be triggered (default is to serve |
60 | * the original). Look up the ServeConvertedWebP:serve() and the ServeConvertedWebPWithErrorHandling::serve() |
61 | * methods to learn more. |
62 | * |
63 | * @param string $source path to source file |
64 | * @param string $destination path to destination |
65 | * @param array $options (optional) options for serving/converting. The options are documented in the |
66 | * ServeConvertedWebPWithErrorHandling::serve() method |
67 | * @param \WebPConvert\Loggers\BaseLogger $serveLogger (optional) |
68 | * @param \WebPConvert\Loggers\BaseLogger $convertLogger (optional) |
69 | * @return void |
70 | */ |
71 | public static function serveConverted( |
72 | $source, |
73 | $destination, |
74 | $options = [], |
75 | $serveLogger = null, |
76 | $convertLogger = null |
77 | ) { |
78 | //return ServeExistingOrHandOver::serveConverted($source, $destination, $options); |
79 | //if (isset($options['handle-errors']) && $options['handle-errors'] === true) { |
80 | if (isset($options['fail']) && ($options['fail'] != 'throw')) { |
81 | ServeConvertedWebPWithErrorHandling::serve($source, $destination, $options, $serveLogger, $convertLogger); |
82 | } else { |
83 | ServeConvertedWebP::serve($source, $destination, $options, $serveLogger, $convertLogger); |
84 | } |
85 | } |
86 | |
87 | /** |
88 | * Get ids of all converters available in webp-convert. |
89 | * |
90 | * @return array Array of ids. |
91 | */ |
92 | public static function getConverterIds() |
93 | { |
94 | $all = Stack::getAvailableConverters(); |
95 | $all[] = 'stack'; |
96 | return $all; |
97 | } |
98 | |
99 | /** |
100 | * Get option definitions for all converters |
101 | * |
102 | * Added in order to give GUI's a way to automatically adjust their setting screens. |
103 | * |
104 | * @param bool $filterOutOptionsWithoutUI If options without UI defined should be filtered out |
105 | * |
106 | * @return array Array of options definitions - ready to be json encoded, or whatever |
107 | * @since 2.8.0 |
108 | */ |
109 | public static function getConverterOptionDefinitions($filterOutOptionsWithoutUI = true) |
110 | { |
111 | $converterIds = self::getConverterIds(); |
112 | $result = []; |
113 | |
114 | $ewww = ConverterFactory::makeConverter('ewww', '', ''); |
115 | $result['general'] = $ewww->getGeneralOptionDefinitions($filterOutOptionsWithoutUI); |
116 | |
117 | $generalOptionHash = []; |
118 | $generalOptionIds = []; |
119 | foreach ($result['general'] as &$option) { |
120 | $generalOptionIds[] = $option['id']; |
121 | $option['unsupportedBy'] = []; |
122 | $generalOptionHash[$option['id']] = &$option; |
123 | } |
124 | //$result['general'] = $generalOptionIds; |
125 | array_unshift($result['general'], OptionFactory::createOption('converter', 'string', [ |
126 | 'title' => 'Converter', |
127 | 'description' => 'Conversion method. ' . |
128 | "Cwebp and vips are best. " . |
129 | 'the *magick are nearly as good, but only recent versions supports near-lossless. ' . |
130 | 'gd is poor, as it does not support any webp options. ' . |
131 | 'For full discussion, check the guide', |
132 | 'default' => 'stack', |
133 | 'enum' => $converterIds, |
134 | 'ui' => [ |
135 | 'component' => 'select', |
136 | 'links' => [ |
137 | [ |
138 | 'Guide', |
139 | 'https://github.com/rosell-dk/webp-convert/blob/master/docs/v1.3/converting/converters.md' |
140 | ] |
141 | ], |
142 | ] |
143 | ])->getDefinition()); |
144 | |
145 | $supportedBy = []; |
146 | $uniqueOptions = []; |
147 | |
148 | foreach ($converterIds as $converterId) { |
149 | $c = ConverterFactory::makeConverter($converterId, '', ''); |
150 | foreach ($c->getUnsupportedGeneralOptions() as $optionId) { |
151 | $generalOptionHash[$optionId]['unsupportedBy'][] = $converterId; |
152 | } |
153 | $optionDefinitions = $c->getUniqueOptionDefinitions($filterOutOptionsWithoutUI); |
154 | $uniqueOptions[$converterId] = $optionDefinitions; |
155 | } |
156 | $result['unique'] = $uniqueOptions; |
157 | return $result; |
158 | } |
159 | } |