samalapsy's avatar

Upload Multiple Images to Server Using Image Intervention

public function saveImages(Request $request)
    {

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

        
        $slug = session::get('new_hostel_slug');
        $folderName = $slug;
        $listingPath = $folderName ."/";
        $path = "uploads/listings/hostels/". $folderName ."/";

        

        //File Does not Exist
        if(!File::exists($path)) {
            File::makeDirectory($path, 0775, true);
        }

        //Add path to DB
        $hostel = Hostel::whereSlug($slug)->first();
        if (!is_null($hostel->images_id)) {
            $hostel->images_id = $listingPath;  // Path to where all listing images resides
            $hostel->save();
        }


        foreach ($images as $key => $image) {
            $rules = [
                'image' => 'required|image|mimes:jpeg,bmp,png|size:2048' //2MB Max Size
            ];

            $v = Validator::make($image, $rules);

            if ($v->fails()) {
                return redirect()->back()->withInput()->withErrors($v);
            }
            
            $fileName = $image->getClientOriginalName().$image->getClientOriginalExtension();
            $destinationPath = $path;
            
            //Upload Images One After the Order into folder
            $img = Image::make($request->file('hostel_image_files'));
            $img->insert(public_path('img/logo.png'), 'bottom-right', 10, 10)->save($destinationPath.'/'.$fileName);
            $move = $image->move($destinationPath, $fileName);

            //Insert into DB and 
        }

        return redirect()->route('create_hostel_step5');
    }

STACK ERROR LOG

[2017-06-05 04:57:35] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Illuminate\Validation\Factory::make() must be of the type array, object given, called in C:\xampp\htdocs\appname\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 221 

I have the above method to upload multiple images and add watermark on it using Image Intervention. This is the error I get while trying to save the image. Please help me out. IF there's another better and more professional and standard way to do it please help me out.

Thanks in Anticipation

0 likes
3 replies
samalapsy's avatar
samalapsy
OP
Best Answer
Level 2

I've gotten the Solution Thanks Everyone

1 like
johnef_sh's avatar

@samalapsy how did you solve this problem I have done something like this but I got an issue I was able to upload and resize my images but Got duplicated images say I am uploading this three images 0.png, 1.png, 2.png

but this is how the images inserted in database

id=>1 -- 0.png
id=>2 -- 0.png
id=>3 -- 1.png

see my controller

if ( $request->hasFile( 'gImg' ) ) {
            $gImgs = $request->gImg;
            foreach ( $gImgs as $gImg ) {

                $filename = time() . '.' . $gImg->getClientOriginalExtension();
                Image::make( $gImg )->resize( 1890, 1358 )->save( 'images/decoration/' . $filename );
                Image::make( $gImg )->fit( 646, 250 )->save( 'images/decoration/thumbs-' . $filename );
                $dcoImg                = new DecorationGallery();
                $dcoImg->image         = $filename;
                $dcoImg->decoration_id = $insertedId;
                $dcoImg->save();
            }
        }

can you please help

samalapsy's avatar

@johnef_sh You have a duplicated in your controller.. See this

Image::make( $gImg )->resize( 1890, 1358 )->save( 'images/decoration/' . $filename );
                Image::make( $gImg )->fit( 646, 250 )->save( 'images/decoration/thumbs-' . $filename );

The above code would duplicated the images in different size.

I hope It is helpful

Please or to participate in this conversation.