Passed
Push — master ( 165425...e22481 )
by Mattia
04:28
created

Skin::emptyBaseImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Minepic\Image\Sections;
6
7
use Minepic\Image\Exceptions\ImageResourceCreationFailedException;
8
use Minepic\Image\ImageSection;
9
10
class Skin extends ImageSection
11
{
12
    /**
13
     * @param string $type
14
     *
15
     * @return string
16
     */
17
    private function checkType(string $type): string
18
    {
19
        if ($type !== self::BACK) {
20
            $type = self::FRONT;
21
        }
22
23
        return $type;
24
    }
25
26
    /**
27
     * @param $skinHeight
28
     *
29
     * @return int
30
     */
31
    private function checkHeight($skinHeight): int
32
    {
33
        if ($skinHeight === 0 || $skinHeight < 0 || $skinHeight > (int) env('MAX_SKINS_SIZE')) {
34
            $skinHeight = (int) env('DEFAULT_SKIN_SIZE');
35
        }
36
37
        return $skinHeight;
38
    }
39
40
    /**
41
     * Create a PNG with raw texture.
42
     *
43
     * @throws \Minepic\Image\Exceptions\ImageCreateFromPngFailedException
44
     */
45
    public function prepareTextureDownload(): void
46
    {
47
        $this->imgResource = $this->createImageFromPng($this->skinPath);
48
        \imagealphablending($this->imgResource, true);
49
        \imagesavealpha($this->imgResource, true);
50
    }
51
52
    /**
53
     * Render skin.
54
     *
55
     * @param int
56
     * @param string
57
     *
58
     * @throws \Throwable
59
     */
60
    public function render(int $skin_height = 256, $type = self::FRONT): void
61
    {
62
        $type = $this->checkType($type);
63
        $skin_height = $this->checkHeight($skin_height);
64
65
        $image = $this->createImageFromPng($this->skinPath);
66
67
        $tmpImageResource = $this->emptyBaseImage(16, 32);
68
69
        $tmpAvatar = new Avatar($this->skinPath);
70
        $tmpAvatar->render(8, $type);
71
        // Front
72
        if ($type === self::FRONT) {
73
            // Head
74
            \imagecopyresized($tmpImageResource, $tmpAvatar->getResource(), 4, 0, 0, 0, 8, 8, 8, 8);
75
            // Body Front
76
            \imagecopyresized($tmpImageResource, $image, 4, 8, 20, 20, 8, 12, 8, 12);
77
            // Right Arm (left on img)
78
            $r_arm = \imagecreatetruecolor(4, 12);
79
            \imagecopy($r_arm, $image, 0, 0, 44, 20, 4, 12);
0 ignored issues
show
Bug introduced by
It seems like $r_arm can also be of type false; however, parameter $dst_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            \imagecopy(/** @scrutinizer ignore-type */ $r_arm, $image, 0, 0, 44, 20, 4, 12);
Loading history...
80
            \imagecopyresized($tmpImageResource, $r_arm, 0, 8, 0, 0, 4, 12, 4, 12);
0 ignored issues
show
Bug introduced by
It seems like $r_arm can also be of type false; however, parameter $src_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            \imagecopyresized($tmpImageResource, /** @scrutinizer ignore-type */ $r_arm, 0, 8, 0, 0, 4, 12, 4, 12);
Loading history...
81
            // Right leg (left on img)
82
            $r_leg = \imagecreatetruecolor(4, 20);
83
            \imagecopy($r_leg, $image, 0, 0, 4, 20, 4, 12);
84
            \imagecopyresized($tmpImageResource, $r_leg, 4, 20, 0, 0, 4, 12, 4, 12);
85
        } else {
86
            // Head
87
            \imagecopyresized($tmpImageResource, $tmpAvatar->getResource(), 4, 0, 0, 0, 8, 8, 8, 8);
88
            // Body Back
89
            \imagecopyresized($tmpImageResource, $image, 4, 8, 32, 20, 8, 12, 8, 12);
90
            // Right Arm Back (left on img)
91
            $r_arm = \imagecreatetruecolor(4, 12);
92
            \imagecopy($r_arm, $image, 0, 0, 52, 20, 4, 12);
93
            \imagecopyresized($tmpImageResource, $r_arm, 0, 8, 0, 0, 4, 12, 4, 12);
94
            // Right leg Back (left on img)
95
            $r_leg = \imagecreatetruecolor(4, 20);
96
            \imagecopy($r_leg, $image, 0, 0, 12, 20, 4, 12);
97
            \imagecopyresized($tmpImageResource, $r_leg, 4, 20, 0, 0, 4, 12, 4, 12);
98
        }
99
100
        // Left Arm (right flipped)
101
        $l_arm = \imagecreatetruecolor(4, 12);
102
        for ($x = 0; $x < 4; ++$x) {
103
            \imagecopy($l_arm, $r_arm, $x, 0, 4 - $x - 1, 0, 1, 12);
0 ignored issues
show
Bug introduced by
It seems like $r_arm can also be of type false; however, parameter $src_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
            \imagecopy($l_arm, /** @scrutinizer ignore-type */ $r_arm, $x, 0, 4 - $x - 1, 0, 1, 12);
Loading history...
104
        }
105
        \imagecopyresized($tmpImageResource, $l_arm, 12, 8, 0, 0, 4, 12, 4, 12);
106
        // Left leg (right flipped)
107
        $l_leg = \imagecreatetruecolor(4, 20);
108
        for ($x = 0; $x < 4; ++$x) {
109
            \imagecopy($l_leg, $r_leg, $x, 0, 4 - $x - 1, 0, 1, 20);
110
        }
111
        \imagecopyresized($tmpImageResource, $l_leg, 8, 20, 0, 0, 4, 12, 4, 12);
112
113
        $scale = $skin_height / 32;
114
        if ($scale === 0) {
115
            $scale = 1;
116
        }
117
        $skin_width = (int) \round($scale * (16));
118
119
        $this->imgResource = $this->emptyBaseImage($skin_width, $skin_height);
120
        \imagecopyresized($this->imgResource, $tmpImageResource, 0, 0, 0, 0, $skin_width, $skin_height, 16, 32);
121
    }
122
123
    /**
124
     * @param int $width
125
     * @param int $height
126
     *
127
     * @throws ImageResourceCreationFailedException
128
     *
129
     * @return resource
130
     */
131
    private function emptyBaseImage(int $width, int $height)
132
    {
133
        $tmpImageResource = \imagecreatetruecolor($width, $height);
134
        if ($tmpImageResource === false) {
135
            throw new ImageResourceCreationFailedException('imagecreatetruecolor() failed');
136
        }
137
        \imagealphablending($tmpImageResource, false);
138
        \imagesavealpha($tmpImageResource, true);
139
        $transparent = \imagecolorallocatealpha($tmpImageResource, 255, 255, 255, 127);
140
        \imagefilledrectangle($tmpImageResource, 0, 0, $width, $height, $transparent);
141
142
        return $tmpImageResource;
143
    }
144
}
145