GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 788e0e...0fca6a )
by Freek
01:28
created

Image::optimize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Image;
4
5
use BadMethodCallException;
6
use ImageOptimizer\OptimizerFactory;
7
use Spatie\Image\Exceptions\InvalidImageDriver;
8
9
/** @mixin \Spatie\Image\Manipulations */
10
class Image
11
{
12
    /** @var string */
13
    protected $pathToImage;
14
15
    /** @var \Spatie\Image\Manipulations */
16
    protected $manipulations;
17
18
    protected $imageDriver = 'gd';
19
20
    /**
21
     * @param string $pathToImage
22
     *
23
     * @return static
24
     */
25
    public static function load(string $pathToImage)
26
    {
27
        return new static($pathToImage);
28
    }
29
30
    public function __construct(string $pathToImage)
31
    {
32
        $this->pathToImage = $pathToImage;
33
34
        $this->manipulations = new Manipulations();
35
    }
36
37
    /**
38
     * @param string $imageDriver
39
     *
40
     * @return $this
41
     *
42
     * @throws InvalidImageDriver
43
     */
44
    public function useImageDriver(string $imageDriver)
45
    {
46
        if (! in_array($imageDriver, ['gd', 'imagick'])) {
47
            throw InvalidImageDriver::driver($imageDriver);
48
        }
49
50
        $this->imageDriver = $imageDriver;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param callable|$manipulations
57
     *
58
     * @return $this
59
     */
60
    public function manipulate($manipulations)
61
    {
62
        if (is_callable($manipulations)) {
63
            $manipulations($this->manipulations);
64
        }
65
66
        if ($manipulations instanceof Manipulations) {
67
            $this->manipulations->mergeManipulations($manipulations);
68
        }
69
70
        return $this;
71
    }
72
73
    public function __call($name, $arguments)
74
    {
75
        if (! method_exists($this->manipulations, $name)) {
76
            throw new BadMethodCallException("Manipulation `{$name}` does not exist");
77
        }
78
79
        $this->manipulations->$name(...$arguments);
80
81
        return $this;
82
    }
83
84
    public function getManipulationSequence(): ManipulationSequence
85
    {
86
        return $this->manipulations->getManipulationSequence();
87
    }
88
89
    public function save($outputPath = '')
90
    {
91
        if ($outputPath == '') {
92
            $outputPath = $this->pathToImage;
93
        }
94
95
        $this->addFormatManipulation($outputPath);
96
97
        GlideConversion::create($this->pathToImage)
98
            ->useImageDriver($this->imageDriver)
99
            ->performManipulations($this->manipulations)
100
            ->save($outputPath);
101
102
        if ($this->manipulations->contain('optimize')) {
103
104
105
            $this->performOptimization($outputPath);
106
        }
107
    }
108
109
    protected function performOptimization($path)
110
    {
111
        $optimizationOptions = json_decode("{}", true);
112
113
        $factory = new OptimizerFactory($optimizationOptions);
114
115
        $optimizer = $factory->get();
116
117
        $optimizer->optimize($path);
118
    }
119
120
    protected function addFormatManipulation($outputPath)
121
    {
122
        if ($this->manipulations->hasManipulation('format')) {
123
            return;
124
        }
125
126
        $inputExtension = strtolower(pathinfo($this->pathToImage, PATHINFO_EXTENSION));
127
        $outputExtension = strtolower(pathinfo($outputPath, PATHINFO_EXTENSION));
128
129
        if ($inputExtension === $outputExtension) {
130
            return;
131
        }
132
133
        $supportedFormats = ['jpg', 'png', 'gif'];
134
135
        if (in_array($outputExtension, $supportedFormats)) {
136
            $this->manipulations->format($outputExtension);
137
        }
138
    }
139
}
140