php - PHP Laravel 6 - 配置文件图像不显示
问题描述
我目前正在设计一个使用 PHP 的 laravel 框架的网站。我目前正在处理个人资料页面,所有字段都连接到数据库中他们尊重的列。他们正在更新,但是每当用户将他们的图像从默认更改为选定的图像时,它只会显示一个空框。这是我的代码供审查,非常感谢一些外部输入。PS:我确实正确添加了干预图像扩展
Profile.blade.php:
<div class="card-header">
<img src="/uploads/avatars/{{$user->avatar}}" style="width:100px; height:100px; float:left;
margin-right:25px">
<strong>{{$user->name}}'s Profile</strong></div>
配置文件控制器.php:
public function update(Request $request)
{
$this->validate($request, ['name' => 'required','email' => 'required|email']);
$user = Auth::user();
$user->name = $request->name;
$user->email = $request->email;
$user->about = $request->about;
$user->save();
if($request->hasFile('avatar')){
$avatar = $request->avatar;
$avatar_new = time() . '.' . $avatar->getClientOriginalExtension();//Creates unique name for file
Image::make($avatar)->resize(300,300)->save(public_path('/uploads/avatars/' . $avatar_new));
$user->avatar = $avatar_new;
$user->save();
return redirect()->back();
}
扩展: 现在由于某种原因,当我尝试更新我的用户图像时,我遇到了这个错误
Intervention\Image\Exception\NotSupportedException
GD Library extension not available with this PHP installation.```
Even though it was at least updating only 15 minutes ago.
解决方案
你好,在你的控制器中保存你的图像,你需要在 storage 文件夹中使用 store 图像来实现图像,这可以帮助你上传图像,laravel 会将它保存在存储图像中,并且可以在公共存储中访问。
那么你需要什么?
转到这里是您需要将图像存储在文件夹存储中以及数据库上的 url 链接
您还需要像这样将图像存储在控制器中:
public fucntion create(Request $request){
$inputs = $request->all(); // that will be take all inputs
$your_model = Model::class // take your model to save data
$your_model = store($inputs['image'],'name_of_your_link_folder');
$your_model->create();
}
对于更新图像:
public fucntion create(Request $request){
$inputs = $request->all(); // that will be take all inputs
$your_model = Model::class // take your model to save data
$your_model = store($inputs['image'],'name_of_your_link_folder');
$your_model->save();
}
在这之后你需要在你的刀片中使用(你想要显示图像的人)你只需使用这个{{ asset('storage'.$link_of_the_image }}
推荐阅读
- testing - 在 Cypress 中运行测试的所有断言
- magnolia - 渲染不属于 JCR 的节点
- ravendb - 在 Ravendb 中创建使对象数组可搜索的索引
- azure - 如何使用 Azure pod 身份与 Keda 合作
- python - pygame/sprites:暴民碰撞
- c# - 复选框中的 UWP 10 XAML 删除线内容
- angular - 当有效负载数据与当前状态值相同时,如何在 ngxs 状态选择上触发订阅
- javascript - 无法使用 dropzone.js 调整图像大小
- javascript - 如何在 React 中调用类组件?
- android - 无法使用来自 sql Flutter 的数据更新仪表板