diff --git a/Imagine/Filter/Loader/BackgroundFilterLoader.php b/Imagine/Filter/Loader/BackgroundFilterLoader.php index 0e1df3a73..5954e517b 100644 --- a/Imagine/Filter/Loader/BackgroundFilterLoader.php +++ b/Imagine/Filter/Loader/BackgroundFilterLoader.php @@ -34,8 +34,51 @@ public function load(ImageInterface $image, array $options = array()) if (isset($options['size'])) { list($width, $height) = $options['size']; + $position = isset($options['position']) ? $options['position'] : 'center'; + switch ($position) { + case 'topleft': + $x = 0; + $y = 0; + break; + case 'top': + $x = ($width - $image->getSize()->getWidth()) / 2; + $y = 0; + break; + case 'topright': + $x = $width - $image->getSize()->getWidth(); + $y = 0; + break; + case 'left': + $x = 0; + $y = ($height - $image->getSize()->getHeight()) / 2; + break; + case 'center': + $x = ($width - $image->getSize()->getWidth()) / 2; + $y = ($height - $image->getSize()->getHeight()) / 2; + break; + case 'right': + $x = $width - $image->getSize()->getWidth(); + $y = ($height - $image->getSize()->getHeight()) / 2; + break; + case 'bottomleft': + $x = 0; + $y = $height - $image->getSize()->getHeight(); + break; + case 'bottom': + $x = ($width - $image->getSize()->getWidth()) / 2; + $y = $height - $image->getSize()->getHeight(); + break; + case 'bottomright': + $x = $width - $image->getSize()->getWidth(); + $y = $height - $image->getSize()->getHeight(); + break; + default: + throw new \InvalidArgumentException("Unexpected position '{$position}'"); + break; + } + $size = new Box($width, $height); - $topLeft = new Point(($width - $image->getSize()->getWidth()) / 2, ($height - $image->getSize()->getHeight()) / 2); + $topLeft = new Point($x, $y); } $canvas = $this->imagine->create($size, $background); diff --git a/Resources/doc/filters.rst b/Resources/doc/filters.rst index 311886911..5a13ed318 100644 --- a/Resources/doc/filters.rst +++ b/Resources/doc/filters.rst @@ -149,7 +149,7 @@ created (with the given size and color) and the original image is placed on top: filter_sets: my_thumb: filters: - background: { size: [1026, 684], color: '#fff' } + background: { size: [1026, 684], position: center, color: '#fff' } The ``watermark`` filter ~~~~~~~~~~~~~~~~~~~~~~~~