Skip to content

Commit

Permalink
paste layers one by one, centered
Browse files Browse the repository at this point in the history
BC-Break
- options `size` and `position` are removed
- option `transparency` is renamed `opacity` to conform to values 0=transparent ; 100=full opaque color
  • Loading branch information
jygaulier committed Nov 19, 2024
1 parent d4f5449 commit 0594deb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Alchemy\RenditionFactory\Transformer\Image\Imagine;

use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\Point;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;

class BackgroundFilterLoader implements LoaderInterface
{
public function __construct(protected ImagineInterface $imagine)
{
}

public function load(ImageInterface $image, array $options = [])
{
$background = $image->palette()->color(
$options['color'] ?? '#fff',
$options['opacity'] ?? 100,
);
$imageSize = $image->getSize();

$imageW = $imageSize->getWidth();
$imageH = $imageSize->getHeight();

$canvas = $this->imagine->create($imageSize, $background);

/**
* @var ImageInterface $layer
*/
foreach ($image->layers() as $layer) {
$layerSize = $layer->getSize();
$layerDW = ($imageW - $layerSize->getWidth()) / 2;
$layerDH = ($imageH - $layerSize->getHeight()) / 2;
$canvas->paste($layer, new Point($layerDW, $layerDH));
}

return $canvas;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Liip\ImagineBundle\Imagine\Filter\Loader\AutoRotateFilterLoader;
use Liip\ImagineBundle\Imagine\Filter\Loader\BackgroundFilterLoader;
use Liip\ImagineBundle\Imagine\Filter\Loader\CropFilterLoader;
use Liip\ImagineBundle\Imagine\Filter\Loader\DownscaleFilterLoader;
use Liip\ImagineBundle\Imagine\Filter\Loader\FixedFilterLoader;
Expand Down

0 comments on commit 0594deb

Please sign in to comment.