lat4732's avatar
Level 12

How to override image with Image Intervention Laravel 8

Hello everyone! So I have this form:

<form method="POST" action="{{ url('/save') }}" enctype="multipart/form-data">
	<input type="file" name="logo" />
	<input type="submit" value="Submit" />
</form>

And I need to upload and override with the same name an existing image at directory public/logos. I tried with:

	Image::make($request->logo)->resize(120, 120)->save(asset($details['logo_path']));

where $details['logo_path'] is actually extracted path from the database (logos/image.jpg).

So in short the user is uploading an image and it overrides the already existing one in the logos folder and stores the path in the database as logos/imageName.jpg. Any idea?

0 likes
16 replies
Sergiu17's avatar
dd($request->logo); // this should be an object
Image::make($request->logo->getRealPath())
    ->resize(120, 120)
    ->save(public_path($details['logo_path']));
anilkumarthakur60's avatar
 $this->validate($request, [
            'file' => 'required|image|mimes:jpg,jpeg,png,gif,svg|max:2048',
        ]);

        $image = $request->file('file');
        $input['file'] = time().'.'.$image->getClientOriginalExtension();
        
        $destinationPath = public_path('/thumbnail');

        $imgFile = Image::make($image->getRealPath());

        $imgFile->resize(150, 150, function ($constraint) {
		    $constraint->aspectRatio();
		})->save($destinationPath.'/'.$input['file']);

        $destinationPath = public_path('/uploads');
        $image->move($destinationPath, $input['file']);

        return back()
        	->with('success','Image has successfully uploaded.')
        	->with('fileName',$input['file']);
lat4732's avatar
Level 12

Okay, nothing worked. So I got to the conclusion that the form is not getting properly the uploaded file. I think it's not even working right. I have enctype="multipart/form-data" in my form tag and I'm pretty sure I'm doing everything right. I updated the code like that:

$image = $request->file('logo');
$name_gen = $details['logo_path'].'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(120,120)->save(public_path('/logos/' . $name_gen));

and I'm getting error Call to a member function getClientOriginalExtension() on null.

Any ideas?

Snapey's avatar

@LarAlex check the request. Does it contain uploaded file object, if not then the rest of the code is irrelevant

You have not made the mistake of putting the logo form inside another form?

lat4732's avatar
Level 12

@Snapey So what can I do? Why it's not processing through the form?

Snapey's avatar

You have not made the mistake of putting the logo form inside another form?

lat4732's avatar
Level 12

I really need a solution, any ideas?

Please or to participate in this conversation.