Passed
Push — master ( e87575...fd66d3 )
by Bjørn
02:00
created

Vips::getOptionDefinitionsExtra()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
class Vips extends AbstractConverter
10
{
11
    protected function getOptionDefinitionsExtra()
12
    {
13
        return [];
14
    }
15
16
    /**
17
     * Check operationality of Vips converter.
18
     *
19
     * @throws SystemRequirementsNotMetException  if system requirements are not met
20
     */
21
    protected function checkOperationality()
22
    {
23
        if (!extension_loaded('vips')) {
24
            throw new SystemRequirementsNotMetException('Required Vips extension is not available.');
25
        }
26
27
        if (!function_exists('vips_image_new_from_file')) {
28
            throw new SystemRequirementsNotMetException(
29
                'Vips extension seems to be installed, however something is not right: ' .
30
                'the function "vips_image_new_from_file" is not available.'
31
            );
32
        }
33
34
        // TODO: Should we also test if webp is available? (It seems not to be neccessary - it seems
35
        // that webp be well intergrated part of vips)
36
    }
37
38
    /**
39
     * Check if specific file is convertable with current converter / converter settings.
40
     *
41
     * @throws SystemRequirementsNotMetException  if Vips does not support image type
42
     */
43
    protected function checkConvertability()
44
    {
45
        // It seems that png and jpeg are always supported by Vips
46
        // - so nothing needs to be done here
47
    }
48
49
    protected function doActualConvert()
50
    {
51
        /*
52
        $im = \Jcupitt\Vips\Image::newFromFile(__DIR__ . '/images/small.jpg');
53
        //$im->writeToFile(__DIR__ . '/images/small-vips.webp', ["Q" => 10]);
54
        $im->webpsave(__DIR__ . '/images/small-vips.webp', [
55
            "Q" => 80,
56
            'near_lossless' => true
57
        ]);
58
        return;
59
        */
60
61
        if (function_exists('vips_version')) {
62
            $this->logLn('vips version: ' . vips_version());
63
        }
64
65
        // We are currently using vips_image_new_from_file(), but we could consider
66
        // calling vips_jpegload / vips_pngload instead
67
        $result = vips_image_new_from_file($this->source, []);
0 ignored issues
show
Bug introduced by
The function vips_image_new_from_file was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

67
        $result = /** @scrutinizer ignore-call */ vips_image_new_from_file($this->source, []);
Loading history...
68
        if ($result === -1) {
69
            /*throw new ConversionFailedException(
70
                'Failed creating new vips image from file: ' . $this->source
71
            );*/
72
            $message = vips_error_buffer();
0 ignored issues
show
Bug introduced by
The function vips_error_buffer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

72
            $message = /** @scrutinizer ignore-call */ vips_error_buffer();
Loading history...
73
            throw new ConversionFailedException($message);
74
        }
75
76
        if (!is_array($result)) {
77
            throw new ConversionFailedException(
78
                'vips_image_new_from_file did not return an array, which we expected'
79
            );
80
        }
81
82
        if (count($result) != 1) {
83
            throw new ConversionFailedException(
84
                'vips_image_new_from_file did not return an array of length 1 as we expected ' .
85
                '- length was: ' . count($result)
86
            );
87
        }
88
89
        $im = array_shift($result);
90
91
        // for some reason, vips_webpsave function is unavailable on at least one system, so we
92
        // use vips_call instead.
93
94
        // webpsave options are described here:
95
        // https://jcupitt.github.io/libvips/API/current/VipsForeignSave.html#vips-webpsave
96
        $result = vips_call('webpsave', $im, $this->destination, [
0 ignored issues
show
Bug introduced by
The function vips_call was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

96
        $result = /** @scrutinizer ignore-call */ vips_call('webpsave', $im, $this->destination, [
Loading history...
97
            "Q" => $this->getCalculatedQuality(),
98
            //'lossless' => true,
99
            //'lossless' => $this->options['lossless'],       // boolean
100
            //'preset'
101
            //'smart_subsample'     // boolean
102
103
            // hm, when I use near_lossless, I get error: "no property named `near_lossless'"
104
            // btw: beware that if this is used, q must be 20, 40, 60 or 80 (according to link above)
105
            //'near_lossless' => true,   // boolean
106
107
            //'alpha_q'     // int
108
            'strip' => $this->options['metadata'] == 'none'
109
        ]);
110
        if ($result === -1) {
111
            $message = vips_error_buffer();
112
            throw new ConversionFailedException($message);
113
        }
114
    }
115
}
116