如何渲染 blade 存储在 Laravel 7.14 的 storage/app/public 目录中的图像?

how to render to blade the image stored in storage/app/public directory of Laravel 7.14?

我卡住了。如何显示laravel 7.14的/storage/app/public目录下的图片? 我已经尝试过我在堆栈溢出中找到的那些,甚至是我通过谷歌搜索找到的那些。

我试过这个解决方案

//Controller function
 public function renderPic($filename) {
      
      $path = '/storage/app/public/uploads/'.$filename;

      if (!File::exists($path)) {
           abort(404);
      }

      $file = File::get($path);
      $type = File::mimeType($path);

      $response = Response::make($file,200);
      $response->header("Content-Type",$type);
      return $response;
 }

网络路由

Route::get('user/renderPic/{filename}', 'User@renderPic')->name('user.renderPic');

blade 查看文件

<img id="displayPic" src="{{ route('user.renderPic',$user->filename) }}">

它没有出现

您不需要定义路由和控制器方法来渲染图像。

您只需在您的 laravel 应用程序和 运行

的根目录中打开一个终端
php artisan storage:link

这将为 storage/app/public 目录创建一个到 public/storage

的符号链接

在您的 blade 文件中,您只需像这样渲染图像:

<img id="displayPic" src="{{ asset('storage/your-image-name.ext') }}">