Encode   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 74.42%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 71
ccs 32
cts 43
cp 0.7442
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 18 3
A getFormat() 0 20 3
A getQuality() 0 14 4
1
<?php
2
3
namespace League\Glide\Manipulators;
4
5
use Intervention\Image\Image;
6
7
/**
8
 * @property string $fm
9
 * @property string $q
10
 */
11
class Encode extends BaseManipulator
12
{
13
    /**
14
     * Perform output image manipulation.
15
     * @param  Image $image The source image.
16
     * @return Image The manipulated image.
17
     */
18 2
    public function run(Image $image)
19
    {
20 2
        $format = $this->getFormat($image);
21 2
        $quality = $this->getQuality();
22
23 2
        if (in_array($format, ['jpg', 'pjpg'], true)) {
24 2
            $image = $image->getDriver()
25 2
                           ->newImage($image->width(), $image->height(), '#fff')
26 2
                           ->insert($image, 'top-left', 0, 0);
27 2
        }
28
29 2
        if ($format === 'pjpg') {
30 2
            $image->interlace();
31 2
            $format = 'jpg';
32 2
        }
33
34 2
        return $image->encode($format, $quality);
35
    }
36
37
    /**
38
     * Resolve format.
39
     * @param  Image  $image The source image.
40
     * @return string The resolved format.
41
     */
42 4
    public function getFormat(Image $image)
43
    {
44
        $allowed = [
45 4
            'gif' => 'image/gif',
46 4
            'jpg' => 'image/jpeg',
47 4
            'pjpg' => 'image/jpeg',
48 4
            'png' => 'image/png',
49 4
            'webp' => 'image/webp',
50 4
        ];
51
52 4
        if (array_key_exists($this->fm, $allowed)) {
53 4
            return $this->fm;
54
        }
55
56 2
        if ($format = array_search($image->mime(), $allowed, true)) {
57 2
            return $format;
58
        }
59
60 2
        return 'jpg';
61
    }
62
63
    /**
64
     * Resolve quality.
65
     * @return string The resolved quality.
66
     */
67 4
    public function getQuality()
68
    {
69 4
        $default = 90;
70
71 4
        if (!is_numeric($this->q)) {
72 4
            return $default;
73
        }
74
75 2
        if ($this->q < 0 or $this->q > 100) {
76 2
            return $default;
77
        }
78
79 2
        return (int) $this->q;
80
    }
81
}
82