Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Canvas filter #94

Merged
merged 10 commits into from

3 participants

@christeredvartsen

This pull request implements a canvas filter that enables you to define a new canvas size, and place the current image somewhere in the new image.

Example usage:

$imagine = new Imagine\Imagick\Imagine();
$image = $imagine->open('/path/to/image.png');

// Apply canvas
$image->canvas(
    // New canvas size
    new Imagine\Image\Box(200, 200),

    // Where to place the existing image (defaults to 0, 0)
    new Imagine\Image\Point(20, 20),

    // Optional background image for the regions that go outside of the existing image
    new Imagine\Image\Color('000')
);
@avalanche123

Why not use

<?php
$canvas = $imagine->create(new Imagine\Image\Box(200, 200), new Imagine\Image\Color('000'))
    ->paste($imagine->open('/path/to/image.png'), new Imagine\Image\Point(20, 20));

?

@christeredvartsen

That is what I already do. :) Just thought you might want a convenient filter for it.

@stof

well, you are duplicating lots of stuff. Why not simply providing a transformation using the code suggested by @avalanche123 instead of adding a new method ?

@christeredvartsen

Sure. I can take a look at that.

@avalanche123

I think this is a worthy addition in case you use it often enough.
What do you think about creating a new Filter namespace for these filters?

@christeredvartsen

You mean a different namespace just for the filter class? And still have the other stuff as-is?

@avalanche123

I still think that a new method on the image interface is unnecessary, but I think you could make a filter using example code I showed. that filter wouldn't be basic anymore since it uses multiple methods and needs imagine instance.

But this was more of a RFC, what do you guys think of creating a new filter namespace and new types of filters?

@christeredvartsen

I think it sounds like a good idea. I have done several things with Imagine that needed more instances of imagine, such as the code above.

I also have a border filter pull request around here somewhere that also might fit in this new namespace? I remember you mentioning creating a new namespace for more advanced filters back then as well.

@avalanche123

Yes, I remember that one too

@christeredvartsen

Just write a comment once you have figured out what namespace to use and I'll be happy to rewrite the code attached to this PR (and the code in the border-PR if you want that).

@avalanche123
Owner

ok, I just merged your border filter under advanced namespace, can you change this one too so I can merge it?

@christeredvartsen

Should Imagine\Filter\Transformation support the advanced filters as well or shall I remove canvas() from it? I can see you haven't added any border().

If you want it to have advanced filters I can add border() to it as well if you want while I'm at it.

@christeredvartsen

Wow. Seems like I really messed up the PR now. :/

Not sure what just happened but I just rebased my develop branch on yours, and rebased this feature branch on my develop, and pushed to my fork.

@christeredvartsen

I'm not sure how to clean this up so I can just move the relevant parts to another PR if you want.

Before I start, would you like me to change to implementation to use another Imagine instance (like your example above) and remove the methods in the different image implementations? I can see you didn't implement border() on any of the images as well, so I take it you don't want advanced filters on the images?

@stof

@christeredvartsen the issue is that you merged your remote branch after rebasing instead of forcing the push, meaning you now have the commits twice in the branch

@christeredvartsen

@stof Is there any way of cleaning this up? There aren't that many changes so I might as well just create a new feature branch...

@stof

@christeredvartsen rebase again and force the push this time. It should drop the duplicated commits as the changes will already be there

@christeredvartsen

I think I actually managed to pull it off despite my level 1 in git skills. Now I'll wait for @avalanche123 to answer the questions. :)

@avalanche123
Owner

Hi! Yes, I think this should be just a filter without modifications to Image or Transformation

@avalanche123
Owner

And yes, the filter should take Imagine instance in the constructor, which it will then use to create canvas it will paste image onto

@christeredvartsen

@avalanche123 Updated the branch. Not sure if you want a specific parameter order, but the ctor of the filter now takes an imagine instance as the first argument. I have updated the test case as well.

@avalanche123
Owner

