Passed
Push — master ( 943cd5...e87575 )
by Bjørn
02:27
created

Vips::doActualConvert()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 55
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 0
dl 0
loc 55
rs 9.3888
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $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

61
        $result = /** @scrutinizer ignore-call */ vips_image_new_from_file($this->source);
Loading history...
62
        if ($result === -1) {
63
            /*throw new ConversionFailedException(
64
                'Failed creating new vips image from file: ' . $this->source
65
            );*/
66
            $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

66
            $message = /** @scrutinizer ignore-call */ vips_error_buffer();
Loading history...
67
            throw new ConversionFailedException($message);
68
69
        }
70
71
        if (!is_array($result)) {
72
            throw new ConversionFailedException(
73
                'vips_image_new_from_file did not return an array, which we expected'
74
            );
75
        }
76
77
        if (count($result) != 1) {
78
            throw new ConversionFailedException(
79
                'vips_image_new_from_file did not return an array of length 1 as we expected - length was: ' . count($result)
80
            );
81
        }
82
83
        $im = array_shift($result);
84
85
        // webpsave options are described here:
86
        // https://jcupitt.github.io/libvips/API/current/VipsForeignSave.html#vips-webpsave
87
        $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

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