D
D
DocTypeMaster2021-04-19 11:15:04
Laravel
DocTypeMaster, 2021-04-19 11:15:04

How to convert images to webp on voyager?

I'm making a site on laravel voyager and I desperately need to convert all images uploaded to webp format. I found which controller is responsible for loading images, but I can't figure out where to insert the following conversion code imagewebp($im, 'path'); (or does anyone know how to do it with another function)

Here is the code of the class that loads the images:

public function upload(Request $request)
    {
        $fullFilename = null;
        $resizeWidth = 1800;
        $resizeHeight = null;
        $slug = $request->input('type_slug');
        $file = $request->file('image');
        $path = $slug.'/'.date('F').date('Y').'/';
        $filename = basename($file->getClientOriginalName(), '.'.$file->getClientOriginalExtension());
        $filename_counter = 1;

        while (Storage::disk(config('voyager.storage.disk'))->exists($path.$filename.'.'.$file->getClientOriginalExtension())) {
            $filename = basename($file->getClientOriginalName(), '.'.$file->getClientOriginalExtension()).(string) ($filename_counter++);
        }

        $fullPath = $path.$filename.'.'.$file->getClientOriginalExtension();

        $ext = $file->guessClientExtension();

        if (in_array($ext, ['jpeg', 'jpg', 'png', 'gif'])) {
            $image = Image::make($file)
                ->resize($resizeWidth, $resizeHeight, function (Constraint $constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
            if ($ext !== 'gif') {
                $image->orientate();
            }
            $image->encode($file->getClientOriginalExtension(), 75);
    
            if (Storage::disk(config('voyager.storage.disk'))->put($fullPath, (string) $image, 'public')) {
                $status = __('voyager::media.success_uploading');
                $fullFilename = $fullPath;
            } else {
                $status = __('voyager::media.error_uploading');
            }
        } else {
            $status = __('voyager::media.uploading_wrong_type');
        }

        return "<script> parent.helpers.setImageValue('".Voyager::image($fullFilename)."'); </script>";
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2021-09-15
@MikeDeveloper

I am using this solution. Works perfect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question