Skip to content
Advertisement

How to compress image before uploading in Laravel?

I’m making a images gallery website where users can upload any image and they will be displayed on frontend. I need to compress images without effecting it’s quality to reduce there size so that page load speed should not effect that much. I’m using following code to upload image:

$rules = array('file' => 'required');
$destinationPath = 'assets/images/pages'
$validator = Validator::make(array('file' => $file), $rules);
if ($validator->passes()) {
   $filename = time() . $uploadcount . '.' . $file->getClientOriginalExtension();
   $file->move($destinationPath, $filename);
   return $filename;
} else {
   return '';
}

Advertisement

Answer

You need to optimize the image for web usage as user may upload images that are way to large (Either in size or resolution). You may also want to remove the meta data from the images to decrease the size even more. Intervention Image perfect for resizing/optimizing images for web usage in Laravel. You need to optimize the image before it is saved so that the optimized version is used when loading the web page.

Intervention Image

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement