3

Assume I have an image file whose size is 400 KB. Now I want to reduce its size to 200 KB!

is it possible using laravel intervention?

If not which method I should follow?

Intervention Image resize package

Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
Rakibul Islam
  • 679
  • 9
  • 18

2 Answers2

0

try this function after uploading image

function compress($source, $destination, $quality)
{

    $info = getimagesize($source);
    $image = '';

    if ($info['mime'] == 'image/jpeg')
        $image = imagecreatefromjpeg($source);

    elseif ($info['mime'] == 'image/gif')
        $image = imagecreatefromgif($source);

    elseif ($info['mime'] == 'image/png')
        $image = imagecreatefrompng($source);

    imagejpeg($image, $destination, $quality);

    return $destination;
}

use it like this: compress(path_to_your_image,same_path_to_reolace_it,quality:10->100)

Mohammed Aktaa
  • 1,345
  • 9
  • 14
  • So how do you know the result will be 200 KB at the end? Just by setting quality, you can't be sure about that. – kodfire Mar 02 '21 at 17:29
-2

You should try this:

if($request->hasFile('image')) {

    $image       = $request->file('image');
    $filename    = $image->getClientOriginalName();

    $image_resize = Image::make($image->getRealPath());              
    $image_resize->resize(300, 300);
    $image_resize->save(public_path('images/ServiceImages/' .$filename));

}
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • But what if the image should be bigger than 300x300 pixels? OP did not define what the images should be used for. – Jerodev Jul 11 '19 at 06:47
  • This does not maintain aspect ratio. `$file = Image::make($file)->resize(800, null, function ($constraint) {$constraint->aspectRatio();});` works for me – Rakibul Islam Jul 11 '19 at 10:59
  • 1
    So how do you know the result will be 200 KB at the end? Just by resizing, you can't be sure about that. – kodfire Mar 02 '21 at 17:30