perfect, thanks!

@avalanche123 avalanche123 merged commit df2eeb9 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
72 lib/Imagine/Filter/Advanced/Canvas.php
@@ -0,0 +1,72 @@
+<?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\Advanced;
+
+use Imagine\Filter\FilterInterface;
+use Imagine\Image\ImageInterface;
+use Imagine\Image\BoxInterface;
+use Imagine\Image\Point;
+use Imagine\Image\PointInterface;
+use Imagine\Image\Color;
+use Imagine\Image\ImagineInterface;
+
+class Canvas implements FilterInterface
+{
+ /**
+ * @var Imagine\Image\BoxInterface
+ */
+ private $size;
+
+ /**
+ * @var Imagine\Image\PointInterface
+ */
+ private $placement;
+
+ /**
+ * @var Imagine\Image\Color
+ */
+ private $background;
+
+ /**
+ * @var Imagine\Image\ImagineInterface
+ */
+ private $imagine;
+
+ /**
+ * Constructs Canvas filter with given width and height and the placement of the current image
+ * inside the new canvas
+ *
+ * @param Imagine\Image\ImagineInterface $imagine
+ * @param Imagine\Image\BoxInterface $size
+ * @param Imagine\Image\PointInterface $placement
+ * @param Imagine\Image\Color $background
+ */
+ public function __construct(ImagineInterface $imagine, BoxInterface $size, PointInterface $placement = null, Color $background = null)
+ {
+ $this->imagine = $imagine;
+ $this->size = $size;
+ $this->placement = $placement ?: new Point(0, 0);
+ $this->background = $background;
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter\FilterInterface::apply()
+ */
+ public function apply(ImageInterface $image)
+ {
+ $canvas = $this->imagine->create($this->size, $this->background);
+ $canvas->paste($image, $this->placement);
+
+ return $canvas;
+ }
+}
View
61 tests/Imagine/Filter/Advanced/CanvasTest.php
@@ -0,0 +1,61 @@
+<?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\Advanced;
+
+use Imagine\Image\Box;
+use Imagine\Image\BoxInterface;
+use Imagine\Image\Point;
+use Imagine\Image\PointInterface;
+use Imagine\Image\Color;
+use Imagine\Filter\FilterTestCase;
+
+class CanvasTest extends FilterTestCase
+{
+ /**
+ * @covers Imagine\Filter\Advanced\Canvas::apply
+ *
+ * @dataProvider getDataSet
+ *
+ * @param BoxInterface $size
+ * @param PointInterface $placement
+ * @param Color $background
+ */
+ public function testShouldCanvasImageAndReturnResult(BoxInterface $size, PointInterface $placement = null, Color $background = null)
+ {
+ $placement = $placement ?: new Point(0, 0);
+ $image = $this->getImage();
+
+ $canvas = $this->getImage();
+ $canvas->expects($this->once())->method('paste')->with($image, $placement);
+
+ $imagine = $this->getImagine();
+ $imagine->expects($this->once())->method('create')->with($size, $background)->will($this->returnValue($canvas));
+
+ $command = new Canvas($imagine, $size, $placement, $background);
+
+ $this->assertSame($canvas, $command->apply($image));
+ }
+
+ /**
+ * Data provider for testShouldCanvasImageAndReturnResult
+ *
+ * @return array
+ */
+ public function getDataSet()
+ {
+ return array(
+ array(new Box(50, 15), new Point(10, 10), new Color('fff')),
+ array(new Box(300, 25), new Point(15, 15)),
+ array(new Box(123, 23)),
+ );
+ }
+}
View
5 tests/Imagine/Filter/FilterTestCase.php
@@ -17,4 +17,9 @@ protected function getImage()
{
return $this->getMock('Imagine\\Image\\ImageInterface');
}
+
+ protected function getImagine()
+ {
+ return $this->getMock('Imagine\\Image\\ImagineInterface');
+ }
}
Something went wrong with that request. Please try again.