GIF在上传时丢失动画 [英] GIF lose animation on upload

查看:73
本文介绍了GIF在上传时丢失动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a project with Laravel 7.1 and Backpack and I lose the animation of the GIF images on upload.

CRUD

        $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而不是临时文件

I have tried to upload the gifs separately, but I don't know where I could do it, since the return value is a base64 and not a temporary file

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

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

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

Where could I use it when trying to upload a gif?

推荐答案

我通过解码并存储请求中的原始base64(而不是如果上传了gif的干预生成的图像)​​来解决了这个问题.

I solved this by decoding and storing the original base64 from the request instead of the Intervention generated image if a gif was uploaded.

            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.

Make sure to import Illuminate\Support\Str in your model if you haven't already.

这篇关于GIF在上传时丢失动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