Home > database >  How to reduce the image size by reducing the quality when uploading images in Laravel?
How to reduce the image size by reducing the quality when uploading images in Laravel?

Time:03-06

I'm developing a Larvel website for ads, now users can upload very large images, I would like to resize, compress, reduce the quality of these images before storing them in the DB.

I have

$img = request->file("images"); 

Where do I go from here?

It seems strange to me how Php actually works. I was expecting some kind of byte[] array-like in Java that represents the image then do some calculation over it and we are done.

I checked a couple of posts like this and this one here

However, all code snippets deal with local disk images is there a way I can use:

$img = request->file("images");

Or I don't understand how files work in PHP?

CodePudding user response:

You can use spatie package or any other package for image manipulation:

spatie image with this package you can simply make:

Image::load($request->file("images")) // or its path
   ->width(100)
   ->height(100)
   ->save($pathToNewImage);

It also support for reducing image sizes by decreasing quality.

CodePudding user response:

Try http://image.intervention.io/ to compress the file. It is simple and efficient.

Compression details: http://image.intervention.io/api/encode

reference How to compress image file size laravel

  • Related