Imbo\Image\Transformation\Crop::transform PHP Метод

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

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');
        foreach (['width', 'height'] as $param) {
            if (!isset($params[$param])) {
                throw new TransformationException('Missing required parameter: ' . $param, 400);
            }
        }
        // Fetch the x, y, width and height of the resulting image
        $x = !empty($params['x']) ? (int) $params['x'] : $this->x;
        $y = !empty($params['y']) ? (int) $params['y'] : $this->y;
        $mode = !empty($params['mode']) ? $params['mode'] : null;
        $width = (int) $params['width'];
        $height = (int) $params['height'];
        $imageWidth = $image->getWidth();
        $imageHeight = $image->getHeight();
        // Set correct x and/or y values based on the crop mode
        if ($mode === 'center' || $mode === 'center-x') {
            $x = (int) ($imageWidth - $width) / 2;
        }
        if ($mode === 'center' || $mode === 'center-y') {
            $y = (int) ($imageHeight - $height) / 2;
        }
        // Throw exception on X/Y values that are out of bounds
        if ($x + $width > $imageWidth) {
            throw new TransformationException('Crop area is out of bounds (`x` + `width` > image width)', 400);
        } else {
            if ($y + $height > $imageHeight) {
                throw new TransformationException('Crop area is out of bounds (`y` + `height` > image height)', 400);
            }
        }
        // Return if there is no need for cropping
        if ($x === 0 && $y === 0 && $imageWidth <= $width && $imageHeight <= $height) {
            return;
        }
        try {
            $this->imagick->cropImage($width, $height, $x, $y);
            $this->imagick->setImagePage(0, 0, 0, 0);
            $size = $this->imagick->getImageGeometry();
            $image->setWidth($size['width'])->setHeight($size['height'])->hasBeenTransformed(true);
        } catch (ImagickException $e) {
            throw new TransformationException($e->getMessage(), 400, $e);
        }
    }

Usage Example

Пример #1
0
 /**
  * @covers Imbo\Image\Transformation\Crop::transform
  * @expectedException Imbo\Exception\TransformationException
  * @expectedExceptionCode 400
  * @expectedExceptionMessage Missing required parameter: height
  */
 public function testThrowsExceptionWhenHeightIsMissing()
 {
     $event = $this->getMock('Imbo\\EventManager\\Event');
     $event->expects($this->at(0))->method('getArgument')->with('image')->will($this->returnValue($this->getMock('Imbo\\Model\\Image')));
     $event->expects($this->at(1))->method('getArgument')->with('params')->will($this->returnValue(array('width' => 123)));
     $transformation = new Crop();
     $transformation->transform($event);
 }