Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added `LayersInterface#coalesce()`.

Moved image coalescing from `Imagine\Imagick\Imagine` to `LayersInterface#coalesce()`.
  • Loading branch information...
commit a43af3bcd04cabf7338f104f9f3894753bbfc7be 1 parent 3d66c4b
@rjkip rjkip authored
View
7 lib/Imagine/Gd/Layers.php
@@ -41,6 +41,13 @@ public function merge()
/**
* {@inheritdoc}
*/
+ public function coalesce()
+ {
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function current()
{
return new Image($this->resource);
View
8 lib/Imagine/Gmagick/Layers.php
@@ -59,6 +59,14 @@ public function merge()
/**
* {@inheritdoc}
*/
+ public function coalesce()
+ {
+ throw new RuntimeException("Gmagick does not support coalescing");
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function current()
{
if (!isset($this->layers[$this->offset])) {
View
6 lib/Imagine/Image/LayersInterface.php
@@ -22,4 +22,10 @@
* @throws RuntimeException
*/
public function merge();
+
+ /**
+ * Coalesce layers. Each layer in the sequence is the same size as the first and composited with the next layer in
+ * the sequence.
+ */
+ public function coalesce();
}
View
4 lib/Imagine/Imagick/Imagine.php
@@ -107,8 +107,6 @@ public function load($string)
$imagick->readImageBlob($string);
$imagick->setImageMatte(true);
- $imagick = $imagick->coalesceImages();
-
return new Image($imagick);
} catch (\ImagickException $e) {
throw new RuntimeException(
@@ -129,8 +127,6 @@ public function read($resource)
try {
$imagick = new \Imagick();
$imagick->readImageFile($resource);
-
- $imagick = $imagick->coalesceImages();
} catch (\ImagickException $e) {
throw new RuntimeException(
'Could not read image from resource', $e->getCode(), $e
View
19 lib/Imagine/Imagick/Layers.php
@@ -58,6 +58,25 @@ public function merge()
/**
* {@inheritdoc}
*/
+ public function coalesce()
+ {
+ $coalesced_resource = $this->resource->coalesceImages();
+
+ foreach ($this as $offset => $image) {
+ try {
+ $coalesced_resource->setIteratorIndex($offset);
+ $this->layers[$offset] = $coalesced_resource->getImage();
+ } catch (\ImagickException $e) {
+ throw new RuntimeException(
+ 'Failed to retrieve layer', $e->getCode(), $e
+ );
+ }
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function current()
{
if (!isset($this->layers[$this->offset])) {
View
71 tests/Imagine/Image/AbstractImageTest.php
@@ -238,77 +238,6 @@ public function testLayerOnMultiLayeredImage()
}
}
- public function testMultiLayeredImageHasConsistentSize()
- {
- if (!$this->supportMultipleLayers()) {
- $this->markTestSkipped('This driver does not support multiple layers');
- }
-
- $width = null;
- $height = null;
-
- foreach ($this->getInconsistentMultiLayeredImage()->layers() as $layer) {
- $size = $layer->getSize();
-
- if ($width === null) {
- $width = $size->getWidth();
- } else {
- $this->assertEquals($width, $size->getWidth());
- }
-
- if ($height === null) {
- $height = $size->getHeight();
- } else {
- $this->assertEquals($height, $size->getHeight());
- }
- }
- }
-
- public function testMultiLayeredImageToMonoLayeredPng()
- {
- if (!$this->supportMultipleLayers()) {
- $this->markTestSkipped('This driver does not support multiple layers');
- }
-
- $path = "tests/Imagine/Fixtures/results/layers-multi-mono.png";
-
- $image = $this->getInconsistentMultiLayeredImage();
- $image->save($path, array("format" => "png"));
-
- $image = $this->getImagine()->open($path);
- $this->assertEquals(1, count($image->layers()));
- }
-
- public function testMultiLayeredImageToMultiLayeredGif()
- {
- if (!$this->supportMultipleLayers()) {
- $this->markTestSkipped('This driver does not support multiple layers');
- }
-
- $path = "tests/Imagine/Fixtures/results/layers-multi-multi.gif";
-
- $image = $this->getInconsistentMultiLayeredImage();
- $image->save($path, array("format" => "gif", "flatten" => false));
-
- $image = $this->getImagine()->open($path);
- $this->assertGreaterThan(1, count($image->layers()));
- }
-
- public function testMultiLayeredImageToMonoLayeredGif()
- {
- if (!$this->supportMultipleLayers()) {
- $this->markTestSkipped('This driver does not support multiple layers');
- }
-
- $path = "tests/Imagine/Fixtures/results/layers-multi-multi.gif";
-
- $image = $this->getInconsistentMultiLayeredImage();
- $image->save($path, array("format" => "gif"));
-
- $image = $this->getImagine()->open($path);
- $this->assertEquals(1, count($image->layers()));
- }
-
private function getMonoLayeredImage()
{
return $this->getImagine()->open('tests/Imagine/Fixtures/google.png');
View
29 tests/Imagine/Imagick/LayersTest.php
@@ -64,6 +64,35 @@ public function testGetLayer()
}
}
+ public function testCoalesce()
+ {
+ $width = null;
+ $height = null;
+
+ $resource = new \Imagick;
+ $resource->newImage(20, 10, new \ImagickPixel("black"));
+ $resource->newImage(10, 10, new \ImagickPixel("black"));
+
+ $layers = new Layers(new Image($resource), $resource);
+ $layers->coalesce();
+
+ foreach ($layers as $layer) {
+ $size = $layer->getSize();
+
+ if ($width === null) {
+ $width = $size->getWidth();
+ } else {
+ $this->assertEquals($width, $size->getWidth());
+ }
+
+ if ($height === null) {
+ $height = $size->getHeight();
+ } else {
+ $this->assertEquals($height, $size->getHeight());
+ }
+ }
+ }
+
protected function getImagine()
{
return new Imagine();
Please sign in to comment.
Something went wrong with that request. Please try again.