Test Failed
Push — master ( 975525...307699 )
by Bjørn
02:39
created

Vips::checkConvertability()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebPConvert\Convert\Converters;
4
5
use WebPConvert\Convert\Converters\AbstractConverter;
6
use WebPConvert\Convert\Converters\ConverterTraits\EncodingAutoTrait;
7
use WebPConvert\Convert\Exceptions\ConversionFailedException;
8
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
9
use WebPConvert\Options\BooleanOption;
10
11
//require '/home/rosell/.composer/vendor/autoload.php';
12
13
/**
14
 * Convert images to webp using Vips extension.
15
 *
16
 * @package    WebPConvert
17
 * @author     Bjørn Rosell <[email protected]>
18
 * @since      Class available since Release 2.0.0
19
 */
20
class Vips extends AbstractConverter
21
{
22
    use EncodingAutoTrait;
23
24
    protected function getUnsupportedDefaultOptions()
25
    {
26
        return [
27
            'size-in-percentage',
28
            'use-nice'
29
        ];
30 6
    }
31
32
    protected function createOptions()
33 6
    {
34
        parent::createOptions();
35
36
        $this->options2->addOptions(
37
            new BooleanOption('smart-subsample', false),
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $defaultValue of WebPConvert\Options\BooleanOption::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
            new BooleanOption('smart-subsample', /** @scrutinizer ignore-type */ false),
Loading history...
38
        );
39
    }
40
41
    /**
42 4
     * Check operationality of Vips converter.
43
     *
44 4
     * @throws SystemRequirementsNotMetException  if system requirements are not met
45 1
     */
46
    public function checkOperationality()
47
    {
48 3
        if (!extension_loaded('vips')) {
49 1
            throw new SystemRequirementsNotMetException('Required Vips extension is not available.');
50
        }
51 1
52
        if (!function_exists('vips_image_new_from_file')) {
53
            throw new SystemRequirementsNotMetException(
54
                'Vips extension seems to be installed, however something is not right: ' .
55
                'the function "vips_image_new_from_file" is not available.'
56
            );
57 2
        }
58
59
        // TODO: Should we also test if webp is available? (It seems not to be neccessary - it seems
60
        // that webp be well intergrated part of vips)
61
    }
62
63
    /**
64 1
     * Check if specific file is convertable with current converter / converter settings.
65
     *
66
     * @throws SystemRequirementsNotMetException  if Vips does not support image type
67
     */
68
    public function checkConvertability()
69 1
    {
70 1
        // It seems that png and jpeg are always supported by Vips
71
        // - so nothing needs to be done here
72 1
73 1
        if (function_exists('vips_version')) {
74
            $this->logLn('vipslib version: ' . vips_version());
75
        }
76
        $this->logLn('vips extension version: ' . phpversion('vips'));
77
    }
78
79
    /**
80
     * Create vips image resource from source file
81 1
     *
82
     * @throws  ConversionFailedException  if image resource cannot be created
83
     * @return  resource  vips image resource
84
     */
85 1
    private function createImageResource()
86 1
    {
87
        // We are currently using vips_image_new_from_file(), but we could consider
88
        // calling vips_jpegload / vips_pngload instead
89
        $result = /** @scrutinizer ignore-call */ vips_image_new_from_file($this->source, []);
90
        if ($result === -1) {
91
            /*throw new ConversionFailedException(
92
                'Failed creating new vips image from file: ' . $this->source
93
            );*/
94 1
            $message = /** @scrutinizer ignore-call */ vips_error_buffer();
95
            throw new ConversionFailedException($message);
96
        }
97
98
        if (!is_array($result)) {
99
            throw new ConversionFailedException(
100 1
                'vips_image_new_from_file did not return an array, which we expected'
101
            );
102
        }
103
104
        if (count($result) != 1) {
105
            throw new ConversionFailedException(
106
                'vips_image_new_from_file did not return an array of length 1 as we expected ' .
107 1
                '- length was: ' . count($result)
108 1
            );
109
        }
110
111
        $im = array_shift($result);
112
        return $im;
113
    }
114
115
    /**
116 3
     * Create parameters for webpsave
117
     *
118
     * @return  array  the parameters as an array
119
     */
120
    private function createParamsForVipsWebPSave()
121
    {
122
        // webpsave options are described here:
123 3
        // https://jcupitt.github.io/libvips/API/current/VipsForeignSave.html#vips-webpsave
124 3
        // Note that "method" is currently not supported (27 may 2019)
125 3
126
        $options = [
127
            "Q" => $this->getCalculatedQuality(),
128
            'lossless' => ($this->options['encoding'] == 'lossless'),
129
            'strip' => $this->options['metadata'] == 'none',
130 3
        ];
131 1
132
        // Only set the following options if they differ from the default of vipslib
133 3
        // This ensures we do not get warning if that property isn't supported
134 2
        if ($this->options['smart-subsample'] !== false) {
135
            $options['smart_subsample'] = $this->options['smart-subsample'];
136 3
        }
137
        if ($this->options['alpha-quality'] !== 100) {
138
            $options['alpha_q'] = $this->options['alpha-quality'];
139 1
        }
140 1
        if (!is_null($this->options['preset'])) {
141 1
            // preset. 0:default, 1:picture, 2:photo, 3:drawing, 4:icon, 5:text, 6:last
142
143
            $options['preset'] = array_search(
144 3
                $this->options['preset'],
145 3
                ['default', 'picture', 'photo', 'drawing', 'icon', 'text']
146
            );
147
        }
148 2
        if ($this->options['near-lossless'] !== 100) {
149
            if ($this->options['encoding'] == 'lossless') {
150
                // We only let near_lossless have effect when encoding is set to lossless
151
                // otherwise encoding=auto would not work as expected
152
                $options['near_lossless'] = true;
153 2
154
                // In Vips, the near-lossless value is controlled by Q.
155
                // this differs from how it is done in cwebp, where it is an integer.
156
                // We have chosen same option syntax as cwebp
157 3
                $options['Q'] = $this->options['near-lossless'];
158
            }
159
        }
160
161
        return $options;
162
    }
163
164
    /**
165
     * Convert with vips extension.
166
     *
167
     * Tries to create image resource and save it as webp using the calculated options.
168
     * Vips fails when a parameter is not supported, but we detect this and unset that parameter and try again
169
     * (recursively call itself until there is no more of these kind of errors).
170 1
     *
171
     * @param  resource  $im  A vips image resource to save
172 1
     * @throws  ConversionFailedException  if conversion fails.
173
     */
174 1
    private function webpsave($im, $options)
175
    {
176
        $result = /** @scrutinizer ignore-call */ vips_call('webpsave', $im, $this->destination, $options);
177
178
        if ($result === -1) {
179
            $message = /** @scrutinizer ignore-call */ vips_error_buffer();
180
181
            // If the error
182
            if (preg_match("#no property named .(.*).#", $message, $matches)) {
183
                $nameOfPropertyNotFound = $matches[1];
184
                $this->logLn(
185
                    'Your version of vipslib does not support the "' . $nameOfPropertyNotFound . '" property. ' .
186
                    'The option is ignored.'
187
                );
188
                unset($options[$nameOfPropertyNotFound]);
189
                $this->webpsave($im, $options);
190 1
            } else {
191
                throw new ConversionFailedException($message);
192
            }
193
        }
194
    }
195
196
    /**
197
     * Convert with vips extension.
198
     *
199
     * Tries to create image resource and save it as webp using the calculated options.
200
     * Vips fails when a parameter is not supported, but we detect this and unset that parameter and try again
201 1
     * (repeat until success).
202
     *
203
     * @throws  ConversionFailedException  if conversion fails.
204
     */
205
    protected function doActualConvert()
206
    {
207
/*
208
        $im = \Jcupitt\Vips\Image::newFromFile($this->source);
209
        //$im->writeToFile(__DIR__ . '/images/small-vips.webp', ["Q" => 10]);
210
211
        $im->webpsave($this->destination, [
212
            "Q" => 80,
213 1
            //'near_lossless' => true
214 1
        ]);
215 1
        return;*/
216 1
217
        $im = $this->createImageResource();
218
        $options = $this->createParamsForVipsWebPSave();
219
        $this->webpsave($im, $options);
220
    }
221
}
222