我正在尝试将动画gif作为用户头像上传。这些图像通过javascript转换为base64,然后通过图像干预上传。
$img = Image::make(file_get_contents($file));
$mime = $img->mime();
if ($mime == 'image/jpeg')
$ext= '.jpg';
elseif ($mime == 'image/png')
$ext= '.png';
elseif ($mime == 'image/gif')
$ext= '.gif';
elseif($mime == 'image/x-icon')
$ext = '.ico';
elseif($mime == 'image/bmp')
$ext = '.bmp';
else {
$ext = '.jpg';
}
$name = time().$ext;
$path = public_path("images/$controller");
File::isDirectory($path) or File::makeDirectory($path);
if(property_exists($field, 'size') && !$ext=='.gif') {
$field->size = explode('*',$field->size);
if($img->resize($field->size[0], $field->size[1])->save("$path/$name")) {
$data[$key] = $name;
} else {
$data[$key] = null;
}
} else {
if($img->save("$path/$name")) {
$data[$key] = $name;
} else {
$data[$key] = null;
}
}
但是,这些图像在上传后会丢失动画。我不知道该怎么办。如何不丢失动画?
JavaScript代码:
reader.onload = function(e) {
var src = e.target.result;
var name = file.name;
var type = file.type;
var img = "<img src='"+src+"' class='img-fluid mx-auto' alt='image'>";
$('.card_group_'+field+' .previewContainer_'+count+' .file-loading').remove();
$('.card_group_'+field+' .previewContainer_'+count+' .imagePreview').append(img);
$('.card_group_'+field+' .previewContainer_'+count+' .card-header').append(name);
var input = "input[name='"+field+"']";
var added = $(input).val();
if(added == '') {
$(input).val(src);
} else {
$(input).val(added+';'+src);
}
}
reader.readAsDataURL(file);
答案 0 :(得分:3)
现在,您可以使用另一个函数将其保存为gif格式,并继续使用库中的其他功能。
if ($file->getClientOriginalExtension() == 'gif') {
copy($file->getRealPath(), $destination);
}
else {
$image->save($destination);
}
$width = $image->width();
$height = $image->height();
$image->destroy();