如何在laravel blade中显示存储图像?

12 浏览
0 Comments

如何在laravel blade中显示存储图像?

我用以下代码存储了我的图像:

if (isset($image)) {
    // 为图像创建唯一的名称
    $currentDate = Carbon::now()->toDateString();
    $imagename = $slug . '-' . $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
    // 检查类别目录是否存在
    if (!Storage::disk('public')->exists('category')) {
        Storage::disk('public')->makeDirectory('category');
    }
    // 调整类别图像大小并上传
    $category = Image::make($image)->resize(1600,479)->stream();
    Storage::disk('public')->put('category/'.$imagename,$category);
    // 检查类别滑块目录是否存在
    if (!Storage::disk('public')->exists('category/slider')) {
        Storage::disk('public')->makeDirectory('category/slider');
    }
    // 调整类别滑块图像大小并上传
    $slider = Image::make($image)->resize(500,333)->stream();
    Storage::disk('public')->put('category/slider/'.$imagename,$slider);
} else {
    $imagename = "default.png";
}

请尝试使用以下代码:

Profile Image

无法加载资源:服务器响应状态为404(未找到)

这是错误信息。

0
0 Comments

如何在Laravel Blade中显示存储的图像?

在Laravel Blade中显示存储的图像可能会遇到一些问题。下面将讨论出现这些问题的原因以及解决方法。

问题的原因是在Blade视图中使用了错误的代码。正确的代码应该是:

{{$category->name}}

这里的`url`函数将会访问到`/public`文件夹。因此,你可能需要编辑`/config/filesystem.php`文件,以便将上传的图片保存在`/public`目录下。具体的操作请参考[Laravel文档](https://laravel.com/docs/5.7/filesystem)。

如果你尝试了上述代码仍然无法显示图片,可能是因为你在Blade视图中不需要使用`Storage::disk('public')`。请检查以下代码:

{{ $category->name }}

另外,请确保你的图片被保存在`/public/category/slider`目录下。你可以分享一下你的`config/filesystem.php`文件的内容,以便我们能够帮助你。

 env('FILESYSTEM_DRIVER', 'local'),
    'disks' => [
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],
        'public' => [
            'driver' => 'local',
            'root' => public_path('storage'),
            '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()`。这样做可以确保图片保存在`/public`目录下。

希望上述解决方法能够帮助你在Laravel Blade中成功显示存储的图像。

0