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

PngStrategy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 4 1
A create() 0 3 1
A getContentType() 0 3 1
A render() 0 5 1
A handleTransparency() 0 13 1
1
<?php
2
3
namespace Del\Image\Strategy;
4
5
class PngStrategy implements ImageTypeStrategyInterface
6
{
7
    /**
8
     * @param string $filename
9
     * @return resource
10
     */
11 10
    public function create($filename)
12
    {
13 10
        return imagecreatefrompng($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
        imagepng($resource, $filename);
26 1
    }
27
28
    /**
29
     * @return string
30
     */
31 1
    public function getContentType()
32
    {
33 1
        return 'image/png';
34
    }
35
36
    /**
37
     * @param $resource
38
     */
39 7
    public function render($resource)
40
    {
41 7
        imagealphablending($resource, true);
42 7
        imagesavealpha($resource, true);
43 7
        imagepng($resource);
44 7
    }
45
46 4
    public function handleTransparency($newImage, $image)
47
    {
48
        // Set blending mode as false
49 4
        imagealphablending($newImage, false);
50
51
        // Tell it we want to save alpha channel info
52 4
        imagesavealpha($newImage, true);
53
54
        // Set the transparent color
55 4
        $color = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
56
57
        // Fill the image with nothingness
58 4
        imagefill($newImage, 0, 0, $color);
59
    }
60
}