LiipImagineBundle placeholder outline filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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