Imbo\Image\Transformation\Compress::compress PHP Method

compress() public method

Apply the compression
public compress ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface The event instance
    public function compress(EventInterface $event)
    {
        if ($this->level === null) {
            return;
        }
        $image = $event->getArgument('image');
        $mimeType = $image->getMimeType();
        if ($mimeType === 'image/gif') {
            // No need to do anything if the image is a GIF
            return;
        }
        try {
            // Levels from 0 - 100 will work for both JPEG and PNG, although the level has different
            // meaning for these two image types. For PNG's a high level will mean more compression,
            // which usually results in a smaller file size, as for JPEG's, a high level means a
            // higher quality, resulting in a larger file size.
            $this->imagick->setImageCompressionQuality($this->level);
            $image->hasBeenTransformed(true);
        } catch (ImagickException $e) {
            throw new TransformationException($e->getMessage(), 400, $e);
        }
    }

Usage Example

Example #1
0
 public function testDoesNotApplyCompressionToGifImages()
 {
     $image = $this->getMock('Imbo\\Model\\Image');
     $image->expects($this->once())->method('getMimeType')->will($this->returnValue('image/gif'));
     $event = $this->getMock('Imbo\\EventManager\\Event');
     $event->expects($this->at(0))->method('getArgument')->with('params')->will($this->returnValue(['level' => 40]));
     $event->expects($this->at(1))->method('getArgument')->with('image')->will($this->returnValue($image));
     $imagick = $this->getMock('Imagick');
     $imagick->expects($this->never())->method('setImageCompressionQuality');
     $this->transformation->setImagick($imagick)->transform($event);
     $this->transformation->compress($event);
 }