Imbo\EventListener\ImageTransformer::transform PHP Method

transform() public method

Transform images
public transform ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface The current event
    public function transform(EventInterface $event)
    {
        $request = $event->getRequest();
        $image = $event->getResponse()->getModel();
        $eventManager = $event->getManager();
        $presets = $event->getConfig()['transformationPresets'];
        // Fetch transformations specifed in the query and transform the image
        foreach ($request->getTransformations() as $transformation) {
            if (isset($presets[$transformation['name']])) {
                // Preset
                foreach ($presets[$transformation['name']] as $name => $params) {
                    if (is_int($name)) {
                        // No hardcoded params, use the ones from the request
                        $name = $params;
                        $params = $transformation['params'];
                    } else {
                        // Some hardcoded params. Merge with the ones from the request, making the
                        // hardcoded params overwrite the ones from the request
                        $params = array_replace($transformation['params'], $params);
                    }
                    $eventManager->trigger('image.transformation.' . strtolower($name), ['image' => $image, 'params' => $params]);
                }
            } else {
                // Regular transformation
                $eventManager->trigger('image.transformation.' . strtolower($transformation['name']), ['image' => $image, 'params' => $transformation['params']]);
            }
        }
    }

Usage Example

Example #1
0
 /**
  * @covers Imbo\EventListener\ImageTransformer::transform
  */
 public function testPresetsCanHardcodeSomeParameters()
 {
     $this->event->expects($this->once())->method('getConfig')->will($this->returnValue(array('transformationPresets' => array('preset' => array('thumbnail' => array('height' => 75))))));
     $this->request->expects($this->once())->method('getTransformations')->will($this->returnValue(array(array('name' => 'preset', 'params' => array('width' => '100', 'height' => '200')))));
     $this->eventManager->expects($this->once())->method('trigger')->with('image.transformation.thumbnail', array('image' => $this->image, 'params' => array('width' => '100', 'height' => 75)));
     $this->listener->transform($this->event);
 }