簡體   English   中英

使用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次循環后,它將返回並退出您的函數:

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();
      }
   }

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM