View source
<?php
namespace Drupal\Tests\image_effects\Functional\Effect;
use Drupal\Tests\image_effects\Functional\ImageEffectsTestBase;
class BackgroundTest extends ImageEffectsTestBase {
public function providerToolkits() {
$toolkits = parent::providerToolkits();
unset($toolkits['ImageMagick-graphicsmagick']);
return $toolkits;
}
public function testBackgroundEffect($toolkit_id, $toolkit_config, array $toolkit_settings) {
$this
->changeToolkit($toolkit_id, $toolkit_config, $toolkit_settings);
$original_uri = $this
->getTestImageCopyUri('core/tests/fixtures/files/image-test.png');
$background_uri = $this
->getTestImageCopyUri('core/tests/fixtures/files/image-1.png');
$effect = [
'id' => 'image_effects_background',
'data' => [
'placement' => 'left-top',
'x_offset' => 0,
'y_offset' => 0,
'opacity' => 100,
'background_image' => $background_uri,
],
];
$uuid = $this
->addEffectToTestStyle($effect);
$image = $this->imageFactory
->get($original_uri);
$this
->assertEquals(40, $image
->getWidth());
$this
->assertEquals(20, $image
->getHeight());
$derivative_url = file_url_transform_relative($this->testImageStyle
->buildUrl($original_uri));
$variables = [
'#theme' => 'image_style',
'#style_name' => 'image_effects_test',
'#uri' => $original_uri,
'#width' => $image
->getWidth(),
'#height' => $image
->getHeight(),
];
$this
->assertRegExp("/\\<img src=\"" . preg_quote($derivative_url, '/') . "\" width=\"360\" height=\"240\" alt=\"\" .*class=\"image\\-style\\-image\\-effects\\-test\" \\/\\>/", $this
->getImageTag($variables));
$derivative_uri = $this->testImageStyle
->buildUri($original_uri);
$this->testImageStyle
->createDerivative($original_uri, $derivative_uri);
$image = $this->imageFactory
->get($derivative_uri, 'gd');
$this
->assertEquals(360, $image
->getWidth());
$this
->assertEquals(240, $image
->getHeight());
$this
->assertColorsAreEqual($this->red, $this
->getPixelColor($image, 0, 0));
$this
->assertColorsAreEqual($this->green, $this
->getPixelColor($image, 39, 0));
$this
->assertColorsAreEqual([
185,
185,
185,
0,
], $this
->getPixelColor($image, 0, 19));
$this
->assertColorsAreEqual($this->blue, $this
->getPixelColor($image, 39, 19));
$this
->removeEffectFromTestStyle($uuid);
switch ($this->imageFactory
->getToolkitId()) {
case 'gd':
$image = $this->imageFactory
->get($original_uri);
$old_res = $image
->getToolkit()
->getResource();
$image
->apply('background', [
'x_offset' => 0,
'y_offset' => 0,
'opacity' => 100,
'background_image' => $this->imageFactory
->get($background_uri),
]);
if (PHP_VERSION_ID < 80000) {
$new_res = $image
->getToolkit()
->getResource();
$this
->assertTrue(is_resource($new_res));
$this
->assertNotEquals($new_res, $old_res);
$this
->assertFalse(is_resource($old_res));
}
$this
->assertTrue($image
->save($original_uri . '.modified.png'));
$image_original = $this->imageFactory
->get($original_uri);
$image_modified = $this->imageFactory
->get($original_uri . '.modified.png');
$this
->assertImagesAreNotEqual($image_original, $image_modified);
break;
case 'imagemagick':
$image = $this->imageFactory
->get($original_uri);
$image
->apply('background', [
'x_offset' => 0,
'y_offset' => 0,
'opacity' => 100,
'background_image' => $this->imageFactory
->get($background_uri),
]);
$this
->assertEquals(360, $image
->getToolkit()
->getWidth());
$this
->assertEquals(240, $image
->getToolkit()
->getHeight());
break;
}
}
}