diff --git a/src/Plugin/ImageEffect/BackgroundImageEffect.php b/src/Plugin/ImageEffect/BackgroundImageEffect.php index a8b9ae5..080d75e 100644 --- a/src/Plugin/ImageEffect/BackgroundImageEffect.php +++ b/src/Plugin/ImageEffect/BackgroundImageEffect.php @@ -94,10 +94,10 @@ class BackgroundImageEffect extends ConfigurableImageEffectBase implements Conta * {@inheritdoc} */ public function getSummary() { - $summary = array( + $summary = [ '#theme' => 'image_effects_background_summary', '#data' => $this->configuration, - ); + ]; $summary += parent::getSummary(); return $summary; diff --git a/src/Plugin/ImageToolkit/Operation/imagemagick/Background.php b/src/Plugin/ImageToolkit/Operation/imagemagick/Background.php new file mode 100644 index 0000000..2615aea --- /dev/null +++ b/src/Plugin/ImageToolkit/Operation/imagemagick/Background.php @@ -0,0 +1,64 @@ +getToolkit()->getWidth(); + $h = $arguments['background_image']->getToolkit()->getHeight(); + // Reverse offset sign. Offset arguments require a sign in front. + $x = $arguments['x_offset'] > 0 ? ('-' . $arguments['x_offset']) : ('+' . -$arguments['x_offset']); + $y = $arguments['y_offset'] > 0 ? ('-' . $arguments['y_offset']) : ('+' . -$arguments['y_offset']); + $op .= " -extent {$w}x{$h}{$x}{$y} "; + + // Add the background image. + $op .= $this->getToolkit()->escapeShellArg($arguments['background_image']->getToolkit()->getSourceLocalPath()); + + // Compose it with the destination. + if ($arguments['opacity'] == 100) { + $op .= ' -compose dst-over -composite'; + } + else { + $op .= " -compose blend -define compose:args=100,{$arguments['opacity']} -composite"; + } + + $this->getToolkit() + ->addArgument($op) + ->setWidth($w) + ->setHeight($h); + return TRUE; + } + +} diff --git a/src/Tests/ImageEffectsBackgroundTest.php b/src/Tests/ImageEffectsBackgroundTest.php index a828345..e0e3ec5 100644 --- a/src/Tests/ImageEffectsBackgroundTest.php +++ b/src/Tests/ImageEffectsBackgroundTest.php @@ -65,13 +65,13 @@ class ImageEffectsBackgroundTest extends ImageEffectsTestBase { $this->assertEqual(40, $image->getWidth()); $this->assertEqual(20, $image->getHeight()); $url = file_url_transform_relative($image_style->buildUrl($original_uri)); - $variables = array( + $variables = [ '#theme' => 'image_style', '#style_name' => 'image_effects_test', '#uri' => $original_uri, '#width' => $image->getWidth(), '#height' => $image->getHeight(), - ); + ]; $this->assertEqual('', $this->getImageTag($variables)); // Check that ::applyEffect generates image with expected canvas. @@ -87,25 +87,45 @@ class ImageEffectsBackgroundTest extends ImageEffectsTestBase { // Remove effect. $this->removeEffectFromTestStyle($uuid); - // For the GD toolkit, test we are not left with orphan resource after - // applying the operation. - if ($image_factory->getToolkitId() === 'gd') { - $image = $image_factory->get($original_uri); - // Store the original GD resource. - $old_res = $image->getToolkit()->getResource(); - // Apply the operation. - $image->apply('background', [ - 'x_offset' => 0, - 'y_offset' => 0, - 'opacity' => 100, - 'background_image' => $image_factory->get($background_uri), - ]); - // The operation replaced the resource, check that the old one has - // been destroyed. - $new_res = $image->getToolkit()->getResource(); - $this->assertTrue(is_resource($new_res)); - $this->assertNotEqual($new_res, $old_res); - $this->assertFalse(is_resource($old_res)); + // Toolkit-specific tests. + switch ($image_factory->getToolkitId()) { + case 'gd': + // For the GD toolkit, test we are not left with orphan resource after + // applying the operation. + $image = $image_factory->get($original_uri); + // Store the original GD resource. + $old_res = $image->getToolkit()->getResource(); + // Apply the operation. + $image->apply('background', [ + 'x_offset' => 0, + 'y_offset' => 0, + 'opacity' => 100, + 'background_image' => $image_factory->get($background_uri), + ]); + // The operation replaced the resource, check that the old one has + // been destroyed. + $new_res = $image->getToolkit()->getResource(); + $this->assertTrue(is_resource($new_res)); + $this->assertNotEqual($new_res, $old_res); + $this->assertFalse(is_resource($old_res)); + break; + + case 'imagemagick': + // For the Imagemagick toolkit, toolkit should return backround + // image dimensions after applying the operation, but before + // saving. + $image = $image_factory->get($original_uri); + // Apply the operation. + $image->apply('background', [ + 'x_offset' => 0, + 'y_offset' => 0, + 'opacity' => 100, + 'background_image' => $image_factory->get($background_uri), + ]); + $this->assertEqual(360, $image->getToolkit()->getWidth()); + $this->assertEqual(240, $image->getToolkit()->getHeight()); + break; + } }