【问题标题】:How to display storage image in laravel blade?如何在 laravel 刀片中显示存储图像?
【发布时间】:2019-06-12 12:45:49
【问题描述】:

我已经使用以下代码存储了我的图像

if (isset($image)) {
    // make unique name for image
    $currentDate = Carbon::now()->toDateString();
    $imagename = $slug . '-' . $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();

    // check category dir is exists
    if (!Storage::disk('public')->exists('category')) {
        Storage::disk('public')->makeDirectory('category');
    }
    // resize image for category and upload
    $category = Image::make($image)->resize(1600,479)->stream();
    Storage::disk('public')->put('category/'.$imagename,$category);
    // check category slider dir is exists
    if (!Storage::disk('public')->exists('category/slider')) {
        Storage::disk('public')->makeDirectory('category/slider');
    }
    // resize image for category slider and upload
    $slider = Image::make($image)->resize(500,333)->stream();
    Storage::disk('public')->put('category/slider/'.$imagename,$slider);
} else {
    $imagename = "default.png";
}

试试这段代码

<img src="images/icons8-team-355979.jpg" alt="Profile Image">

加载资源失败:服务器响应状态为 404(未找到) 这是错误

【问题讨论】:

标签: php mysql laravel


【解决方案1】:

试试这个

<img src="{{url('category/slider')}}/{{ $category->image }}" alt="{{$category->name}}">

url 函数将转到“/public”文件夹。因此,您可能需要编辑“/config/filesystem.php”,以便将上传的照片保存在“/public”目录中。

有关这方面的更多信息,请查看document

【讨论】:

  • 对不起先生 :( 我已经尝试使用此代码 url('category/slider/'.$category->image ) }}" alt="{{ $category->name }}">
  • 您不需要在刀片视图中使用 'Storage::disk('public')'。检查我更新的答案。
  • 检查你的图片是否保存在'/public/category/slider'目录下。你能分享你的'config/filesystem.php'
  • env('FILESYSTEM_DRIVER', 'local'), 'disks' => [ 'local' => ['driver' => 'local', 'root' => storage_path('app'),], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'),url' =>env(' APP_URL').'/storage','visibility' => 'public', ],'s3' => ['driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), ], ], ] ;
  • 将 'disks->public->root' 从 'storage_path('app/public')' 更改为 'public_path()'
【解决方案2】:

当您想直接从公共磁盘提供文件时,您需要为这些文件创建符号链接。这在此处的文档中有所描述:https://laravel.com/docs/5.7/filesystem#the-public-disk

完成此操作后,您可以使用asset() 函数链接到资产。

echo asset('category/slider/icons8-team-355979.jpg');

【讨论】:

  • 图片在 storage->app->public->category->slider 文件夹
  • 应该可以正常工作,但您必须为资产函数使用正确的相对路径。
猜你喜欢
  • 2022-07-08
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2018-09-05
  • 2021-09-21
  • 1970-01-01
  • 2019-08-07
相关资源
最近更新 更多