How to encode jpeg/jpg to webp in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Encode JPEG/JPG to WebP in Laravel: A Comprehensive Guide Introduction As a Laravel developer working with Intervention/Image, you might be seeking to convert your image files into the modern format - WebP. But in case you are experiencing issues while encoding images from JPEG/JPG to WebP, this article aims to provide a thorough solution. We will cover relevant code examples and best practices to ensure smooth conversion of images without any hiccups. Understanding Intervention/Image and Laravel 7 Intervention/Image is an excellent PHP library for manipulating images on your Laravel apps, from various operations like resizing, encoding, and converting image formats. In Laravel 7, you can easily perform these tasks using various methods in the Image facade class. However, as we move towards modern image formats like WebP, it is essential to make use of this library for seamless conversion. Proper Encoding Process with Intervention/Image To enable your application to encode images from JPEG/JPG into WebP, follow the provided steps: 1. Include Intervention/Image in your Laravel application using the command 'composer require intervention/image'. 2. Modify your code as shown below, and ensure you use the correct path to your image file (either public or locally):
public function storePoster(Request $request, Tournament $tournament)
{
    if ($request->hasFile('poster')) {
        $tournament->update([
            'poster' => $request->poster->store('images', ['disk' => 'public_uploads']),
        ]);
        $image = Image::make(public_path('uploads/' . $tournament->poster))->encode('webp', 90)->resize(200, 250);
        $image->save();
    }
}
3. Make sure that the `Image::make()` method is called with the correct image format and quality settings. In this case, we are using WebP format and setting a compression level of 90. 4. To avoid losing quality during conversion, consider resizing the image as well. The given code snippet achieves both tasks by making use of the `resize()` method. 5. Finally, save the newly encoded image to your desired storage location using the `save()` method. Best Practices and Troubleshooting Tips While encoding images in Laravel with Intervention/Image can be straightforward, here are some best practices and troubleshooting tips: 1. Test and optimize your compression levels for different image types to achieve the desired balance between image quality and file size. 2. Ensure that you are using the correct path to your image files to avoid encoding errors. 3. Pay attention to any error messages that may occur during the conversion process, as they can provide valuable information on potential issues with file paths or image manipulation settings. 4. Update Intervention/Image and Laravel to their latest versions for any bug fixes or improvements. 5. Explore other encoding methods like GD or Imagick, which might be better suited for specific use cases in your application. Conclusion With this comprehensive guide on how to encode JPEG/JPG images into WebP using Laravel 7 and Intervention/Image, you can now ensure the best quality and file size results for your users. By following these steps and incorporating helpful tips, you'll be able to implement advanced image manipulation features in no time. Remember always to test your code and optimize encoding settings as per your requirements to deliver top-notch performance.