Passed
Push — master ( 05757a...dfbf27 )
by Derek Stephen
09:55
created

GifStrategy::getFileExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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) also could return the type GdImage which is incompatible with the documented return type resource.
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, 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
}