1 | <?php |
||||||||||
2 | |||||||||||
3 | /** |
||||||||||
4 | * @copyright Bluz PHP Team |
||||||||||
5 | * @link https://github.com/bluzphp/skeleton |
||||||||||
6 | */ |
||||||||||
7 | |||||||||||
8 | /** |
||||||||||
9 | * @namespace |
||||||||||
10 | */ |
||||||||||
11 | |||||||||||
12 | namespace Image; |
||||||||||
13 | |||||||||||
14 | /** |
||||||||||
15 | * Wrapper over Gd for support some Imagick functions |
||||||||||
16 | * |
||||||||||
17 | * @category Application |
||||||||||
18 | * @package Library |
||||||||||
19 | */ |
||||||||||
20 | class Gd |
||||||||||
21 | { |
||||||||||
22 | /** |
||||||||||
23 | * @var string |
||||||||||
24 | */ |
||||||||||
25 | protected $file; |
||||||||||
26 | |||||||||||
27 | /** |
||||||||||
28 | * @var resource |
||||||||||
29 | */ |
||||||||||
30 | protected $image; |
||||||||||
31 | |||||||||||
32 | /** |
||||||||||
33 | * @var integer |
||||||||||
34 | */ |
||||||||||
35 | protected $width; |
||||||||||
36 | |||||||||||
37 | /** |
||||||||||
38 | * @var integer |
||||||||||
39 | */ |
||||||||||
40 | protected $height; |
||||||||||
41 | |||||||||||
42 | /** |
||||||||||
43 | * @var integer |
||||||||||
44 | */ |
||||||||||
45 | protected $type; |
||||||||||
46 | |||||||||||
47 | /** |
||||||||||
48 | * Compression quality for JPEG |
||||||||||
49 | * |
||||||||||
50 | * @var integer |
||||||||||
51 | */ |
||||||||||
52 | protected $quality = 86; |
||||||||||
53 | |||||||||||
54 | /** |
||||||||||
55 | * Constructor of Gd |
||||||||||
56 | * |
||||||||||
57 | * @param string $file |
||||||||||
58 | * |
||||||||||
59 | * @throws Exception |
||||||||||
60 | */ |
||||||||||
61 | public function __construct(string $file) |
||||||||||
62 | { |
||||||||||
63 | if (!file_exists($file)) { |
||||||||||
64 | throw new Exception("Image `$file` file not found"); |
||||||||||
65 | } |
||||||||||
66 | $this->file = $file; |
||||||||||
67 | |||||||||||
68 | // Retrieve image information |
||||||||||
69 | [$this->width, $this->height, $this->type] = getimagesize($file); |
||||||||||
70 | |||||||||||
71 | // Check support of file type |
||||||||||
72 | if (!(imagetypes() & $this->type)) { |
||||||||||
73 | throw new Exception('Server does not support this image type'); |
||||||||||
74 | } |
||||||||||
75 | |||||||||||
76 | // Using imagecreatefromstring will automatically detect the file type |
||||||||||
77 | $this->image = imagecreatefromstring(file_get_contents($file)); |
||||||||||
0 ignored issues
–
show
|
|||||||||||
78 | |||||||||||
79 | if (!$this->image) { |
||||||||||
80 | throw new Exception('Could not load image'); |
||||||||||
81 | } |
||||||||||
82 | } |
||||||||||
83 | |||||||||||
84 | /** |
||||||||||
85 | * @param integer $quality |
||||||||||
86 | * |
||||||||||
87 | * @return void |
||||||||||
88 | */ |
||||||||||
89 | public function setImageCompressionQuality(int $quality): void |
||||||||||
90 | { |
||||||||||
91 | $this->quality = $quality; |
||||||||||
92 | } |
||||||||||
93 | |||||||||||
94 | /** |
||||||||||
95 | * Create a crop thumbnail image from source image |
||||||||||
96 | * cropped by smaller side |
||||||||||
97 | * |
||||||||||
98 | * @param integer $width |
||||||||||
99 | * @param integer $height |
||||||||||
100 | * |
||||||||||
101 | * @return bool |
||||||||||
102 | */ |
||||||||||
103 | public function cropThumbnailImage(int $width, int $height): bool |
||||||||||
104 | { |
||||||||||
105 | // Compare image size with required thumbnail size |
||||||||||
106 | if ( |
||||||||||
107 | ($this->width < $width) && |
||||||||||
108 | ($this->height < $height) |
||||||||||
109 | ) { |
||||||||||
110 | return true; |
||||||||||
111 | } |
||||||||||
112 | |||||||||||
113 | $widthScale = round($this->width / $width); |
||||||||||
114 | $heightScale = round($this->height / $height); |
||||||||||
115 | |||||||||||
116 | if ($heightScale < $widthScale) { |
||||||||||
117 | // Crop width |
||||||||||
118 | $cropWidth = $heightScale * $width; |
||||||||||
119 | $cropHeight = $this->height; |
||||||||||
120 | $srcX = round(($this->width - $cropWidth) / 2); |
||||||||||
121 | $srcY = 0; |
||||||||||
122 | } else { |
||||||||||
123 | // Crop height |
||||||||||
124 | $cropWidth = $this->width; |
||||||||||
125 | $cropHeight = $widthScale * $height; |
||||||||||
126 | $srcX = 0; |
||||||||||
127 | $srcY = round(($this->height - $cropHeight) / 2); |
||||||||||
128 | } |
||||||||||
129 | |||||||||||
130 | $thumb = imagecreatetruecolor($width, $height); |
||||||||||
131 | |||||||||||
132 | // Copy resampled makes a smooth thumbnail |
||||||||||
133 | imagecopyresampled($thumb, $this->image, 0, 0, $srcX, $srcY, $width, $height, $cropWidth, $cropHeight); |
||||||||||
0 ignored issues
–
show
It seems like
$cropWidth can also be of type double ; however, parameter $src_w of imagecopyresampled() does only seem to accept integer , 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
![]() It seems like
$cropHeight can also be of type double ; however, parameter $src_h of imagecopyresampled() does only seem to accept integer , 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
![]() It seems like
$srcY can also be of type double ; however, parameter $src_y of imagecopyresampled() does only seem to accept integer , 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
![]() It seems like
$srcX can also be of type double ; however, parameter $src_x of imagecopyresampled() does only seem to accept integer , 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
![]() |
|||||||||||
134 | imagedestroy($this->image); |
||||||||||
135 | |||||||||||
136 | $this->width = $width; |
||||||||||
137 | $this->height = $height; |
||||||||||
138 | $this->image = $thumb; |
||||||||||
0 ignored issues
–
show
It seems like
$thumb can also be of type GdImage . However, the property $image is declared as type 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;
}
![]() |
|||||||||||
139 | |||||||||||
140 | return (bool)$thumb; |
||||||||||
141 | } |
||||||||||
142 | |||||||||||
143 | /** |
||||||||||
144 | * Save the image to a file. Type is determined from the extension |
||||||||||
145 | * |
||||||||||
146 | * @param string $fileName |
||||||||||
147 | * |
||||||||||
148 | * @return bool |
||||||||||
149 | */ |
||||||||||
150 | public function writeImage(string $fileName): bool |
||||||||||
151 | { |
||||||||||
152 | if (!$this->image || file_exists($fileName)) { |
||||||||||
153 | return false; |
||||||||||
154 | } |
||||||||||
155 | |||||||||||
156 | $ext = strtolower(substr($fileName, strrpos($fileName, '.'))); |
||||||||||
157 | |||||||||||
158 | switch ($ext) { |
||||||||||
159 | case '.gif': |
||||||||||
160 | return imagegif($this->image, $fileName); |
||||||||||
161 | // break |
||||||||||
162 | case '.jpg': |
||||||||||
163 | case '.jpeg': |
||||||||||
164 | return imagejpeg($this->image, $fileName, $this->quality); |
||||||||||
165 | // break |
||||||||||
166 | case '.png': |
||||||||||
167 | return imagepng($this->image, $fileName); |
||||||||||
168 | // break |
||||||||||
169 | case '.bmp': |
||||||||||
170 | return imagewbmp($this->image, $fileName); |
||||||||||
171 | // break |
||||||||||
172 | default: |
||||||||||
173 | return false; |
||||||||||
174 | } |
||||||||||
175 | } |
||||||||||
176 | |||||||||||
177 | /** |
||||||||||
178 | * Destroy image source |
||||||||||
179 | */ |
||||||||||
180 | public function __destruct() |
||||||||||
181 | { |
||||||||||
182 | imagedestroy($this->image); |
||||||||||
183 | } |
||||||||||
184 | } |
||||||||||
185 |
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.