Skip to content

Commit

Permalink
Merge pull request #721 from uvoelkel/background-filter-position
Browse files Browse the repository at this point in the history
background filter: allow image positioning
  • Loading branch information
lsmith77 authored Jul 12, 2016
2 parents 94aca9c + e9160b1 commit 4bf0e7d
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 4bf0e7d

Please sign in to comment.