Laravel Convert jpeg Image to webp

Using Image Intervention package, we can easily do it.

Example 1

During file upload:

public function storePoster(Request $request, Tournament $tournament)
{
    if ($request->hasFile('poster')) {
        $tournament->update([
            'poster' => $request->poster->store('images', ['disk' => 'public_uploads']),
        ]);

       $classifiedImg = $request->file('poster');
       $filename = $classifiedImg->getClientOriginalExtension();
       // Intervention 
       $image = Image::make($classifiedImg)->encode('webp', 90)->resize(200, 250)->save(public_path('uploads/'  .  $filename . '.webp')
    }
}

Example 2

Keep image's ration:

$imageResize = Image::make($image)->encode('webp', 90);
if ($imageResize->width() > 380){
    $imageResize->resize(380, null, function ($constraint) {
        $constraint->aspectRatio();
    });
}
$destinationPath = public_path('/imgs/covers/');
$imageResize->save($destinationPath.$name);

Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.