【问题标题】:Laravel Intervention: Unable to init from given binary dataLaravel 干预:无法从给定的二进制数据初始化
【发布时间】:2020-07-22 13:50:53
【问题描述】:

我正在使用 Laravel 5.6 让用户上传他们的个人资料照片。在生产中,我遇到了一些返回错误的案例

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

最近 2,000 个用户上传的内容中有 5 个错误 (0.25%)。很低,但这些用户是我想保留的重要用户。

我使用 Dropzone 作为前端,仅通过 Ajax Post 将图像数据发送到控制器。

这里是控制器:

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 机器上查看。但是 Laravel 不知何故无法处理它们,并返回上述错误。

对于那些从 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

【问题讨论】:

    标签: laravel laravel-5 laravel-5.6 intervention


    【解决方案1】:

    可能是并发上传的问题,因为您对所有文件使用相同的文件名。如果用户同时上传两张图片,其中一张损坏了,则另一张请求无法打开它。建议先剪裁再上传到Storage::disk('original_images')

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

    【讨论】:

      猜你喜欢
      • 2019-02-08
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      相关资源
      最近更新 更多