Created
September 12, 2016 08:47
-
-
Save ragusa87/634982b833d7a812443ebf5c1f442742 to your computer and use it in GitHub Desktop.
LiipImagine Stroke 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
liip_imagine: | |
#... | |
filter_sets: | |
FILTER_NAME: | |
filters: | |
# ... | |
stroke: { color: "#FF0000", size: 5 } |
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: | |
generalmedia.app_bundle.filter.stroke_filter: | |
class: Generalmedia\AccrowinBundle\Filter\StrokeFilter | |
arguments: ["@liip_imagine" ] | |
tags: | |
- { name: "liip_imagine.filter.loader", loader: "stroke" } | |
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 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