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

Laravel 9中使用intervention/image遇问题:图片可上传但无法缩放

图片缩放功能失效问题解决

你的代码问题在于只执行了缩放逻辑,但没有将缩放后的图片保存到服务器,原图片通过store()方法正常上传,但缩放后的图片仅停留在内存中,没有持久化到磁盘。

修改后的代码

public function store(Request $request)
{
    $validatedData = $request->validate([
        'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:20480',
    ]);

    $imageFile = $request->file('image');
    $name = $imageFile->getClientOriginalName();    
    // 定义缩放后图片的存储路径
    $path = 'images/' . $name;

    // 缩放图片并保存到public磁盘
    Image::make($imageFile)->resize(50, 50)->save(storage_path('app/public/' . $path));

    $save = new Gallery;
    $save->image = $path;
    $save->status = 1;
    $save->user_id = Auth::id();
    $save->save();

    return redirect('gallery')->with('success', 'Image has been uploaded')->with('image',$name);
}

关键修改点

  • 移除了原有的store()方法调用,改为先处理图片缩放再保存
  • 使用save()方法将缩放后的图片写入到Laravel public磁盘的实际存储路径(storage/app/public/)
  • 数据库中存储的路径对应缩放后的图片,确保后续访问的是处理后的图片

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 05:35:20