1 | <?php |
||||
2 | /** Created by Gorlum 09.01.2024 15:59 */ |
||||
3 | |||||
4 | namespace Tools; |
||||
5 | |||||
6 | /** |
||||
7 | * @property int $height |
||||
8 | * @property int $width |
||||
9 | */ |
||||
10 | class ImageContainer { |
||||
11 | private $height = -1; |
||||
12 | private $width = -1; |
||||
13 | |||||
14 | /** @var resource|null $image */ |
||||
15 | public $image = null; |
||||
16 | |||||
17 | /** |
||||
18 | * @param string $file |
||||
19 | * |
||||
20 | * @return static|null |
||||
21 | */ |
||||
22 | public static function load($file) { |
||||
23 | $image = @imagecreatefromstring(file_get_contents($file)); |
||||
24 | if (!$image) { |
||||
25 | return null; |
||||
26 | } |
||||
27 | |||||
28 | $that = new static(); |
||||
29 | |||||
30 | $that->image = $image; |
||||
0 ignored issues
–
show
|
|||||
31 | imagesavealpha($that->image, true); |
||||
32 | |||||
33 | $that->width = imagesx($that->image); |
||||
34 | $that->height = imagesy($that->image); |
||||
35 | |||||
36 | return $that; |
||||
37 | } |
||||
38 | |||||
39 | /** |
||||
40 | * @param string $file |
||||
41 | * |
||||
42 | * @return static|null |
||||
43 | */ |
||||
44 | public static function copyGdImage($gdImage) { |
||||
0 ignored issues
–
show
The parameter
$gdImage is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
45 | $image = @imagecreatefromstring(file_get_contents($file)); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
46 | if (!$image) { |
||||
47 | return null; |
||||
48 | } |
||||
49 | |||||
50 | $that = new static(); |
||||
51 | |||||
52 | $that->image = $image; |
||||
0 ignored issues
–
show
It seems like
$image can also be of type GdImage . However, the property $image is declared as type null|resource . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
![]() |
|||||
53 | imagesavealpha($that->image, true); |
||||
54 | |||||
55 | $that->width = imagesx($that->image); |
||||
56 | $that->height = imagesy($that->image); |
||||
57 | |||||
58 | return $that; |
||||
59 | } |
||||
60 | |||||
61 | /** |
||||
62 | * @param int $width |
||||
63 | * @param int $height |
||||
64 | * |
||||
65 | * @return static |
||||
66 | */ |
||||
67 | public static function create($width, $height) { |
||||
68 | $that = new static(); |
||||
69 | |||||
70 | $that->width = $width; |
||||
71 | $that->height = $height; |
||||
72 | |||||
73 | $that->imageReset(); |
||||
74 | |||||
75 | return $that; |
||||
76 | } |
||||
77 | |||||
78 | public function __get($property) { |
||||
79 | if (in_array($property, ['height', 'width',]) && ($this->$property === -1)) { |
||||
80 | if (isset($this->image)) { |
||||
81 | $this->width = imagesx($this->image); |
||||
82 | $this->height = imagesy($this->image); |
||||
83 | } else { |
||||
84 | $this->width = $this->height = 0; |
||||
85 | } |
||||
86 | } |
||||
87 | |||||
88 | return property_exists($this, $property) ? $this->$property : null; |
||||
89 | } |
||||
90 | |||||
91 | public function __destruct() { |
||||
92 | if (!empty($this->image)) { |
||||
93 | imagedestroy($this->image); |
||||
94 | } |
||||
95 | } |
||||
96 | |||||
97 | /** |
||||
98 | * @param ImageContainer $anImage |
||||
99 | * @param int $positionX |
||||
100 | * @param int $positionY |
||||
101 | * @param int $sourceX |
||||
102 | * @param int $sourceY |
||||
103 | * |
||||
104 | * @return bool |
||||
105 | */ |
||||
106 | public function copyFrom(ImageContainer $anImage, $positionX, $positionY, $sourceX = 0, $sourceY = 0) { |
||||
107 | return imagecopy($this->image, $anImage->image, $positionX, $positionY, $sourceX, $sourceY, $anImage->width, $anImage->height); |
||||
108 | } |
||||
109 | |||||
110 | /** |
||||
111 | * @param \GdImage|resource $anImage |
||||
112 | * @param int $positionX |
||||
113 | * @param int $positionY |
||||
114 | * @param int $sourceX |
||||
115 | * @param int $sourceY |
||||
116 | * |
||||
117 | * @return bool |
||||
118 | */ |
||||
119 | public function copyFromGd($anImage, $positionX, $positionY, $sourceX = 0, $sourceY = 0) { |
||||
120 | return imagecopy($this->image, $anImage, $positionX, $positionY, $sourceX, $sourceY, imagesx($anImage), imagesy($anImage)); |
||||
121 | } |
||||
122 | |||||
123 | /** |
||||
124 | * @param string $string |
||||
125 | * |
||||
126 | * @return bool |
||||
127 | */ |
||||
128 | public function savePng($string) { |
||||
129 | return imagepng($this->image, $string, 9); |
||||
130 | } |
||||
131 | |||||
132 | /** |
||||
133 | * @return void |
||||
134 | */ |
||||
135 | protected function imageReset() { |
||||
136 | if (!empty($this->image)) { |
||||
137 | imagedestroy($this->image); |
||||
138 | } |
||||
139 | |||||
140 | $this->image = imagecreatetruecolor($this->width, $this->height); |
||||
0 ignored issues
–
show
It seems like
imagecreatetruecolor($this->width, $this->height) can also be of type GdImage . However, the property $image is declared as type null|resource . Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
![]() |
|||||
141 | imagealphablending($this->image, true); |
||||
142 | imagesavealpha($this->image, true); |
||||
143 | $color = imagecolorallocatealpha($this->image, 0, 0, 0, 127); |
||||
144 | imagefill($this->image, 0, 0, $color); |
||||
145 | } |
||||
146 | |||||
147 | } |
||||
148 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.