Skip to content

Instantly share code, notes, and snippets.

@ragusa87
Created September 12, 2016 08:47
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 ragusa87/634982b833d7a812443ebf5c1f442742 to your computer and use it in GitHub Desktop.
Save ragusa87/634982b833d7a812443ebf5c1f442742 to your computer and use it in GitHub Desktop.
LiipImagine Stroke filter
liip_imagine:
#...
filter_sets:
FILTER_NAME:
filters:
# ...
stroke: { color: "#FF0000", size: 5 }
services:
generalmedia.app_bundle.filter.stroke_filter:
class: Generalmedia\AccrowinBundle\Filter\StrokeFilter
arguments: ["@liip_imagine" ]
tags:
- { name: "liip_imagine.filter.loader", loader: "stroke" }
<?php
namespace Generalmedia\AppBundle\Filter;
use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Point;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
/**
* Class StrokeFilter
*/
class StrokeFilter implements LoaderInterface
{
/**
* @var ImagineInterface
*/
protected $imagine;
public function __construct(ImagineInterface $imagine)
{
$this->imagine = $imagine;
}
/**
* @see Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load()
*
* @param \Imagine\Image\ImageInterface $image
* @param array $options
*
* @return \Imagine\Image\ImageInterface
*/
public function load(ImageInterface $image, array $options = [])
{
$options += [
'color' => "#FFF",
'size' => '1',
];
// Create a transparent image
$rgb = new RGB();
$transparent = $rgb->color("#FFFFFF", 0);
$size = $image->getSize();
$bordered = $this->imagine->create($size, $transparent);
$thickness = $options["size"];
$strokeColor = $rgb->color($options["color"]);
// Corners coordinates
$topLeft = new Point(0, 0);
$bottomLeft = new Point(0, $size->getHeight());
$topRight = new Point($size->getWidth(), 0);
$bottomRight = new Point($size->getWidth(), $size->getHeight());
// Draw 4 lines in clockwise, starting from top
$bordered->draw()->line($topLeft, $topRight, $strokeColor, $thickness);
$bordered->draw()->line($topLeft, $bottomLeft, $strokeColor, $thickness);
$bordered->draw()->line($bottomLeft, $bottomRight, $strokeColor, $thickness);
$bordered->draw()->line($topRight, $bottomRight, $strokeColor, $thickness);
return $image->paste($bordered, new Point(0, 0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment