【发布时间】:2017-09-20 15:53:40
【问题描述】:
我对每个图像的接受限制为 10MB,任何更大的内容都会阻止代码的执行。我不知道该怎么做。这是我尝试过的:
在控制器方法中:
// Increase memory limit before processing
ini_set('memory_limit','256M');
$base64_image = $request->get('base64_image');
$image = Image::make($base64_image);
// Returns 0, looks like we have to encode image to get file size...
$image_size = strlen((string) $image);
Log::critical('image_size file size from string: ' . $image_size);
$image = $image->encode('jpg');
// Returns byte size
$image_size = strlen((string) $image);
Log::critical('image_size file size from string: ' . $image_size);
上述方法完美适用于小图像,但问题在于大图像。我想尽早检测到大小超过 10MB 限制,以免浪费任何内存/处理时间,并向用户返回图像超出允许的文件大小限制的错误。
当我以 base64 格式发送 100MB 图像时,Laravel 会抛出错误
PostTooLargeException,因为 post base64 的大小当然是巨大的。那么如何检测到实际图像超过了 10MB 的限制,如果超过,则向用户返回正常的错误?
【问题讨论】: