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\ImageTrueColorCreationFailedException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ImageSection. |
12
|
|
|
*/ |
13
|
|
|
abstract class ImageSection |
14
|
|
|
{ |
15
|
|
|
public const TOP = 'TOP'; |
16
|
|
|
public const BOTTOM = 'BOTTOM'; |
17
|
|
|
public const FRONT = 'FRONT'; |
18
|
|
|
public const BACK = 'BACK'; |
19
|
|
|
public const RIGHT = 'RIGHT'; |
20
|
|
|
public const LEFT = 'LEFT'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Skin Path. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected string $skinPath = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Resource with the image. |
31
|
|
|
* |
32
|
|
|
* @var resource |
33
|
|
|
*/ |
34
|
|
|
protected $imgResource; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Avatar constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $skinPath |
40
|
|
|
*/ |
41
|
|
|
public function __construct(string $skinPath) |
42
|
|
|
{ |
43
|
|
|
$this->skinPath = $skinPath; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* From resource to string. |
48
|
|
|
*/ |
49
|
|
|
public function __toString(): string |
50
|
|
|
{ |
51
|
|
|
\ob_start(); |
52
|
|
|
\imagepng($this->imgResource); |
53
|
|
|
$imgToString = (string) \ob_get_clean(); |
54
|
|
|
|
55
|
|
|
return $imgToString; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Destructor. |
60
|
|
|
*/ |
61
|
|
|
public function __destruct() |
62
|
|
|
{ |
63
|
|
|
if ($this->imgResource) { |
64
|
|
|
\imagedestroy($this->imgResource); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get generated resource image. |
70
|
|
|
* |
71
|
|
|
* @return resource |
72
|
|
|
*/ |
73
|
|
|
public function getResource() |
74
|
|
|
{ |
75
|
|
|
return $this->imgResource; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Creates imagecreatefrompng resource, it fails throws an Exception. |
80
|
|
|
* |
81
|
|
|
* @param string $path |
82
|
|
|
* |
83
|
|
|
* @throws ImageCreateFromPngFailedException |
84
|
|
|
* |
85
|
|
|
* @return resource |
86
|
|
|
*/ |
87
|
|
|
protected function createImageFromPng(string $path) |
88
|
|
|
{ |
89
|
|
|
$resource = \imagecreatefrompng($path); |
90
|
|
|
if ($resource === false) { |
91
|
|
|
throw new ImageCreateFromPngFailedException("Cannot create png image from file {$path}"); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $resource; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create imagecreatetruecolor square empty image. |
99
|
|
|
* |
100
|
|
|
* @param $size |
101
|
|
|
* |
102
|
|
|
* @throws ImageTrueColorCreationFailedException |
103
|
|
|
* |
104
|
|
|
* @return resource |
105
|
|
|
*/ |
106
|
|
|
protected function createTrueColorSquare($size) |
107
|
|
|
{ |
108
|
|
|
$square = \imagecreatetruecolor($size, $size); |
109
|
|
|
if ($square === false) { |
110
|
|
|
throw new ImageTrueColorCreationFailedException('imagecreatetruecolor failed'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $square; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $image |
118
|
|
|
* |
119
|
|
|
* @throws \Exception |
120
|
|
|
* |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
|
|
protected function colorAllocateAlpha($image): int |
124
|
|
|
{ |
125
|
|
|
$colorIdentifier = \imagecolorallocatealpha($image, 255, 255, 255, 127); |
126
|
|
|
if (!$colorIdentifier) { |
127
|
|
|
throw new \Exception(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $colorIdentifier; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|