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

@@ -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());
}
}
}