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();
    }
}
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?)
    – C3roe
    Commented Jul 8, 2020 at 8:03
  • I m looking at file extension after storing.
    – Danish
    Commented Jul 8, 2020 at 8:07
  • 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.”
    – C3roe
    Commented Jul 8, 2020 at 8:09
  • But OP is using method encode not save @CBroe
    – GetSet
    Commented Jul 8, 2020 at 8:10
  • Ahh I see, I got it, @CBroe Thank you
    – Danish
    Commented Jul 8, 2020 at 8:11

3 Answers 3

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')
    }
}
2
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);
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);
            
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.