10

I have a script which saves and caches images with intervention, and it's working 100%

However i am trying to work out how i can add 75% compression to jpg & png files, but i don't know i would apply it in this situation.

I didn't think PNG files could be compressed apart from software which does it, so im not really sure if its the same thing?

There is an example of compression here: 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 );
    }
} );
cardi777
  • 563
  • 2
  • 9
  • 22

2 Answers2

11

Try the encode() method, where you can specify the format and the quality (for jpg). So, everytime you use the cache, try to do this:

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

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

    // ...

    return $image;
});
Marco Pallante
  • 3,923
  • 1
  • 21
  • 26
  • What if it is a png, how can it discriminate? – cardi777 Jul 21 '15 at 22:55
  • in other words, do i need some kind of if/then statement to change the encode feature? idealy if i could use ->encode(null, 75) that would be great – cardi777 Jul 22 '15 at 01:49
  • 4
    I don't think you can do that. While `quality` parameter is only applied if you are saving `jpg`s, the `format` must be there: I think you need an `if (...) { $format = 'jpg'; } else { $format = 'png'; }` and then `->encode($format, 75);` – Marco Pallante Jul 22 '15 at 07:00
  • "By default the method will return data encoded in the type of the current image. If no image type is defined yet, data will be encoded as jpeg." [docs](http://image.intervention.io/api/encode) – Linuslabo Nov 03 '19 at 22:00
4

You need to use the save() method of the Image class where you will specify the quality of the image. The actual signature of the method save() is:

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

Sample Examples are as follows:

<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>

For more information, please take a look at http://image.intervention.io/api/save

sirandy
  • 1,834
  • 5
  • 27
  • 32
Asra Fud Duha
  • 511
  • 5
  • 9