View source
<?php
namespace Drupal\Tests\image_effects\Functional\Effect;
use Drupal\Tests\image_effects\Functional\ImageEffectsTestBase;
class WatermarkTest extends ImageEffectsTestBase {
public function testWatermarkEffect($toolkit_id, $toolkit_config, array $toolkit_settings) {
$this
->changeToolkit($toolkit_id, $toolkit_config, $toolkit_settings);
$original_uri = $this
->getTestImageCopyUri('/files/image-1.png', 'simpletest');
$derivative_uri = $this->testImageStyle
->buildUri($original_uri);
$watermark_uri = $this
->getTestImageCopyUri('/files/image-test.png', 'simpletest');
$effect = [
'id' => 'image_effects_watermark',
'data' => [
'watermark_image' => $watermark_uri,
'placement' => 'left-top',
'x_offset][c0][c1][value' => 1,
'x_offset][c0][c1][uom' => 'px',
'y_offset][c0][c1][value' => 1,
'y_offset][c0][c1][uom' => 'px',
'opacity' => 100,
],
];
$uuid = $this
->addEffectToTestStyle($effect);
$this->testImageStyle
->createDerivative($original_uri, $derivative_uri);
$image = $this->imageFactory
->get($derivative_uri, 'gd');
$watermark = $this->imageFactory
->get($watermark_uri, 'gd');
$this
->assertColorsAreNotEqual($this
->getPixelColor($watermark, 0, 0), $this
->getPixelColor($image, 0, 0));
$this
->assertColorsAreEqual($this
->getPixelColor($watermark, 0, 0), $this
->getPixelColor($image, 1, 1));
$this
->assertColorsAreEqual($this
->getPixelColor($watermark, 0, 1), $this
->getPixelColor($image, 1, 2));
$this
->assertColorsAreEqual($this
->getPixelColor($watermark, 0, 3), $this
->getPixelColor($image, 1, 4));
$this
->removeEffectFromTestStyle($uuid);
$original_uri = $this
->getTestImageCopyUri('/files/image-1.png', 'simpletest');
$derivative_uri = $this->testImageStyle
->buildUri($original_uri);
$watermark_uri = $this
->getTestImageCopyUri('/tests/images/fuchsia.png', 'image_effects');
$effect = [
'id' => 'image_effects_watermark',
'data' => [
'watermark_image' => $watermark_uri,
'placement' => 'left-top',
'x_offset][c0][c1][value' => NULL,
'y_offset][c0][c1][value' => NULL,
'opacity' => 100,
'watermark_resize][watermark_width][c0][c1][value' => 5,
'watermark_resize][watermark_width][c0][c1][uom' => 'perc',
],
];
$uuid = $this
->addEffectToTestStyle($effect);
$this->testImageStyle
->createDerivative($original_uri, $derivative_uri);
$image = $this->imageFactory
->get($derivative_uri, 'gd');
$this
->assertColorsAreClose($this
->getPixelColor($image, 17, 0), $this->fuchsia, 4);
$this
->assertColorsAreNotClose($this
->getPixelColor($image, 19, 0), $this->fuchsia, 4);
$this
->assertColorsAreClose($this
->getPixelColor($image, 0, 13), $this->fuchsia, 4);
$this
->assertColorsAreNotClose($this
->getPixelColor($image, 0, 15), $this->fuchsia, 4);
$this
->removeEffectFromTestStyle($uuid);
$original_uri = $this
->getTestImageCopyUri('/tests/images/fuchsia.png', 'image_effects');
$derivative_uri = $this->testImageStyle
->buildUri($original_uri);
$watermark_uri = $this
->getTestImageCopyUri('/files/image-test.png', 'simpletest');
$effect = [
'id' => 'image_effects_watermark',
'data' => [
'watermark_image' => $watermark_uri,
'placement' => 'left-top',
'x_offset][c0][c1][value' => NULL,
'y_offset][c0][c1][value' => NULL,
'opacity' => 100,
],
];
$uuid = $this
->addEffectToTestStyle($effect);
$this->testImageStyle
->createDerivative($original_uri, $derivative_uri);
$image = $this->imageFactory
->get($derivative_uri, 'gd');
$this
->assertColorsAreEqual($this
->getPixelColor($image, 0, 19), $this->fuchsia);
$this
->removeEffectFromTestStyle($uuid);
if ($this->imageFactory
->getToolkitId() === 'imagemagick' && \Drupal::configFactory()
->get('imagemagick.settings')
->get('binaries') === 'graphicsmagick') {
return;
}
$original_uri = $this
->getTestImageCopyUri('/tests/images/fuchsia.png', 'image_effects');
$derivative_uri = $this->testImageStyle
->buildUri($original_uri);
$watermark_uri = $this
->getTestImageCopyUri('/files/image-test.png', 'simpletest');
$effect = [
'id' => 'image_effects_watermark',
'data' => [
'watermark_image' => $watermark_uri,
'placement' => 'left-top',
'x_offset][c0][c1][value' => NULL,
'y_offset][c0][c1][value' => NULL,
'opacity' => 50,
],
];
$uuid = $this
->addEffectToTestStyle($effect);
$this->testImageStyle
->createDerivative($original_uri, $derivative_uri);
$image = $this->imageFactory
->get($derivative_uri, 'gd');
$this
->assertColorsAreEqual($this
->getPixelColor($image, 0, 19), $this->fuchsia);
$this
->assertColorsAreClose($this
->getPixelColor($image, 39, 0), $this->grey, 4);
$this
->removeEffectFromTestStyle($uuid);
}
}