首页 > 解决方案 > 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.

标签: phplaravelweblaravel-6

解决方案


你好,在你的控制器中保存你的图像,你需要在 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 }}


推荐阅读