Skip to content

Commit

Permalink
Merge branch 'fix-validateImage-Psr7UploadedFile-v2' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
k-holy committed Apr 26, 2023
2 parents f87e53b + e7d188b commit 71b41f7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
12 changes: 8 additions & 4 deletions composer.json
Expand Up @@ -3,7 +3,7 @@
"homepage": "https://github.com/k-holy/volcanus-file-uploader",
"type": "library",
"description": "file uploader",
"version": "2.1.1",
"version": "2.1.2",
"license": "MIT",
"authors": [
{
Expand All @@ -17,14 +17,18 @@
}
},
"require": {
"ext-bcmath": "*",
"ext-ctype": "*",
"ext-exif": "*",
"ext-fileinfo": "*",
"ext-gmp": "*",
"php": ">=7.3||>=8.0.2",
"psr/http-message": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/http-foundation": "^5.4||^6.0",
"symfony/mime": "^5.4||^6.0",
"laminas/laminas-diactoros": "^2.9"
"symfony/http-foundation": "^5.4||^6.2",
"symfony/mime": "^5.4||^6.2",
"laminas/laminas-diactoros": "^2.14||^2.25"
}
}
47 changes: 29 additions & 18 deletions src/FileValidator.php
Expand Up @@ -410,9 +410,10 @@ public function validateImageSize(FileInterface $file): ?bool
if ($maxWidth === null && $maxHeight === null) {
return null;
}
$filepath = $file->getPath();
/** @noinspection PhpUnusedLocalVariableInspection */
if (false != (list($width, $height, $type, $attr) = getimagesize($filepath))) {
$imageInfo = $this->getImageInfo($file);
if (is_array($imageInfo) && isset($imageInfo[0]) && isset($imageInfo[1])) {
$width = $imageInfo[0];
$height = $imageInfo[1];
if (!empty($maxWidth) && $width > $maxWidth) {
$this->errors['imageWidth'] = $width;
if ($throwExceptionOnValidate) {
Expand All @@ -432,37 +433,47 @@ public function validateImageSize(FileInterface $file): ?bool
return !(isset($this->errors['imageWidth']) || isset($this->errors['imageHeight']));
}
throw new \InvalidArgumentException(
sprintf('The filepath "%s" is invalid image.', $filepath)
sprintf('The file is invalid image. path:%s', $file->getPath())
);
}

/**
* 指定されたファイルのImageType定数を返します。
*
* @param FileInterface $file アップロードファイル
* @return mixed 定数値またはFALSE
* @return int|false 定数値またはFALSE
*/
private function getImageType(FileInterface $file)
{
$filepath = $file->getPath();
if ($filepath === null) {
$content = $file->getContent();
/** @noinspection PhpUnusedLocalVariableInspection */
if ((list($width, $height, $type, $attr) = getimagesizefromstring($content))) {
return $type;
}
}
if ($this->config('enableExif')) {
/** @noinspection PhpComposerExtensionStubsInspection */
return exif_imagetype($filepath);
$filepath = $file->getPath();
if ($filepath !== null) {
/** @noinspection PhpComposerExtensionStubsInspection */
return exif_imagetype($filepath);
}
}
/** @noinspection PhpUnusedLocalVariableInspection */
if ((list($width, $height, $type, $attr) = getimagesize($filepath))) {
return $type;
$imageInfo = $this->getImageInfo($file);
if (is_array($imageInfo) && isset($imageInfo[2])) {
return $imageInfo[2];
}
return false;
}

/**
* 指定されたファイルの画像情報を返します。
*
* @param FileInterface $file アップロードファイル
* @return array|false 配列またはFALSE
*/
private function getImageInfo(FileInterface $file)
{
$filepath = $file->getPath();
if ($filepath === null) {
return getimagesizefromstring($file->getContent());
}
return getimagesize($filepath);
}

/**
* 単位付きバイト数をバイト数に変換して返します。
* 2GB以上を扱うにはGMP関数またはBCMath関数が有効になっている必要があります。
Expand Down

0 comments on commit 71b41f7

Please sign in to comment.