How to encode jpeg/jpg to webp in laravel
Asked Answered
B

3

6

I'm working with laravel 7 and using intervention/image to store images. However, I want to encode and store images as webp, I'm using the following code but it is not encoding the image in webp rather it is storing in the original format. Can you please tell me what I'm doing wrong?

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();
    }
}
Bar answered 8/7, 2020 at 7:57 Comment(10)
“but it is not encoding the image in webp rather it is storing in the original format” - how exactly did you determine that? Did you check the actual image file content, or are you just going by the file extension it gets stored under? (I don’t see you explicitly setting or changing the latter, is there supposed to be some automatism that sets this based on the encode type?)Ing
I m looking at file extension after storing.Bar
image.intervention.io/api/save: “By default the format of the saved image is defined by the file extension of the given path. Alternatively it is possible to define the image format by passing one of the image format extension as a third parameter.”Ing
But OP is using method encode not save @CBroeSlug
Ahh I see, I got it, @CBroe Thank youBar
How do you "got it" @Danish, please elaborateSlug
You need to save the image after format, but you save it on table onlyVocalic
Something like $image = Image::make(public_path('uploads/' . $tournament->poster))->encode('webp', 90)->resize(200, 250)->save(public_path('/images/users/listing-images/' . 'Image'));Vocalic
@Slug but that is not the part that actually stores the image to the file system, $image->save(); does that. The abstract “Image” object in memory might have the webp encoded version added to it (encode method description from docs, “Return value: Instance of Intervention\Image\Image with attached encoded image data.“), but if the file name sored internally ends with .jpg, then save without additional parameters will still store it in that encoding.Ing
Let me write the full codeVocalic
V
14

Try this :

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')
    }
}
Vocalic answered 8/7, 2020 at 8:21 Comment(2)
is image a seperate class or does it come with laravel?Tuneless
@natghi Image::make() is from image intervention packageTranslate
R
0

This is my code to convert to .webp and resize (keep image's ratio)

$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);
Ralph answered 31/7, 2021 at 7:12 Comment(0)
I
0

if you want to convert image in to WEBP without any service or package, try this method. work for me. have any question can ask. Thankyou

        $post = $request->all();
        $file = @$post['file'];
        $code = 200;
        $extension = $file->getClientOriginalExtension();
        $imageName = $file->getClientOriginalName();
        $path = 'your_path';

        if(in_array($extension,["jpeg","jpg","png"])){
    //old image
            $webp = public_path().'/'.$path.'/'.$imageName;
            $im = imagecreatefromstring(file_get_contents($webp));
            imagepalettetotruecolor($im);
    // have exact value with WEBP extension
            $new_webp = preg_replace('"\.(jpg|jpeg|png|webp)$"', '.webp', $webp);
    //del old image
            unlink($webp);
    // set qualityy according to requirement
            return imagewebp($im, $new_webp, 50);
            
        }
Inkberry answered 13/9, 2022 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.