Imbo\Image\Transformation\Watermark::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');
        $width = !empty($params['width']) ? (int) $params['width'] : 0;
        $height = !empty($params['height']) ? (int) $params['height'] : 0;
        $imageIdentifier = !empty($params['img']) ? $params['img'] : $this->defaultImage;
        $position = !empty($params['position']) ? $params['position'] : $this->position;
        $x = !empty($params['x']) ? (int) $params['x'] : $this->x;
        $y = !empty($params['y']) ? (int) $params['y'] : $this->y;
        $opacity = (!empty($params['opacity']) ? (int) $params['opacity'] : 100) / 100;
        if (empty($imageIdentifier)) {
            throw new TransformationException('You must specify an image identifier to use for the watermark', 400);
        }
        // Try to load watermark image from storage
        try {
            $watermarkData = $event->getStorage()->getImage($event->getRequest()->getUser(), $imageIdentifier);
            $watermark = new Imagick();
            $watermark->readImageBlob($watermarkData);
            $watermarkSize = $watermark->getImageGeometry();
            $watermark->setImageOpacity($opacity);
        } catch (StorageException $e) {
            if ($e->getCode() == 404) {
                throw new TransformationException('Watermark image not found', 400);
            }
            throw $e;
        }
        // Should we resize the watermark?
        if ($height || $width) {
            // Calculate width or height if not both have been specified
            if (!$height) {
                $height = $watermarkSize['height'] / $watermarkSize['width'] * $width;
            } else {
                if (!$width) {
                    $width = $watermarkSize['width'] / $watermarkSize['height'] * $height;
                }
            }
            $watermark->thumbnailImage($width, $height);
        } else {
            $width = $watermarkSize['width'];
            $height = $watermarkSize['height'];
        }
        // Determine placement of the watermark
        if ($position === 'top-right') {
            $x = $image->getWidth() - $width + $x;
        } else {
            if ($position === 'bottom-left') {
                $y = $image->getHeight() - $height + $y;
            } else {
                if ($position === 'bottom-right') {
                    $x = $image->getWidth() - $width + $x;
                    $y = $image->getHeight() - $height + $y;
                } else {
                    if ($position === 'center') {
                        $x = $image->getWidth() / 2 - $width / 2 + $x;
                        $y = $image->getHeight() / 2 - $height / 2 + $y;
                    }
                }
            }
        }
        // Now make a composite
        try {
            $this->imagick->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
            $image->hasBeenTransformed(true);
        } catch (ImagickException $e) {
            throw new TransformationException($e->getMessage(), 400, $e);
        }
    }

Usage Example

Example #1
0
 /**
  * @expectedException Imbo\Exception\TransformationException
  * @expectedExceptionCode 400
  * @expectedExceptionMessage Watermark image not found
  */
 public function testThrowsExceptionIfSpecifiedImageIsNotFound()
 {
     $image = $this->getMock('Imbo\\Model\\Image');
     $e = new StorageException('File not found', 404);
     $storage = $this->getMock('Imbo\\Storage\\StorageInterface');
     $storage->expects($this->once())->method('getImage')->with('publickey', 'foobar')->will($this->throwException($e));
     $request = $this->getMock('Imbo\\Http\\Request\\Request');
     $request->expects($this->once())->method('getPublicKey')->will($this->returnValue('publickey'));
     $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('img' => 'foobar')));
     $event->expects($this->at(2))->method('getStorage')->will($this->returnValue($storage));
     $event->expects($this->at(3))->method('getRequest')->will($this->returnValue($request));
     $this->transformation->transform($event);
 }