Skip to content

Commit

Permalink
background filter: allow image positioning
Browse files Browse the repository at this point in the history
allow positioning of the image on the background.
uses the same position options as the watermark filter.
  • Loading branch information
uvoelkel committed Mar 30, 2016
1 parent 0a7932e commit e9160b1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 44 additions & 1 deletion Imagine/Filter/Loader/BackgroundFilterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit e9160b1

Please sign in to comment.