Passed
Push — master ( 8ad17e...5319fe )
by Derek Stephen
02:06
created

GifStrategy::prepWithCustomTransparencyIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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 6
    public function create($filename)
12
    {
13 6
        return imagecreatefromgif($filename);
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, $filename, $compression)
23
    {
24 1
        unset($compression);
25 1
        imagegif($resource, $filename);
26 1
    }
27
28
    /**
29
     * @return string
30
     */
31 1
    public function getContentType()
32
    {
33 1
        return 'image/gif';
34
    }
35
36
    /**
37
     * @param $resource
38
     */
39 5
    public function render($resource)
40
    {
41 5
        imagegif($resource);
42 5
    }
43
44 3
    public function handleTransparency($newImage, $image)
45
    {
46
        // Get transparency color's index number
47 3
        $transparency = $this->getTransparencyIndex($image);
48
49
        // Is a strange index other than -1 set?
50 3
        if ($transparency >= 0) {
51 3
            $this->prepWithCustomTransparencyIndex($newImage, $image, $transparency);
52 3
        }
53 3
    }
54
55
    /**
56
     * @param resource $newImage
57
     * @param resource $image
58
     * @param int $index
59
     */
60 3
    private function prepWithCustomTransparencyIndex($newImage, $image, $index)
61
    {
62
        // Get the array of RGB vals for the transparency index
63 3
        $transparentColor = imagecolorsforindex($image, $index);
64
65
        // Now allocate the color
66 3
        $transparency = imagecolorallocate($newImage, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
67
68
        // Fill the background with the color
69 3
        imagefill($newImage, 0, 0, $transparency);
70
71
        // And set that color as the transparent one
72 3
        imagecolortransparent($newImage, $transparency);
73 3
    }
74
75
    /**
76
     * @param resource $image
77
     * @return int
78
     */
79 3
    private function getTransparencyIndex($image)
80
    {
81 3
        return imagecolortransparent($image);
82
    }
83
}