Laravel中使用ImageIntervention将图片保存至storage/app/public/uploads
问题解决:将ImageIntervention处理后的图片保存到storage/app/public/uploads
你的问题出在路径定位错误:storage_path('uploads/')指向的是项目根目录下的storage/uploads,而你目标的storage/app/public/uploads需要调整路径写法,同时可以优化目录创建和权限设置,下面给两种可行方案:
方案一:直接修正路径写法
修改目录判断和保存路径,明确指向storage/app/public/uploads:
$file = $request->file('image-profile'); $filename = \Carbon\Carbon::now()->format('U') .'-'. Str::uuid() . '.' . $file->getClientOriginalExtension(); // 正确指向storage/app/public/uploads $uploadDir = storage_path('app/public/uploads/'); if (!file_exists($uploadDir)) { mkdir($uploadDir, 0755, true); // 用0755权限更符合Web服务器访问要求 } $path = $uploadDir . $filename; $img = Image::make($file->getRealPath()); $img->fit(200, 200); $img->circle(100, 50, 50, function ($draw) { }); $img->save($path);
方案二:用Laravel Storage门面(更符合框架规范)
借助Laravel的Storage门面直接操作public磁盘,不用手动处理目录创建:
$file = $request->file('image-profile'); $filename = \Carbon\Carbon::now()->format('U') .'-'. Str::uuid() . '.' . $file->getClientOriginalExtension(); $img = Image::make($file->getRealPath()); $img->fit(200, 200); $img->circle(100, 50, 50, function ($draw) { }); // 直接保存到public磁盘的uploads目录下 Storage::disk('public')->put('uploads/' . $filename, (string) $img->encode());
说明:Laravel的public磁盘默认对应storage/app/public目录,如果你之前执行过php artisan storage:link,前端可以通过storage/uploads/xxx访问到这些文件。
内容的提问来源于stack exchange,提问作者ramedju
相关产品推荐
相关产品推荐

