【问题标题】:GIF lose animation on upload上传时 GIF 丢失动画
【发布时间】:2020-09-24 22:50:46
【问题描述】:

我有一个使用 Laravel 7.1 和 Backpack 的项目,但我在上传时丢失了 GIF 图像的动画。

增删改查

        $this->crud->addField([
        'name' => 'photo',
        'label' => 'Imagen ES',
        'type' => 'image',
        'upload' => false,
        'prefix' => 'uploads/',
    ]);

型号

    public function setPhotoAttribute($value){
    $year = date('Y');
    $attribute_name = "photo";
    $disk = "uploads";
    $destination_path = "/noticias/$year";

    // if the image was erased
    if ($value==null) {
        // delete the image from disk
        \Storage::disk($disk)->delete($this->{$attribute_name});
        // set null in the database column
        $this->attributes[$attribute_name] = null;
    } else
    {
        $extension = '';
        if (starts_with($value, 'data:image/png')) {
            $extension = '.png';
        }else if(starts_with($value, 'data:image/gif')){
            $extension = '.gif';
        }else if(starts_with($value, 'data:image/jpeg')) {
            $extension = '.jpg';
        }                    
                // if a base64 was sent, store it in the db
                if($extension != '') {
                    // 0. Make the image
                    $image = \Image::make($value);
                    // 1. Generate a filename.
                    $filename = md5($value.time()).$extension;
                    // 2. Store the image on disk.
                    \Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
                    // 3. Save the path to the database
                    $this->attributes[$attribute_name] = $destination_path.'/'.$filename;
                } else
                {

    }

}

我尝试单独上传 gif,但我不知道在哪里可以这样做,因为返回值是 base64 而不是临时文件

    $image = Image::make($file->getRealPath());

if ($file->getClientOriginalExtension() == 'gif') {
    copy($file->getRealPath(), $destination);
}
else {
    $image->save($destination);
}

尝试上传 gif 时我可以在哪里使用它?

【问题讨论】:

    标签: image animation gif laravel-backpack


    【解决方案1】:

    如果上传了 gif,我通过解码和存储请求中的原始 base64 而不是干预生成的图像解决了这个问题。

                if($image->mime() == "image/gif") {
                    \Storage::disk($disk)->put($destination_path.'/'.$filename, base64_decode(Str::replaceFirst('data:image/gif;base64,', '', $value)));
                } else {
                    \Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
                }
    

    如果您尚未在模型中导入 Illuminate\Support\Str,请确保。

    【讨论】:

      猜你喜欢
      • 2018-07-17
      • 2012-04-06
      • 2012-02-04
      • 2013-08-26
      • 2012-09-07
      • 2012-01-01
      • 2012-11-19
      • 2015-03-19
      • 2013-01-06
      相关资源
      最近更新 更多