3
votes

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

2
It depends on what the images are used for. Can the canvas size be reduced? Can you lower the quality? - Jerodev
its a raw image. i want to reduce while uploading - Rakibul Islam

2 Answers

0
votes

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)

-2
votes

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));

}