使用 Laravel 上传多个调整大小的图片

Multiple resize image upload with Laravel

我正在 Laravel 中使用干预调整器将多张图片上传到数据库。

这就是我现在在我的控制器中编写的代码 imgProdukProc 控制器:

use Illuminate\Http\Request;
use File;
use Image;
use App\imgProd;

.....

public function store(Request $request)
    {
        if($request->hasFile('img')){
            foreach ($request->file('img') as $image) {
            if ($image->isValid()) {

                $img = new imgProd();
                $image_name = uniqid() .'.'. $image->getClientOriginalExtension();
                $path = public_path('/img/prod');
                $imgx = Image::make($image->getRealPath());
                $imgx->resize(360, 360, function ($constraint) {
                $constraint->aspectRatio();
                })->save($path.'/'.$image_name);

                $img->id_prod = $request->get('id_prod');
                $img->pics = 'img/prod/'.$image_name;

                $date=date_create('now');
                $format = date_format($date,"Y-m-d");
                $img->date = $format;
                $img->save();
                return redirect('adminImgProd')->with('success', 'Picture successfully added ');
            }
        }          
    }
}

这是我的看法

adminImgProd 浏览量

<form enctype="multipart/form-data" action="{{url('adminImgProd')}}" method='post'>
@csrf

<div class="form-group">
<label>CODE PRODUCT</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-pencil"></span></span>
<input type="text" autocomplete="off" class="form-control" id='id_prod' name='id_prod' required="required" />
<span class="input-group-addon"><button type="button" onClick="javascript:openWindow2();">Select</button></span>
</div>
</div>

<div class="form-group">
<label>IMAGE PRODUCT</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-pencil"></span></span>
<input multiple type="file" class="form-control" name='img[]' id='file-input' required="required" /></div>
</div>

上面的代码有效,但是不知何故,当我尝试上传 2 张或 3 张图像时,同时保存在目标文件夹和数据库中的唯一图像只有一张,而且是最后一张

我的代码哪里出错了,或者只是我的代码从一开始就错了?

提前致谢

你把你的 return 放在你的 foreach 中,所以在 1 个循环之后,它会 return 并退出你的函数 :

public function store(Request $request)
{
    if ($request->hasFile('img')){
        foreach ($request->file('img') as $image) {
            if ($image->isValid()) {

                $img = new imgProd();
                $image_name = uniqid() .'.'. $image->getClientOriginalExtension();
                $path = public_path('/img/prod');
                $imgx = Image::make($image->getRealPath());
                $imgx->resize(360, 360, function ($constraint) {
                    $constraint->aspectRatio();
                })->save($path.'/'.$image_name);

                $img->id_prod = $request->get('id_prod');
                $img->pics = 'img/prod/'.$image_name;

                $date=date_create('now');
                $format = date_format($date,"Y-m-d");
                $img->date = $format;
                $img->save();
            }
        }

        return redirect('adminImgProd')->with('success', 'Picture successfully added ');
    }
}

您可以对多张图片执行以下操作:

   $images = $request->file('images');

   foreach ($images as $key => $image) {
     if ($request->hasFile('images') && $request->file('images')[$key]->isValid()) {
        $path = $request->images[$key]->store('public/images');
        $path = basename($path);

        $image = new ProductImages();
        $image->product_id = $request->get('product_id');
        $image->photo = $path;
        $image->save();
      }
   }

希望对您有所帮助。