Search code examples
phplaravelimageimage-upload

Intervention image multiple image upload in laravel


in blade

<input type="file" name="image[]" id="" required class="form-control" multiple accept="image/*">

in controller

public function addReviewPost(Request $request)
{
    $image = $request->file('image');

    $this->validate($request, [
        'image' => 'required',
        'image.*' => ' max:2048 | dimensions:max_width=2200',
    ]);

    if (request()->hasFile('image')) {
        $counter = count($image);

        for ($i = 0; $i < $counter; $i++) {

            $image = Image::make($image[$i]);
            $image->resize(null, 627, function ($constraint) {
                $constraint->aspectRatio();
            });
            $image->save(public_path('../../img/testimonial/' . time() . '.png'));
        }
    }

}

it shows error

Symfony\Component\Debug\Exception\FatalThrowableError
Cannot use object of type Intervention\Image\Image as array

can anyone please help me how can I upload multi file using intervention image package?


Solution

  • Please try the following:

       public function addReviewPost(Request $request)
        {
         if (request()->hasFile('image')) {
            $images = $request->file('image');
            foreach ($images as $key => $file) { 
                $image = Image::make($request->file($file));
                $image->resize(null, 627, function ($constraint) {
                    $constraint->aspectRatio();
                });
                $image->save(public_path('../../img/testimonial/' . time() . '.png'));
            }
        }
        }
    

    Let me know If you get any errors.

    Don't forget to mark it answer if works

    Hope it helps you

    Thank you