Passed
Push — master ( 5b0c2e...3b0a13 )
by Mattia
05:10
created

ImageSection::createTrueColorSquare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Image;
6
7
use App\Image\Exceptions\ImageTrueColorCreationFailedException;
8
9
/**
10
 * Class ImageSection.
11
 */
12
abstract class ImageSection
13
{
14
    public const TOP = 'TOP';
15
    public const BOTTOM = 'BOTTOM';
16
    public const FRONT = 'FRONT';
17
    public const BACK = 'BACK';
18
    public const RIGHT = 'RIGHT';
19
    public const LEFT = 'LEFT';
20
21
    /**
22
     * Skin Path.
23
     *
24
     * @var string
25
     */
26
    protected $skinPath = '';
27
28
    /**
29
     * Resource with the image.
30
     *
31
     * @var resource
32
     */
33
    protected $imgResource;
34
35
    /**
36
     * Avatar constructor.
37
     */
38
    public function __construct(string $skinPath)
39
    {
40
        $this->skinPath = $skinPath;
41
    }
42
43
    /**
44
     * From resource to string.
45
     */
46
    public function __toString(): string
47
    {
48
        \ob_start();
49
        \imagepng($this->imgResource);
50
        $imgToString = (string) \ob_get_clean();
51
52
        return $imgToString;
53
    }
54
55
    /**
56
     * Destructor.
57
     */
58
    public function __destruct()
59
    {
60
        if ($this->imgResource) {
61
            \imagedestroy($this->imgResource);
62
        }
63
    }
64
65
    /**
66
     * Get generated resource image.
67
     *
68
     * @return resource
69
     */
70
    public function getResource()
71
    {
72
        return $this->imgResource;
73
    }
74
75
    /**
76
     * Create imagecreatetruecolor square empty image.
77
     *
78
     * @param $size
79
     *
80
     * @throws ImageTrueColorCreationFailedException
81
     *
82
     * @return resource
83
     */
84
    protected function createTrueColorSquare($size)
85
    {
86
        $helm = \imagecreatetruecolor($size, $size);
87
        if ($helm === false) {
88
            throw new ImageTrueColorCreationFailedException();
89
        }
90
91
        return $helm;
92
    }
93
94
    /**
95
     * @param $image
96
     * @return int
97
     * @throws \Exception
98
     */
99
    protected function colorAllocateAlpha($image): int
100
    {
101
        $colorIdentifier = \imagecolorallocatealpha($image, 255, 255, 255, 127);
102
        if (!$colorIdentifier) {
103
            throw new \Exception();
104
        }
105
        return $colorIdentifier;
106
    }
107
}
108