Issues (11)

app/Image/ImageSection.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Minepic\Image;
6
7
use Minepic\Image\Exceptions\ImageCreateFromPngFailedException;
8
use Minepic\Image\Exceptions\ImageResourceCreationFailedException;
9
10
abstract class ImageSection
11
{
12
    protected \GdImage $skinResource;
13
14
    protected int $skinWidth;
15
16
    protected int $skinHeight;
17
    /**
18
     * Resource with the image.
19
     */
20
    protected \GdImage $imgResource;
21
22
    /**
23
     * @throws ImageCreateFromPngFailedException
24
     */
25
    public function __construct(protected string $skinPath = '')
26
    {
27
        $this->skinResource = $this->createImageFromPng($this->skinPath);
28
        $this->skinWidth = (int) imagesx($this->skinResource);
29
        $this->skinHeight = (int) imagesy($this->skinResource);
30
    }
31
32
    public function __destruct()
33
    {
34
        if ($this->imgResource instanceof \GdImage) {
0 ignored issues
show
$this->imgResource is always a sub-type of GdImage.
Loading history...
35
            imagedestroy($this->imgResource);
36
        }
37
    }
38
39
    public function __toString(): string
40
    {
41
        ob_start();
42
        imagepng($this->imgResource);
43
        $imgToString = (string) ob_get_clean();
44
45
        return $imgToString;
46
    }
47
48
    public function is64x64(): bool
49
    {
50
        return $this->skinWidth === 64 && $this->skinHeight === 64;
51
    }
52
53
    /**
54
     * Get generated resource image.
55
     */
56
    public function getResource(): \GdImage
57
    {
58
        return $this->imgResource;
59
    }
60
61
    /**
62
     * @throws ImageCreateFromPngFailedException
63
     */
64
    protected function createImageFromPng(string $path): \GdImage
65
    {
66
        $resource = imagecreatefrompng($path);
67
        if ($resource === false) {
68
            throw new ImageCreateFromPngFailedException("Cannot create png image from file {$path}");
69
        }
70
71
        return $resource;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $resource 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...
72
    }
73
74
    /**
75
     * @throws ImageResourceCreationFailedException
76
     */
77
    protected function emptyBaseImage(int $width, int $height): \GdImage
78
    {
79
        $tmpImageResource = imagecreatetruecolor($width, $height);
80
        if ($tmpImageResource === false) {
81
            throw new ImageResourceCreationFailedException('imagecreatetruecolor() failed');
82
        }
83
        imagealphablending($tmpImageResource, false);
84
        imagesavealpha($tmpImageResource, true);
85
        $transparent = (int) imagecolorallocatealpha($tmpImageResource, 255, 255, 255, 127);
86
        imagefilledrectangle($tmpImageResource, 0, 0, $width, $height, $transparent);
87
88
        return $tmpImageResource;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $tmpImageResource 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...
89
    }
90
}
91