Laravel干预:无法从给定的二进制数据初始化



我使用Laravel 5.6让用户上传他们的个人资料照片。在生产方面,我有一些案例返回错误

无法从给定的二进制数据初始化。

从最近2000个用户上传中得到了大约5个错误(0.25%(。相当低,但这些用户是我想保留的重要用户。

我使用Dropzone作为前端,并通过Ajax Post只向Controller发送图像数据。

这是控制器:

public function savePhotos(Request $request) {
    if(!$_FILES["file"]["error"]){
        $file = $request->file('file');
        $ext = $file->getClientOriginalExtension();
        $filename = Auth::user()->id . '.' . $ext;
        $filePathAndName = 'uploaded/'.$filename;
        Storage::disk('original_images')->put($filePathAndName, file_get_contents($file -> getRealPath()));
        //generate thumbnail
        try{
            $img = Image::make(Storage::disk('original_images')->get($filePathAndName));
            $img->orientate()->fit(200, 240);
            $newImg=Image::canvas(200, 240, '#ffffff')->encode('jpg',75); //create a blank image 200x240 px with white background
            $newImg->insert($img, 'center'); //paste resized img to new blank image
            Storage::disk('thumb_images')->put($filePathAndName, (string) $newImg->encode());
        } catch (ErrorException $e) {}
    }
}

错误发生在Image::make()的行,即使它在try-catch块中。我已经检查了所有上传的文件,这里有2个错误案例。

  1. 一些入门级安卓手机上传0字节图像

  2. iPhone(iOS13/Safari(上传图像,我可以从服务器下载,并在我的Windows机器上查看。但拉拉维尔不知怎么地无法处理它们,并返回说错误。

对于那些从iPhone上传的图像,我无法分享,因为这是上传者的面部表情。不能违反隐私法。相反,我可以分享exifs。两者都是.JPEG.

从iPhone上传#1:

Camera: Apple iPhone 8
Lens:   iPhone 8 back camera 3.99mm f/1.8 Shot at 4 mm
Exposure:   Auto exposure, Program AE, 1/5 sec, f/1.8, ISO 100
Flash:  Off, Did not fire
File:   1,181 × 1,475 JPEG (1.7 megapixels)   504,666 bytes (493 kilobytes)
Color Encoding: WARNING: Embedded color profile: “(unrecognized embedded color profile 'Display P3')”
JFIF Version 1.01
Resolution  72 pixels/None
File Type   JPEG
File Type Extension jpg
MIME Type   image/jpeg
Exif Byte Order Big-endian (Motorola, MM)
Encoding Process    Baseline DCT, Huffman coding
Bits Per Sample 8
Color Components    3
File Size   493 kB
Image Size  1,181 × 1,475
Y Cb Cr Sub Sampling    YCbCr4:2:0 (2 2)

从iPhone#2上传,令人惊讶的是,图像是由佳能5D:拍摄的

Camera: Canon EOS 5D Mark II
Lens:   85 mm
Exposure:   Manual, 1/125 sec, f/9, ISO 100
Flash:  none
File:   1,800 × 2,400 JPEG (4.3 megapixels)   589,171 bytes (575 kilobytes)
Color Encoding: WARNING: Embedded color profile: “(unrecognized embedded color profile 'Display P3')”
XMP Toolkit Adobe XMP Core 5.6-c140 79.160451, 2017/05/06-01:08:21
Creator Tool    Adobe Photoshop CS6 (Windows)
Photographic Sensitivity    100
Exif Image Size 1,800 × 2,400
Make    Canon
Camera Model Name   Canon EOS 5D Mark II
Software    Adobe Photoshop CS6 (Windows)
Exposure Time   1/125
F Number    9.00
Exposure Program    Manual
ISO 100
Shutter Speed Value 1/125
Aperture Value  9.00
Exposure Compensation   0
Metering Mode   Multi-segment
Flash   No Flash
Focal Length    85.0 mm
JFIF Version    1.01
Resolution  72 pixels/None
File Type   JPEG
File Type Extension jpg
MIME Type   image/jpeg
Exif Byte Order Big-endian (Motorola, MM)
Encoding Process    Baseline DCT, Huffman coding
Bits Per Sample 8
Color Components    3
File Size   575 kB
Image Size  1,800 × 2,400
Y Cb Cr Sub Sampling    YCbCr4:2:0 (2 2)

PHP.ini

upload_max_filesize = 10M

可能是并发上传的问题,因为所有文件都使用相同的文件名。如果用户同时上传两个图像,其中一个图像已损坏,则另一个请求无法打开该图像。我建议您在上传到Storage::disk('original_images')之前先进行裁剪。

只需双击表单提交按钮就可以很容易地进行并发上传,所以这是一个常见的错误。

相关内容

  • 没有找到相关文章

最新更新