This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Nip\Filesystem; |
||
4 | |||
5 | /** |
||
6 | * Nip Framework |
||
7 | * |
||
8 | * @category Nip |
||
9 | * @copyright 2009 Nip Framework |
||
10 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License |
||
11 | * @version SVN: $Id: Image.php 193 2009-06-23 23:11:53Z victorstanciu $ |
||
12 | */ |
||
13 | View Code Duplication | class Image extends File |
|
0 ignored issues
–
show
|
|||
14 | { |
||
15 | |||
16 | public $extensions = ["jpg", "jpeg", "gif", "png"]; |
||
17 | public $quality = 90; |
||
18 | public $type = 'jpg'; |
||
19 | public $max_width = false; |
||
20 | public $errors = []; |
||
21 | |||
22 | protected $_resource; |
||
23 | protected $_file; |
||
24 | protected $_upload; |
||
25 | protected $_width; |
||
26 | protected $_height; |
||
27 | |||
28 | /** |
||
29 | * @param array $upload |
||
30 | */ |
||
31 | public function setResourceFromUpload($upload) |
||
32 | { |
||
33 | $this->_upload = $upload; |
||
34 | $this->setResourceFromFile($upload['tmp_name']); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param string $path |
||
39 | * @return bool |
||
40 | */ |
||
41 | public function setResourceFromFile($path) |
||
42 | { |
||
43 | $this->_file = $path; |
||
44 | if (file_exists($path)) { |
||
45 | $details = getimagesize($path); |
||
46 | |||
47 | switch ($details['mime']) { |
||
48 | case 'image/gif': |
||
49 | $this->type = 'gif'; |
||
50 | if (imagetypes() & IMG_GIF) { |
||
51 | $this->_resource = imagecreatefromgif($path); |
||
52 | } |
||
53 | break; |
||
54 | case 'image/jpeg': |
||
55 | $this->type = 'jpg'; |
||
56 | if (imagetypes() & IMG_JPG) { |
||
57 | $this->_resource = imagecreatefromjpeg($path); |
||
58 | } |
||
59 | break; |
||
60 | case 'image/png': |
||
61 | $this->type = 'png'; |
||
62 | if (imagetypes() & IMG_PNG) { |
||
63 | $this->_resource = imagecreatefrompng($path); |
||
64 | } |
||
65 | break; |
||
66 | } |
||
67 | |||
68 | $this->getWidth(); |
||
69 | $this->getHeight(); |
||
70 | |||
71 | return true; |
||
72 | } else { |
||
73 | trigger_error("Cannot find file $path", E_USER_ERROR); |
||
74 | } |
||
75 | |||
76 | return false; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return int |
||
81 | */ |
||
82 | public function getWidth() |
||
83 | { |
||
84 | if (!$this->_width && $this->_resource) { |
||
85 | $this->setWidth(imagesx($this->_resource)); |
||
86 | } |
||
87 | |||
88 | return $this->_width; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param int $width |
||
93 | */ |
||
94 | public function setWidth($width) |
||
95 | { |
||
96 | $this->_width = $width; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return int |
||
101 | */ |
||
102 | public function getHeight() |
||
103 | { |
||
104 | if (!$this->_height && $this->_resource) { |
||
105 | $this->setHeight(imagesy($this->_resource)); |
||
106 | } |
||
107 | |||
108 | return $this->_height; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param int $height |
||
113 | */ |
||
114 | public function setHeight($height) |
||
115 | { |
||
116 | $this->_height = $height; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param string $name |
||
121 | */ |
||
122 | public function setBaseName($name) |
||
123 | { |
||
124 | $name = $name . '.' . $this->type; |
||
125 | $this->setName($name); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param string $name |
||
130 | */ |
||
131 | public function setName($name) |
||
132 | { |
||
133 | $this->name = $name; |
||
134 | $this->url = dirname($this->url) . '/' . $this->name; |
||
135 | $this->path = dirname($this->path) . '/' . $this->name; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return bool |
||
140 | */ |
||
141 | public function save() |
||
142 | { |
||
143 | if (Nip_File_System::instance()->createDirectory(dirname($this->path))) { |
||
144 | |||
145 | switch ($this->type) { |
||
146 | case 'png': |
||
147 | if ($this->quality > 9) { |
||
148 | if ($this->quality < 100) { |
||
149 | $this->quality = $this->quality / 10; |
||
150 | } else { |
||
151 | $this->quality = 9; |
||
152 | } |
||
153 | } |
||
154 | $this->quality = abs($this->quality - 9); |
||
0 ignored issues
–
show
It seems like
abs($this->quality - 9) can also be of type double . However, the property $quality is declared as type integer . 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;
}
![]() |
|||
155 | $this->quality = 0; |
||
156 | |||
157 | $newImg = imagecreatetruecolor($this->_width, $this->_height); |
||
158 | imagealphablending($newImg, false); |
||
159 | imagesavealpha($newImg, true); |
||
160 | |||
161 | imagecopyresampled($newImg, $this->_resource, 0, 0, 0, 0, $this->_width, $this->_height, $this->_width, $this->_height); |
||
162 | |||
163 | $return = imagepng($newImg, $this->path, $this->quality); |
||
164 | break; |
||
165 | case 'jpg': |
||
166 | default: |
||
167 | $return = imagejpeg($this->_resource, $this->path, $this->quality); |
||
168 | break; |
||
169 | } |
||
170 | |||
171 | if ($return) { |
||
172 | chmod($this->path, 0777); |
||
173 | return true; |
||
174 | } |
||
175 | $this->errors[] = 'Error saving file'; |
||
176 | } else { |
||
177 | $this->errors[] = 'Error creating directory'; |
||
178 | } |
||
179 | return false; |
||
180 | } |
||
181 | |||
182 | public function grayscaleFade() |
||
183 | { |
||
184 | $this->grayscaleFilter(); |
||
185 | imagefilter($this->_resource, IMG_FILTER_BRIGHTNESS, 50); |
||
186 | } |
||
187 | |||
188 | public function grayscaleFilter() |
||
189 | { |
||
190 | imagefilter($this->_resource, IMG_FILTER_GRAYSCALE); |
||
191 | } |
||
192 | |||
193 | public function resize($max_width = false, $max_height = false) |
||
194 | { |
||
195 | if (!$max_width) { |
||
196 | if ($this->max_width) { |
||
197 | $max_width = $this->max_width; |
||
198 | } else { |
||
199 | $max_width = $this->getWidth(); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | if (!$max_height) { |
||
204 | if ($this->max_height) { |
||
0 ignored issues
–
show
The property
max_height does not seem to exist. Did you mean _height ?
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading. ![]() |
|||
205 | $max_height = $this->max_height; |
||
0 ignored issues
–
show
The property
max_height does not seem to exist. Did you mean _height ?
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name. If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading. ![]() |
|||
206 | } else { |
||
207 | $max_height = $this->getHeight(); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | $ratio = $this->getRatio(); |
||
212 | $target_ratio = $max_width / $max_height; |
||
213 | |||
214 | if ($ratio > $target_ratio) { |
||
215 | $new_width = $max_width; |
||
216 | $new_height = round($max_width / $ratio); |
||
217 | } else { |
||
218 | $new_height = $max_height; |
||
219 | $new_width = round($max_height * $ratio); |
||
220 | } |
||
221 | |||
222 | $image = imagecreatetruecolor($new_width, $new_height); |
||
223 | imagealphablending($image, false); |
||
224 | imagesavealpha($image, true); |
||
225 | |||
226 | imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight()); |
||
227 | |||
228 | $this->_width = $new_width; |
||
229 | $this->_height = $new_height; |
||
230 | $this->_resource = $image; |
||
231 | |||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | public function getRatio() |
||
236 | { |
||
237 | return $this->getWidth() / $this->getHeight(); |
||
238 | } |
||
239 | |||
240 | public function cropToCenter($cWidth, $cHeight) |
||
241 | { |
||
242 | |||
243 | $this->resizeToLarge($cWidth, $cHeight); |
||
244 | |||
245 | $width = $this->getWidth(); |
||
246 | $height = $this->getHeight(); |
||
247 | |||
248 | $x0 = round(abs(($width - $cWidth) / 2), 0); |
||
249 | $y0 = round(abs(($height - $cHeight) / 2), 0); |
||
250 | |||
251 | $this->crop($x0, $y0, $cWidth, $cHeight, $cWidth, $cHeight); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @param bool|int $max_width |
||
256 | * @param bool|int $max_height |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function resizeToLarge($max_width = false, $max_height = false) |
||
260 | { |
||
261 | if (!$max_width) { |
||
262 | $max_width = $this->getWidth(); |
||
263 | } |
||
264 | |||
265 | if (!$max_height) { |
||
266 | $max_height = $this->getHeight(); |
||
267 | } |
||
268 | |||
269 | $sourceRatio = $this->getRatio(); |
||
270 | $target_ratio = $max_width / $max_height; |
||
271 | |||
272 | if ($sourceRatio > $target_ratio) { |
||
273 | $new_height = $max_height; |
||
274 | $new_width = ( int )($max_height * $sourceRatio); |
||
275 | } else { |
||
276 | $new_width = $max_width; |
||
277 | $new_height = ( int )($max_width / $sourceRatio); |
||
278 | } |
||
279 | |||
280 | $image = imagecreatetruecolor($new_width, $new_height); |
||
281 | imagealphablending($image, false); |
||
282 | imagesavealpha($image, true); |
||
283 | |||
284 | imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight()); |
||
285 | |||
286 | $this->_width = $new_width; |
||
287 | $this->_height = $new_height; |
||
288 | $this->_resource = $image; |
||
289 | |||
290 | return $this; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param $x |
||
295 | * @param $y |
||
296 | * @param $dwidth |
||
297 | * @param $dheight |
||
298 | * @param $swidth |
||
299 | * @param $sheight |
||
300 | */ |
||
301 | public function crop($x, $y, $dwidth, $dheight, $swidth, $sheight) |
||
302 | { |
||
303 | $image = imagecreatetruecolor($dwidth, $dheight); |
||
304 | imagealphablending($image, false); |
||
305 | imagesavealpha($image, true); |
||
306 | |||
307 | imagecopyresampled($image, $this->_resource, |
||
308 | 0, 0, |
||
309 | $x, $y, |
||
310 | $dwidth, $dheight, |
||
311 | $swidth, $sheight); |
||
312 | |||
313 | $this->_width = $dwidth; |
||
314 | $this->_height = $dheight; |
||
315 | $this->_resource = $image; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param int $amount |
||
320 | * @param float $radius |
||
321 | * @param int $threshold |
||
322 | * @return $this |
||
323 | */ |
||
324 | public function unsharpMask($amount = 80, $radius = 0.5, $threshold = 3) |
||
325 | { |
||
326 | $img = &$this->_resource; |
||
327 | |||
328 | if ($amount > 500) { |
||
329 | $amount = 500; |
||
330 | } |
||
331 | $amount = $amount * 0.016; |
||
332 | if ($radius > 50) { |
||
333 | $radius = 50; |
||
334 | } |
||
335 | $radius = $radius * 2; |
||
336 | if ($threshold > 255) { |
||
337 | $threshold = 255; |
||
338 | } |
||
339 | |||
340 | $radius = abs(round($radius)); |
||
341 | if ($radius == 0) { |
||
342 | return; |
||
343 | } |
||
344 | |||
345 | $w = $this->_width; |
||
346 | $h = $this->_height; |
||
347 | |||
348 | $imgCanvas = imagecreatetruecolor($w, $h); |
||
349 | $imgBlur = imagecreatetruecolor($w, $h); |
||
350 | |||
351 | if (function_exists('imageconvolution')) { |
||
352 | $matrix = [[1, 2, 1], [2, 4, 2], [1, 2, 1]]; |
||
353 | imagecopy($imgBlur, $img, 0, 0, 0, 0, $w, $h); |
||
354 | imageconvolution($imgBlur, $matrix, 16, 0); |
||
355 | } else { |
||
356 | for ($i = 0; $i < $radius; $i++) { |
||
357 | imagecopy($imgBlur, $img, 0, 0, 1, 0, $w - 1, $h); |
||
358 | imagecopymerge($imgBlur, $img, 1, 0, 0, 0, $w, $h, 50); |
||
359 | imagecopymerge($imgBlur, $img, 0, 0, 0, 0, $w, $h, 50); |
||
360 | imagecopy($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h); |
||
361 | |||
362 | imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333); |
||
363 | imagecopymerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 25); |
||
364 | } |
||
365 | } |
||
366 | |||
367 | if ($threshold > 0) { |
||
368 | for ($x = 0; $x < $w - 1; $x++) { |
||
369 | for ($y = 0; $y < $h; $y++) { |
||
370 | |||
371 | $rgbOrig = ImageColorAt($img, $x, $y); |
||
372 | $rOrig = (($rgbOrig >> 16) & 0xFF); |
||
373 | $gOrig = (($rgbOrig >> 8) & 0xFF); |
||
374 | $bOrig = ($rgbOrig & 0xFF); |
||
375 | |||
376 | $rgbBlur = ImageColorAt($imgBlur, $x, $y); |
||
377 | |||
378 | $rBlur = (($rgbBlur >> 16) & 0xFF); |
||
379 | $gBlur = (($rgbBlur >> 8) & 0xFF); |
||
380 | $bBlur = ($rgbBlur & 0xFF); |
||
381 | |||
382 | $rNew = (abs($rOrig - $rBlur) >= $threshold) ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig; |
||
383 | $gNew = (abs($gOrig - $gBlur) >= $threshold) ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig; |
||
384 | $bNew = (abs($bOrig - $bBlur) >= $threshold) ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig; |
||
385 | |||
386 | if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) { |
||
387 | $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew); |
||
388 | ImageSetPixel($img, $x, $y, $pixCol); |
||
389 | } |
||
390 | } |
||
391 | } |
||
392 | } else { |
||
393 | for ($x = 0; $x < $w; $x++) { |
||
394 | for ($y = 0; $y < $h; $y++) { |
||
395 | $rgbOrig = ImageColorAt($img, $x, $y); |
||
396 | $rOrig = (($rgbOrig >> 16) & 0xFF); |
||
397 | $gOrig = (($rgbOrig >> 8) & 0xFF); |
||
398 | $bOrig = ($rgbOrig & 0xFF); |
||
399 | |||
400 | $rgbBlur = ImageColorAt($imgBlur, $x, $y); |
||
401 | |||
402 | $rBlur = (($rgbBlur >> 16) & 0xFF); |
||
403 | $gBlur = (($rgbBlur >> 8) & 0xFF); |
||
404 | $bBlur = ($rgbBlur & 0xFF); |
||
405 | |||
406 | $rNew = ($amount * ($rOrig - $rBlur)) + $rOrig; |
||
407 | if ($rNew > 255) { |
||
408 | $rNew = 255; |
||
409 | } elseif ($rNew < 0) { |
||
410 | $rNew = 0; |
||
411 | } |
||
412 | $gNew = ($amount * ($gOrig - $gBlur)) + $gOrig; |
||
413 | if ($gNew > 255) { |
||
414 | $gNew = 255; |
||
415 | } elseif ($gNew < 0) { |
||
416 | $gNew = 0; |
||
417 | } |
||
418 | $bNew = ($amount * ($bOrig - $bBlur)) + $bOrig; |
||
419 | if ($bNew > 255) { |
||
420 | $bNew = 255; |
||
421 | } elseif ($bNew < 0) { |
||
422 | $bNew = 0; |
||
423 | } |
||
424 | $rgbNew = ($rNew << 16) + ($gNew << 8) + $bNew; |
||
425 | ImageSetPixel($img, $x, $y, $rgbNew); |
||
426 | } |
||
427 | } |
||
428 | } |
||
429 | |||
430 | imagedestroy($imgCanvas); |
||
431 | imagedestroy($imgBlur); |
||
432 | |||
433 | return $this; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * @param Image $image |
||
438 | * @return $this |
||
439 | */ |
||
440 | public function copyResource($image) |
||
441 | { |
||
442 | $this->_width = $image->getWidth(); |
||
443 | $this->_height = $image->getHeight(); |
||
444 | $this->_resource = $image->getResource(); |
||
445 | |||
446 | return $this; |
||
447 | } |
||
448 | |||
449 | public function getResource() |
||
450 | { |
||
451 | return $this->_resource; |
||
452 | } |
||
453 | |||
454 | public function setResource($gdImage) |
||
455 | { |
||
456 | $this->_resource = $gdImage; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * @return mixed |
||
461 | */ |
||
462 | public function getFile() |
||
463 | { |
||
464 | return $this->_file; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * @return string |
||
469 | */ |
||
470 | public function getExtension() |
||
471 | { |
||
472 | return Nip_File_System::instance()->getExtension($this->path); |
||
473 | } |
||
474 | |||
475 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.