GIF lose animation on upload

感情迁移 提交于 2021-01-28 13:50:49

问题


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/',
    ]);

MODEL

    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
                {

    }

}

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);
}

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


回答1:


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());
            }

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



来源:https://stackoverflow.com/questions/62220282/gif-lose-animation-on-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!