Carrick
Carrick

Reputation: 65

compress size of image (Laravel)

I have this code to insert a image in db and in the uploads folder. Do you know what is necessary so is possible to compress the size of the uploaded images?

...

if ($request->image) {
    $featured = $request->image;
    $featured_new_name = time() . $featured->getClientOriginalName();
    $featured->move('uploads/posts', $featured_new_name);
} else {
    $featured_new_name = null;
}

 $post = Post::create([
          ...
            'image' => $featured_new_name ? "uploads/posts/{$featured_new_name}" : null,
...
$conf->save();

...

Upvotes: 0

Views: 2852

Answers (1)

Chukwuemeka Inya
Chukwuemeka Inya

Reputation: 2671

I hope you know you would lose some image quality in the process?

If you can live with that, I would recommend you use ImageMagick or imagejpeg().

For ImageMagick():

$image = new Imagick('image.jpg');
$image->setImageCompression(Imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(80);

For imagejpeg():

imagejpeg($image, $savePath, $quality);    //Quality has a default of 75.

Upvotes: 1

Related Questions