Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a border filter along with some tests

  • Loading branch information...
commit 3d4057fa60fcbba623b42fcdd41f9e19d6b44479 1 parent 812ab46
@christeredvartsen authored
View
76 lib/Imagine/Filter/Basic/Border.php
@@ -0,0 +1,76 @@
+<?php
+
+/*
+ * This file is part of the Imagine package.
+ *
+ * (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Imagine\Filter\Basic;
+
+use Imagine\Filter\FilterInterface;
+use Imagine\ImageInterface;
+use Imagine\Image\Color;
+use Imagine\Image\Point;
+
+class Border implements FilterInterface
+{
+ /**
+ * @var Imagine\Image\Color
+ */
+ private $color;
+
+ /**
+ * @var int
+ */
+ private $width;
+
+ /**
+ * @var int
+ */
+ private $height;
+
+ /**
+ * Constructs Border filter with given color, width and height
+ *
+ * @param Imagine\Image\Color $color
+ * @param int $width Width of the border on the left and right sides of the image
+ * @param int $height Height of the border on the top and bottom sides of the image
+ */
+ public function __construct(Color $color, $width = 1, $height = 1)
+ {
+ $this->color = $color;
+ $this->width = $width;
+ $this->height = $height;
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter\FilterInterface::apply()
+ */
+ public function apply(ImageInterface $image)
+ {
+ $size = $image->getSize();
+ $width = $size->getWidth();
+ $height = $size->getHeight();
+
+ $draw = $image->draw();
+
+ // Draw top and bottom lines
+ for ($i = 0; $i < $this->height; $i++) {
+ $draw->line(new Point(0, $i), new Point($width - 1, $i), $this->color)
+ ->line(new Point($width - 1, $height - ($i + 1)), new Point(0, $height - ($i + 1)), $this->color);
+ }
+
+ // Draw sides
+ for ($i = 0; $i < $this->width; $i++) {
+ $draw->line(new Point($i, 0), new Point($i, $height - 1), $this->color)
+ ->line(new Point($width - ($i + 1), 0), new Point($width - ($i + 1), $height - 1), $this->color);
+ }
+
+ return $image;
+ }
+}
View
53 tests/Imagine/Filter/Basic/BorderTest.php
@@ -0,0 +1,53 @@
+<?php
+
+/*
+ * This file is part of the Imagine package.
+ *
+ * (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Imagine\Filter\Basic;
+
+use Imagine\Image\Point;
+use Imagine\Image\Color;
+use Imagine\Filter\FilterTestCase;
+
+class BorderTest extends FilterTestCase
+{
+ public function testBorderImage()
+ {
+ $color = new Color('fff');
+ $width = 2;
+ $height = 4;
+ $image = $this->getImage();
+ $imageWidth = 200;
+ $imageHeight = 100;
+
+ $size = $this->getMock('Imagine\\Image\\BoxInterface');
+ $size->expects($this->once())
+ ->method('getWidth')
+ ->will($this->returnValue($width));
+
+ $size->expects($this->once())
+ ->method('getHeight')
+ ->will($this->returnValue($height));
+
+ $draw = $this->getMock('Imagine\\Draw\\DrawerInterface');
+ $draw->expects($this->exactly(12))->method('line')->will($this->returnValue($draw));
+
+ $image->expects($this->once())
+ ->method('getSize')
+ ->will($this->returnValue($size));
+
+ $image->expects($this->once())
+ ->method('draw')
+ ->will($this->returnValue($draw));
+
+ $filter = new Border($color, $width, $height);
+
+ $this->assertSame($image, $filter->apply($image));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.