Skip to content

Instantly share code, notes, and snippets.

@tbmatuka
Created September 26, 2021 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbmatuka/f78ec9dab0a1b6d0f7eee90520a06cfc to your computer and use it in GitHub Desktop.
Save tbmatuka/f78ec9dab0a1b6d0f7eee90520a06cfc to your computer and use it in GitHub Desktop.
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