You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中Glide无法找到图片的问题求助

Laravel Glide 图片找不到问题排查与解决

问题场景

本地全新安装Laravel(PHP 8.1.13 / Laravel 9.44.0),配置Glide路由和控制器后,访问http://localhost:8000/img/storage/images/portrait-rose.jpg时提示“Could not find the image storage/images/portrait-rose.jpg”,但直接访问http://localhost:8000/storage/images/portrait-rose.jpg可以正常显示。

配置的路由:

Route::get('/img/{path}', [ImageController::class, 'show'])->where('path', '.*');

对应的ImageController代码:

namespace App\Http\Controllers;

use Illuminate\Contracts\Filesystem\Filesystem;
use League\Glide\Responses\LaravelResponseFactory;
use League\Glide\ServerFactory;

class ImageController extends Controller
{
    public function show(Filesystem $filesystem, $path)
    {
        $server = ServerFactory::create([
            'response' => new LaravelResponseFactory(app('request')),
            'source' => $filesystem->getDriver(),
            'cache' => $filesystem->getDriver(),
            'cache_path_prefix' => '.cache',
        ]);

        return $server->getImageResponse($path, request()->all());
    }
}

核心错误日志:

[2022-12-24 09:07:18] local.ERROR: Could not find the image `storage/images/gaia.jpg`. {"userId":1,"exception":"[object] (League\\Glide\\Filesystem\\FileNotFoundException(code: 0): Could not find the image `storage/images/gaia.jpg`. at C:\\wamp\\www\\jolyne\\vendor\\league\\glide\\src\\Server.php:629)
#0 C:\\wamp\\www\\jolyne\\vendor\\league\\glide\\src\\Server.php(548): League\\Glide\\Server->makeImage('storage/images/...', Array)
#1 C:\\wamp\\www\\jolyne\\app\\Http\\Controllers\\ImageController.php(20): League\\Glide\\Server->getImageResponse('storage/images/...', Array)
#2 C:\\wamp\\www\\jolyne\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php(54): App\\Http\\Controllers\\ImageController->show(Object(Illuminate\\Filesystem\\FilesystemAdapter), 'storage/images/...')

问题原因

Laravel默认的Filesystem实例指向storage/app目录,你传入的$path是storage/images/portrait-rose.jpg,导致Glide实际查找路径为storage/app/storage/images/portrait-rose.jpg,该路径不存在。

而直接访问http://localhost:8000/storage/images/portrait-rose.jpg能显示,是因为public/storage软链接指向storage/app/public,实际文件路径是storage/app/public/images/portrait-rose.jpg。

解决方案

方式一:调整访问URL

将访问URL改为:http://localhost:8000/img/public/images/portrait-rose.jpg,Glide会直接定位到storage/app/public/images/portrait-rose.jpg,匹配实际文件位置。

方式二:修改控制器适配原有URL

在Glide配置中指定源目录前缀,并处理传入的路径:

public function show(Filesystem $filesystem, $path)
{
    $server = ServerFactory::create([
        'response' => new LaravelResponseFactory(app('request')),
        'source' => $filesystem->getDriver(),
        'source_path_prefix' => 'public', // 指定源目录为storage/app/public
        'cache' => $filesystem->getDriver(),
        'cache_path_prefix' => '.cache',
    ]);

    // 移除路径中的storage/前缀
    $path = str_replace('storage/', '', $path);

    return $server->getImageResponse($path, request()->all());
}

修改后无需调整原有URL,控制器会自动处理路径,让Glide正确找到文件。

前置检查

确保已执行php artisan storage:link命令,创建public/storage到storage/app/public的软链接,这是Laravel访问存储文件的基础配置。

内容的提问来源于stack exchange,提问作者Exoseed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 06:05:15