如何解决Laravel Backpack 使用原始名称保存图像
我在后台使用 laravel 背包,我需要上传一些图片,我从他们的网站复制了代码并且工作正常,但是我需要图片上的原始名称,我尝试了一些东西,但不起作用。
我需要在这里更改什么?
public function setImageAttribute($value)
{
$attribute_name = "image1";
$disk = "uploads"; // or use your own disk,defined in config/filesystems.php
$destination_path = "productimg"; // path relative to the disk above
// 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;
}
// if a base64 was sent,store it in the db
if (starts_with($value,'data:image'))
{
// 0. Make the image
$image1 = \Image::make($value)->encode('jpg',90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename,$image1->stream());
// 3. Delete the previous image,if there was one.
\Storage::disk($disk)->delete($this->{$attribute_name});
// 4. Save the public path to the database
// but first,remove "public/" from the path,since we're pointing to it from the root folder
// that way,what gets saved in the database is the user-accesible URL
$public_destination_path = Str::replaceFirst('uploads/','',$destination_path);
$this->attributes[$attribute_name] = $public_destination_path.'/'.$filename;
}
}
解决方法
你需要更换这个
$filename = md5($value.time()).'.jpg';
有了这个
$filename = $value->getClientOriginalName();
但你必须确保你的所有图像都有一个唯一的名称,或者你必须在文件名中添加一个随机前缀以防止名称重复
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。