Laravel图片操作压缩

10

我有一个使用Intervention保存和缓存图像的脚本,它能够100%工作。

但是,我想知道如何在jpg和png文件中添加75%的压缩,但我不知道在这种情况下如何应用它。

我认为除了进行软件压缩之外,PNG文件无法被压缩,所以我不确定是否相同?

这里有一个压缩的示例: http://image.intervention.io/api/save

/* ////////////////////// IMAGES //////////////////////// */
Route::get( '/media/{size}/{crop}/{name}', function ( $size = null, $crop = null, $name = null ) {
    if ( ! is_null( $size ) and ! is_null( $name ) and ! is_null( $crop ) ) {
        $size = explode( 'x', $size );

        $hours = 48;
        $cache_length = $hours * 60;

        switch ( $crop ) {

            /*///////////////////////// no crop and change ratio */
            case "0":
                $cache_image = Image::cache( function ( $image ) use ( $size, $name ) {
                    return $image->make( url( '/uploads/' . $name ) )->resize( $size[0], $size[1] )->sharpen(5);
                }, $cache_length);
                break;

            /*///////////////////////// crop - NO upsize */
            default:
            case "1":
                $cache_image = Image::cache( function ( $image ) use ( $size, $name ) {
                    return $image->make( url( '/uploads/' . $name ) )->fit( $size[0], $size[1], function ( $constraint ) {
                        $constraint->upsize();
                    } )->sharpen(5);
                }, $cache_length );
                break;

            /*///////////////////////// crop - WITH upsize */
            case "2":
                $cache_image = Image::cache( function ( $image ) use ( $size, $name ) {
                    return $image->make( url( '/uploads/' . $name ) )->fit( $size[0], $size[1], function ( $constraint ) {
                        //$constraint->upsize();
                    } )->sharpen(5);
                }, $cache_length );
                break;

            /*///////////////////////// No crop & add borders */
            case "3":

                $cache_image = Image::cache( function ( $image ) use ( $size, $name ) {

                    $image->make( url( '/uploads/' . $name ) )->resize( $size[0], $size[1], function ( $constraint ) {
                        $constraint->aspectRatio();
                        $constraint->upsize();
                    } )->sharpen(5);

                    $image->resizeCanvas($size[0], $size[1], 'center', false, array(255, 255, 255, 0.0));

                    return $image;

                }, $cache_length );
                break;

            /*///////////////////////// No crop */
            case "4":
                $cache_image = Image::cache( function ( $image ) use ( $size, $name ) {

                    $image->make( url( '/uploads/' . $name ) )->resize( $size[0], $size[1], function ( $constraint ) {
                        $constraint->aspectRatio();
                        $constraint->upsize();
                    } )->sharpen(5);

                    //$image->resizeCanvas($size[0], $size[1], 'center', false, array(255, 255, 255, 0.0));

                    return $image;

                }, $cache_length );
                break;

        }

        return Response::make( $cache_image, 200, [ 'Content-Type' => 'image' ] )->setMaxAge(604800)->setPublic();

    } else {
        abort( 404 );
    }
} );
2个回答

11

尝试使用encode()方法,可以在其中指定formatquality(对于jpg)。因此,每次使用缓存时,请尝试执行以下操作:

$cache_image = Image::cache(function ($image) use ($size, $name) {

    $image
        ->make(...)
        ->...        // any other call to image manipulation methods
        ->encode('jpg', 75);

    // ...

    return $image;
});

如果它是一个PNG文件,它如何进行区分? - cardi777
换句话说,我需要一些if/then语句来更改编码功能吗?理想情况下,如果我可以使用->encode(null, 75),那就太好了。 - cardi777
4
我不认为你可以这样做。虽然 quality 参数仅适用于保存 jpg 格式的图像,但 format 必须存在:我认为你需要使用一个 if (...) { $format = 'jpg'; } else { $format = 'png'; } 条件语句,然后再使用 ->encode($format, 75); - Marco Pallante
默认情况下,该方法将返回当前图像类型编码的数据。如果尚未定义图像类型,则数据将被编码为jpeg格式。 文档 - Linuslabo

5

您需要使用 Image 类的 save() 方法,其中您将指定图像的质量。方法 save() 的实际签名为:

save([string $path, [int $quality], [string $format]])

以下是示例示例:

<br>
// open an image file<br>
$img = Image::make('public/foo.jpg');
<br>
<br>

// save file as jpg with medium quality<br>
$img->save('public/bar.jpg', 60); <br><br>

// save the same file as jpg with default quality<br>
$img->save('public/baz.jpg');<br><br>

// save the file in png format with good quality<br>
$img->save('public/bar.png', 75);<br><br>

// save the image jpg format defined by third parameter<br>
$img->save('public/foo', 80, 'jpg');
<br><br>

更多信息请参阅http://image.intervention.io/api/save


我猜这个不起作用,因为我尝试了代码,但是图像的大小还是一样的。 - undefined
@CodeFluid 如果你能正确使用干预包,它应该可以正常工作。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接