Imbo\EventListener\ImageVariations::adjustImageTransformations PHP Метод

adjustImageTransformations() публичный Метод

This method will adjust transformation parameters based on the ration between the original image and the image variation used.
public adjustImageTransformations ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface The current event
    public function adjustImageTransformations(EventInterface $event)
    {
        $request = $event->getRequest();
        $transformations = $request->getTransformations();
        $transformationIndex = $event->getArgument('transformationIndex');
        $ratio = $event->getArgument('ratio');
        $transformationNames = ['crop', 'border', 'canvas', 'watermark'];
        // Adjust coordinates according to the ratio between the original and the variation
        for ($i = 0; $i <= $transformationIndex; $i++) {
            $name = $transformations[$i]['name'];
            $params = $transformations[$i]['params'];
            if (in_array($name, $transformationNames)) {
                foreach (['x', 'y', 'width', 'height'] as $param) {
                    if (isset($params[$param])) {
                        $params[$param] = round($params[$param] / $ratio);
                    }
                }
                $transformations[$i]['params'] = $params;
            }
        }
        $request->setTransformations($transformations);
    }

Usage Example

Пример #1
0
 /**
  * @covers Imbo\EventListener\ImageVariations::adjustImageTransformations
  * @dataProvider getAdjustmentTransformations
  */
 public function testAdjustsTransformationParams($transformations, $index, $ratio, $expectedIndex, $expected)
 {
     $this->event->method('getArgument')->will($this->returnValueMap([['transformationIndex', $index], ['ratio', $ratio]]));
     $this->request->expects($this->once())->method('getTransformations')->will($this->returnValue($transformations));
     $this->request->expects($this->once())->method('setTransformations')->with($this->callback(function ($adjusted) use($expected, $expectedIndex) {
         $diff = array_diff($adjusted[$expectedIndex]['params'], $expected);
         return empty($diff);
     }));
     $this->listener->adjustImageTransformations($this->event);
 }