Imbo\Image\Transformation\SmartSize::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['width']) || empty($params['height'])) {
            throw new TransformationException('Both width and height needs to be specified', 400);
        }
        // Get POI from transformation params
        $poi = empty($params['poi']) ? null : explode(',', $params['poi']);
        // Check if we have the POI in metadata
        if (!$poi) {
            $metadataPoi = $this->getPoiFromMetadata($event, $image);
            if ($metadataPoi) {
                $poi = $metadataPoi;
            }
        }
        $event->getResponse()->headers->set('X-Imbo-POIs-Used', $poi ? 1 : 0);
        // Do a simple crop if don't have a POI
        if (!$poi) {
            return $this->simpleCrop($event, $params['width'], $params['height']);
        }
        if (!empty($params['crop']) && array_search($params['crop'], ['close', 'medium', 'wide']) === false) {
            throw new TransformationException('Invalid crop value. Valid values are: close,medium,wide', 400);
        }
        $targetWidth = $params['width'];
        $targetHeight = $params['height'];
        $closeness = isset($params['crop']) ? $params['crop'] : 'medium';
        $crop = $this->calculateCrop(['focalX' => $poi[0], 'focalY' => $poi[1], 'sourceWidth' => $image->getWidth(), 'sourceHeight' => $image->getHeight(), 'targetWidth' => $targetWidth, 'targetHeight' => $targetHeight, 'growFactor' => $this->getGrowFactor($closeness), 'sourcePortionThreshold' => $this->getSourcePercentageThreshold($closeness)]);
        try {
            $this->imagick->cropImage($crop['width'], $crop['height'], $crop['left'], $crop['top']);
            $this->imagick->setImagePage(0, 0, 0, 0);
            $this->resize($image, $targetWidth, $targetHeight);
        } catch (ImagickException $e) {
            throw new TransformationException($e->getMessage(), 400, $e);
        }
    }

Usage Example

Beispiel #1
0
 /**
  * @covers Imbo\Image\Transformation\SmartSize::transform
  * @dataProvider getSmartSizeArguments
  */
 public function testSmartSize($imageDimensions, $params, $cropParams)
 {
     $imagick = $this->getMock('Imagick');
     $imagick->expects($this->any())->method('cropImage')->with($cropParams['width'], $cropParams['height'], $cropParams['left'], $cropParams['top']);
     $image = new Image();
     $image->setWidth($imageDimensions['width']);
     $image->setHeight($imageDimensions['height']);
     $response = new Response();
     $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($params));
     $event->expects($this->at(2))->method('getResponse')->will($this->returnValue($response));
     $transformation = new SmartSize();
     $transformation->setImagick($imagick);
     $transformation->transform($event);
 }