Completed
Push — master ( aae6d4...77a0b7 )
by Derek Stephen
02:32
created

GifStrategy::getFileExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Del\Image\Strategy;
4
5
class GifStrategy implements ImageTypeStrategyInterface
6
{
7
    /**
8
     * @param string $filename
9
     * @return resource
10
     */
11 7
    public function create(string $filename)
12
    {
13 7
        return \imagecreatefromgif($filename);
0 ignored issues
show
Bug Best Practice introduced by
The expression return imagecreatefromgif($filename) could also return false which is incompatible with the documented return type resource. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
14
    }
15
16
    /**
17
     * @param resource $resource
18
     * @param string $filename
19
     * @param int $compression
20
     * @return void
21
     */
22 1
    public function save($resource, string $filename = null, int $compression = 100): void
23
    {
24 1
        unset($compression);
25 1
        \imagegif($resource, $filename);
26 1
    }
27
28
    /**
29
     * @return string
30
     */
31 1
    public function getContentType(): string
32
    {
33 1
        return 'image/gif';
34
    }
35
36
    /**
37
     * @param resource $resource
38
     */
39 6
    public function render($resource): void
40
    {
41 6
        \imagegif($resource);
42 6
    }
43
44 4
    public function handleTransparency($newImage, $image): void
45
    {
46
        // Get transparency color's index number
47 4
        $transparency = $this->getTransparencyIndex($image);
48
49
        // Is a strange index other than -1 set?
50 4
        if ($transparency >= 0) {
51 4
            $this->prepWithCustomTransparencyIndex($newImage, $image, $transparency);
52
        }
53 4
    }
54
55
    /**
56
     * @param resource $newImage
57
     * @param resource $image
58
     * @param int $index
59
     */
60 4
    private function prepWithCustomTransparencyIndex($newImage, $image, int $index): void
61
    {
62
        // Get the array of RGB vals for the transparency index
63 4
        $transparentColor = \imagecolorsforindex($image, $index);
64
65
        // Now allocate the color
66 4
        $transparency = \imagecolorallocate($newImage, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
67
68
        // Fill the background with the color
69 4
        \imagefill($newImage, 0, 0, $transparency);
70
71
        // And set that color as the transparent one
72 4
        \imagecolortransparent($newImage, $transparency);
73 4
    }
74
75
    /**
76
     * @param resource $image
77
     * @return int
78
     */
79 4
    private function getTransparencyIndex($image): int
80
    {
81 4
        return \imagecolortransparent($image);
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getFileExtension(): string
88
    {
89
        return 'gif';
90
    }
91
}
92