Imbo\Image\Transformation\Convert::transform PHP Method

transform() public method

Transform the image
public transform ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface The event instance
    public function transform(EventInterface $event)
    {
        $image = $event->getArgument('image');
        $params = $event->getArgument('params');
        if (empty($params['type'])) {
            throw new TransformationException('Missing required parameter: type', 400);
        }
        $type = $params['type'];
        if ($image->getExtension() === $type) {
            // The requested extension is the same as the image, no conversion is needed
            return;
        }
        try {
            $this->imagick->setImageFormat($type);
            $mimeType = array_search($type, Image::$mimeTypes);
            $image->setMimeType($mimeType)->setExtension($type)->hasBeenTransformed(true);
        } catch (ImagickException $e) {
            throw new TransformationException($e->getMessage(), 400, $e);
        }
    }

Usage Example

Beispiel #1
0
 /**
  * @covers Imbo\Image\Transformation\Convert::transform
  */
 public function testWillNotConvertImageIfNotNeeded()
 {
     $image = $this->getMock('Imbo\\Model\\Image');
     $image->expects($this->once())->method('getExtension')->will($this->returnValue('png'));
     $image->expects($this->never())->method('getBlob');
     $event = $this->getMock('Imbo\\EventManager\\Event');
     $event->expects($this->at(0))->method('getArgument')->with('image')->will($this->returnValue($image));
     $event->expects($this->at(1))->method('getArgument')->with('params')->will($this->returnValue(array('type' => 'png')));
     $this->transformation->transform($event);
 }