【问题标题】:Laravel image intervention compressionLaravel 图像干预压缩
【发布时间】:2015-07-21 06:12:32
【问题描述】:

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

但是我正在尝试研究如何将 75% 压缩添加到 jpg 和 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 );
    }
} );

【问题讨论】:

    标签: laravel compression intervention


    【解决方案1】:

    尝试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,怎么区分呢?
    • 换句话说,我是否需要某种 if/then 语句来更改编码功能?理想情况下,如果我可以使用 ->encode(null, 75) 那就太好了
    • 我认为你做不到。虽然quality 参数仅在您保存jpgs 时应用,但format 必须存在:我认为您需要if (...) { $format = 'jpg'; } else { $format = 'png'; } 然后->encode($format, 75);
    • "默认情况下,该方法将返回以当前图像类型编码的数据。如果尚未定义图像类型,数据将被编码为 jpeg。" docs
    【解决方案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

    【讨论】:

      猜你喜欢
      • 2018-01-11
      • 2015-02-09
      • 1970-01-01
      • 2019-01-16
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多