Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LiipImagineBundle placeholder outline filter
<?php
namespace App\Service;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine\Image\Point;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
class PlaceholderOutlineFilter implements LoaderInterface
{
/**
* Extends the image by 1 pixel in all directions using the current border colors.
*/
public function load(ImageInterface $image, array $options = []): ImageInterface
{
$width = $image->getSize()->getWidth() + 2;
$height = $image->getSize()->getHeight() + 2;
$newSize = new Box($width, $height);
$originalImage = $image->copy();
$image->resize($newSize);
$pastePoints = [
[0, 0], // corners first
[2, 0],
[0, 2],
[2, 2],
[1, 0], // edges
[0, 1],
[2, 1],
[1, 2],
[1, 1], // center
];
foreach ($pastePoints as $pastePoint) {
$point = new Point($pastePoint[0], $pastePoint[1]);
$image->paste($originalImage, $point);
}
return $image;
}
}
services:
App\Service\PlaceholderOutlineFilter:
tags:
- { name: "liip_imagine.filter.loader", loader: placeholder }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment