fix: 修复上传超大尺寸图片

This commit is contained in:
kuaifan 2024-11-18 22:36:23 +08:00
parent 1055daa0e3
commit 00eb8f7b01
2 changed files with 74 additions and 5 deletions

View File

@ -1861,7 +1861,7 @@ class Base
if ($width > 0 || $height > 0) {
$scaleName = "_{WIDTH}x{HEIGHT}";
if (isset($param['scale'][2])) {
$scaleName .= "_c{$param['scale'][2]}";
$scaleName .= "_{$param['scale'][2]}";
}
}
}
@ -1870,13 +1870,14 @@ class Base
}
$fileDir = $param['path'];
$filePath = public_path($fileDir);
$fileFullPath = $filePath . $fileName;
Base::makeDir($filePath);
if (file_put_contents($filePath . $fileName, base64_decode(str_replace($res[1], '', $imgBase64)))) {
$fileSize = filesize($filePath . $fileName);
if (file_put_contents($fileFullPath, base64_decode(str_replace($res[1], '', $imgBase64)))) {
$fileSize = filesize($fileFullPath);
$array = [
"name" => $fileName, //原文件名
"size" => Base::twoFloat($fileSize / 1024, true), //大小KB
"file" => $filePath . $fileName, //文件的完整路径 "D:\www....KzZ.jpg"
"file" => $fileFullPath, //文件的完整路径 "D:\www....KzZ.jpg"
"path" => $fileDir . $fileName, //相对路径 "uploads/pic....KzZ.jpg"
"url" => Base::fillUrl($fileDir . $fileName), //完整的URL "https://.....hhsKzZ.jpg"
"thumb" => '', //缩略图(预览图) "https://.....hhsKzZ.jpg_thumb.jpg"
@ -1884,6 +1885,12 @@ class Base
"height" => -1, //图片高度
"ext" => $extension, //文件后缀名
];
// 图片验证
$res = Image::validateImage($array['file']);
if (Base::isError($res)) {
unlink($array['file']);
return $res;
}
// 图片尺寸
$paramet = getimagesize($array['file']);
$array['width'] = $paramet[0];
@ -2008,6 +2015,7 @@ class Base
return Base::retError('错误的类型参数');
}
$extension = strtolower($file->getClientOriginalExtension());
$isImage = in_array($extension, ['jpg', 'jpeg', 'webp', 'gif', 'png']);
if ($type && !in_array($extension, $type)) {
return Base::retError('文件格式错误,限制类型:' . implode(",", $type));
}
@ -2024,6 +2032,12 @@ class Base
} catch (\Throwable) {
$fileSize = 0;
}
if ($isImage) {
$res = Image::validateImage($file);
if (Base::isError($res)) {
return $res;
}
}
$scaleName = "";
if ($param['fileName'] === true) {
$fileName = $file->getClientOriginalName();
@ -2121,7 +2135,7 @@ class Base
Image::compressImage($thumbFile, null, 80);
}
}
if (in_array($array['ext'], ['jpg', 'jpeg', 'webp', 'gif', 'png'])) {
if ($isImage) {
// 获取图片尺寸
$paramet = getimagesize($array['file']);
$array['width'] = $paramet[0];

View File

@ -313,4 +313,59 @@ class Image
return false;
}
}
/** ******************************************************************************/
/** ******************************************************************************/
/** ******************************************************************************/
// ImageMagick 策略限制配置
private static $limits = [
'width' => 16384, // 16KP
'height' => 16384, // 16KP
'area' => 128000000, // 128MP (128 * 1000000 pixels)
'memory' => 256, // 256MiB
];
/**
* 验证上传的图片
* @param $file
* @return array
*/
public static function validateImage($file)
{
try {
// 获取图片信息
$imageInfo = getimagesize($file);
if ($imageInfo === false) {
return Base::retError('无法获取图片信息');
}
$width = $imageInfo[0];
$height = $imageInfo[1];
$area = $width * $height;
// 检查尺寸限制
if ($width > self::$limits['width']) {
return Base::retError(sprintf('图片宽度(%dpx)超过限制(%dpx)', $width, self::$limits['width']));
}
if ($height > self::$limits['height']) {
return Base::retError(sprintf('图片高度(%dpx)超过限制(%dpx)', $height, self::$limits['height']));
}
if ($area > self::$limits['area']) {
return Base::retError(sprintf('图片总像素(%dpx)超过限制(%dpx)', $area, self::$limits['area']));
}
// 估算内存使用每个像素约4字节
$estimatedMemory = ($area * 4) / (1024 * 1024); // 转换为 MB
if ($estimatedMemory > self::$limits['memory']) {
return Base::retError(sprintf('预计内存使用(%dMB)超过限制(%dMB)', $estimatedMemory, self::$limits['memory']));
}
return Base::retSuccess('success');
} catch (\Exception $e) {
return Base::retError('验证过程发生错误:' . $e->getMessage());
}
}
}