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

Laravel Intervention: Unable to init from given binary data

我正在使用 Laravel 5.6 让用户上传他们的个人资料照片。在生产中,我遇到了一些 returned error

Unable to init from given binary data.

最近 2,000 位用户上传的内容中有大约 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. 部分入门级Android手机上传0字节图片.

  2. iPhones (iOS13/Safari) 上传图像,我可以从服务器下载它们,并在我的 Windows 机器上查看。但是 Laravel 不知何故无法处理它们,并且 return 说错误。

对于从iPhone上传的那些图片,我不能分享它们,因为它是上传者的面部。不能违反隐私法。相反,我可以分享 exif。两者都是.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').

之前进行裁剪

只需双击表单提交按钮即可轻松进行并发上传,因此这是一个常见错误。