Issues (9)

src/Image/Strategy/GifStrategy.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Del\Image\Strategy;
6
7
use GdImage;
8
9
class GifStrategy implements ImageTypeStrategyInterface
10
{
11 7
    public function create(string $filename): GdImage
12
    {
13 7
        return \imagecreatefromgif($filename);
0 ignored issues
show
Bug Best Practice introduced by
The expression return imagecreatefromgif($filename) could return the type resource which is incompatible with the type-hinted return GdImage. Consider adding an additional type-check to rule them out.
Loading history...
14
    }
15
16 1
    public function save(GdImage $resource, string $filename, int $compression = 100): void
17
    {
18 1
        \imagegif($resource, $filename);
19
    }
20
21 1
    public function getContentType(): string
22
    {
23 1
        return 'image/gif';
24
    }
25
26 6
    public function render(GdImage $resource): void
27
    {
28 6
        \imagegif($resource);
29
    }
30
31 4
    public function handleTransparency(GdImage $newImage, GdImage $image): void
32
    {
33
        // Get transparency color's index number
34 4
        $transparency = $this->getTransparencyIndex($image);
35
36
        // Is a strange index other than -1 set?
37 4
        if ($transparency >= 0) {
38 4
            $this->prepWithCustomTransparencyIndex($newImage, $image, $transparency);
39
        }
40
    }
41
42 4
    private function prepWithCustomTransparencyIndex(GdImage $newImage, GdImage $image, int $index): void
43
    {
44
        // Get the array of RGB vals for the transparency index
45 4
        $transparentColor = \imagecolorsforindex($image, $index);
46
47
        // Now allocate the color
48 4
        $transparency = \imagecolorallocate($newImage, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
49
50
        // Fill the background with the color
51 4
        \imagefill($newImage, 0, 0, $transparency);
52
53
        // And set that color as the transparent one
54 4
        \imagecolortransparent($newImage, $transparency);
55
    }
56
57 4
    private function getTransparencyIndex(GdImage $image): int
58
    {
59 4
        return \imagecolortransparent($image);
60
    }
61
}
62