Laravel图像干预压缩

时间:2015-07-21 06:12:33

标签: laravel compression intervention

我有一个脚本,可以通过干预来保存和缓存图像,并且它正在100%

工作

然而,我想弄清楚我如何能为jpg&添加75%的压缩png文件,但我不知道我会在这种情况下应用它。

我不认为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 个答案:

答案 0 :(得分:5)

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

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

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

    // ...

    return $image;
});

答案 1 :(得分:2)

您需要使用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