压缩图像大小(Laravel)

时间:2018-08-31 15:08:35

标签: laravel

我有这段代码可以在db和上载文件夹中插入图像。您知道什么是必要的,以便可以压缩上传的图像的大小吗?

...

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

...

1 个答案:

答案 0 :(得分:1)

我希望您知道在此过程中会损失一些图像质量?

如果可以接受,我建议您使用ImageMagickimagejpeg()

对于ImageMagick()

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

对于imagejpeg()

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