Skip to content

Commit

Permalink
Merge branch 'fix-validateImage-Psr7UploadedFile' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
k-holy committed Apr 26, 2023
2 parents ef6c177 + 7a8a15a commit 3800ecf
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,10 @@ public function validateImageSize(FileInterface $file): ?bool
if ($maxWidth === null && $maxHeight === null) {
return null;
}
$filepath = $file->getPath();
/** @noinspection PhpUnusedLocalVariableInspection */
if ((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 @@ -430,36 +431,46 @@ 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): mixed
{
$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')) {
return exif_imagetype($filepath);
$filepath = $file->getPath();
if ($filepath !== null) {
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): mixed
{
$filepath = $file->getPath();
if ($filepath === null) {
return getimagesizefromstring($file->getContent());
}
return getimagesize($filepath);
}

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

0 comments on commit 3800ecf

Please sign in to comment.