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();
    }
}
Puck
  • 2,080
  • 4
  • 19
  • 30
Danish
  • 343
  • 1
  • 8
  • 21
  • _“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?) – CBroe Jul 08 '20 at 08:03
  • I m looking at file extension after storing. – Danish Jul 08 '20 at 08:07
  • http://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.”_ – CBroe Jul 08 '20 at 08:09
  • But OP is using method `encode` not `save` @CBroe – GetSet Jul 08 '20 at 08:10
  • Ahh I see, I got it, @CBroe Thank you – Danish Jul 08 '20 at 08:11
  • How do you "got it" @Danish, please elaborate – GetSet Jul 08 '20 at 08:12
  • You need to save the image after format, but you save it on table only – STA Jul 08 '20 at 08:12
  • Something like `$image = Image::make(public_path('uploads/' . $tournament->poster))->encode('webp', 90)->resize(200, 250)->save(public_path('/images/users/listing-images/' . 'Image'));` – STA Jul 08 '20 at 08:14
  • @GetSet 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. – CBroe Jul 08 '20 at 08:15
  • Let me write the full code – STA Jul 08 '20 at 08:15

3 Answers3

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')
    }
}
STA
  • 30,729
  • 8
  • 45
  • 59
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);
Gim
  • 79
  • 1
  • 4
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);
            
        }
M Sohaib
  • 21
  • 3