floor12 /
yii2-module-files
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * Created by PhpStorm. |
||||||
| 4 | * User: floor12 |
||||||
| 5 | * Date: 03.01.2018 |
||||||
| 6 | * Time: 19:37 |
||||||
| 7 | */ |
||||||
| 8 | |||||||
| 9 | namespace floor12\files\logic; |
||||||
| 10 | |||||||
| 11 | |||||||
| 12 | use floor12\files\models\File; |
||||||
| 13 | use floor12\files\models\FileType; |
||||||
| 14 | use Yii; |
||||||
| 15 | use yii\base\ErrorException; |
||||||
| 16 | use yii\web\BadRequestHttpException; |
||||||
| 17 | use yii\web\NotFoundHttpException; |
||||||
| 18 | |||||||
| 19 | class FileCropRotate |
||||||
| 20 | { |
||||||
| 21 | private $_file; |
||||||
| 22 | private $_width; |
||||||
| 23 | private $_height; |
||||||
| 24 | private $_top; |
||||||
| 25 | private $_left; |
||||||
| 26 | private $_rotated; |
||||||
| 27 | |||||||
| 28 | |||||||
| 29 | public function __construct(array $data) |
||||||
| 30 | { |
||||||
| 31 | $this->_file = File::findOne($data['id']); |
||||||
| 32 | |||||||
| 33 | if (!$this->_file) |
||||||
| 34 | throw new NotFoundHttpException('File not found'); |
||||||
| 35 | |||||||
| 36 | if ($this->_file->type != FileType::IMAGE) |
||||||
| 37 | throw new BadRequestHttpException('Requested file is not an image.'); |
||||||
| 38 | |||||||
| 39 | if (!file_exists($this->_file->rootPath)) |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 40 | throw new BadRequestHttpException('File not found in file storage.'); |
||||||
| 41 | |||||||
| 42 | $this->_height = (int)$data['height']; |
||||||
| 43 | $this->_width = (int)$data['width']; |
||||||
| 44 | $this->_top = (int)$data['top']; |
||||||
| 45 | $this->_left = (int)$data['left']; |
||||||
| 46 | $this->_rotated = (int)($data['rotated'] ?? 0); |
||||||
| 47 | |||||||
| 48 | if (!$this->_height && !$this->_width) { |
||||||
| 49 | list($this->_width, $this->_height) = getimagesize($this->_file->rootPath); |
||||||
|
0 ignored issues
–
show
It seems like
$this->_file->rootPath can also be of type null; however, parameter $filename of getimagesize() does only seem to accept string, 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
Loading history...
|
|||||||
| 50 | } |
||||||
| 51 | |||||||
| 52 | } |
||||||
| 53 | |||||||
| 54 | public function execute() |
||||||
| 55 | { |
||||||
| 56 | |||||||
| 57 | $src = $this->imageCreateFromAny(); |
||||||
| 58 | |||||||
| 59 | $src = imagerotate($src, -$this->_rotated, 0); |
||||||
| 60 | |||||||
| 61 | $dest = imagecreatetruecolor($this->_width, $this->_height); |
||||||
| 62 | |||||||
| 63 | imagecopy($dest, $src, 0, 0, $this->_left, $this->_top, $this->_width, $this->_height); |
||||||
| 64 | |||||||
| 65 | $newName = new PathGenerator(Yii::$app->getModule('files')->storageFullPath) . '.jpeg'; |
||||||
| 66 | |||||||
| 67 | $newPath = Yii::$app->getModule('files')->storageFullPath . '/' . $newName; |
||||||
|
0 ignored issues
–
show
Are you sure
Yii::app->getModule('files')->storageFullPath of type mixed|null|object can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 68 | |||||||
| 69 | $oldPath = $this->_file->rootPath; |
||||||
| 70 | |||||||
| 71 | imagejpeg($dest, $newPath, 80); |
||||||
| 72 | |||||||
| 73 | imagedestroy($dest); |
||||||
| 74 | |||||||
| 75 | imagedestroy($src); |
||||||
| 76 | |||||||
| 77 | $this->_file->filename = $newName; |
||||||
| 78 | $this->_file->content_type = $this->_file->mime_content_type($newPath); |
||||||
| 79 | $this->_file->size = filesize($newPath); |
||||||
| 80 | $this->_file->changeHash(); |
||||||
| 81 | if ($this->_file->save()) { |
||||||
| 82 | @unlink($oldPath); |
||||||
|
0 ignored issues
–
show
It seems like
$oldPath can also be of type null; however, parameter $filename of unlink() does only seem to accept string, 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
Loading history...
It seems like you do not handle an error condition for
unlink(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||||||
| 83 | return $this->_file->href; |
||||||
| 84 | } else |
||||||
| 85 | throw new ErrorException("Error while saving file model."); |
||||||
| 86 | |||||||
| 87 | |||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | |||||||
| 91 | /** |
||||||
| 92 | * Method to read files from any mime types |
||||||
| 93 | * @return resource |
||||||
| 94 | * @throws BadRequestHttpException |
||||||
| 95 | */ |
||||||
| 96 | |||||||
| 97 | private function imageCreateFromAny() |
||||||
| 98 | { |
||||||
| 99 | $type = exif_imagetype($this->_file->rootPath); |
||||||
|
0 ignored issues
–
show
It seems like
$this->_file->rootPath can also be of type null; however, parameter $filename of exif_imagetype() does only seem to accept string, 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
Loading history...
|
|||||||
| 100 | $allowedTypes = array( |
||||||
| 101 | 1, // [] gif |
||||||
| 102 | 2, // [] jpg |
||||||
| 103 | 3, // [] png |
||||||
| 104 | 6 // [] bmp |
||||||
| 105 | ); |
||||||
| 106 | if (!in_array($type, $allowedTypes)) { |
||||||
| 107 | throw new BadRequestHttpException('File must have GIF, JPG, PNG or BMP mime-type.'); |
||||||
| 108 | |||||||
| 109 | } |
||||||
| 110 | switch ($type) { |
||||||
| 111 | case 1 : |
||||||
| 112 | $im = imageCreateFromGif($this->_file->rootPath); |
||||||
| 113 | break; |
||||||
| 114 | case 2 : |
||||||
| 115 | $im = imageCreateFromJpeg($this->_file->rootPath); |
||||||
| 116 | break; |
||||||
| 117 | case 3 : |
||||||
| 118 | $im = imageCreateFromPng($this->_file->rootPath); |
||||||
| 119 | break; |
||||||
| 120 | case 6 : |
||||||
| 121 | $im = imageCreateFromBmp($this->_file->rootPath); |
||||||
| 122 | break; |
||||||
| 123 | } |
||||||
| 124 | return $im; |
||||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||||
| 125 | } |
||||||
| 126 | } |