【发布时间】: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