Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

background filter: allow image positioning #721

Merged
merged 1 commit into from
Jul 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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