You are here

public function GaussianBlurTest::testGaussianBlurEffect in Image Effects 8

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/Effect/GaussianBlurTest.php \Drupal\Tests\image_effects\Functional\Effect\GaussianBlurTest::testGaussianBlurEffect()
  2. 8.2 tests/src/Functional/Effect/GaussianBlurTest.php \Drupal\Tests\image_effects\Functional\Effect\GaussianBlurTest::testGaussianBlurEffect()

Gaussian blur effect test.

@dataProvider providerToolkits

Parameters

string $toolkit_id: The id of the toolkit to set up.

string $toolkit_config: The config object of the toolkit to set up.

array $toolkit_settings: The settings of the toolkit to set up.

File

tests/src/Functional/Effect/GaussianBlurTest.php, line 26

Class

GaussianBlurTest
Gaussian blur effect test.

Namespace

Drupal\Tests\image_effects\Functional\Effect

Code

public function testGaussianBlurEffect($toolkit_id, $toolkit_config, array $toolkit_settings) {
  $this
    ->changeToolkit($toolkit_id, $toolkit_config, $toolkit_settings);
  $effect = [
    'id' => 'image_effects_gaussian_blur',
    'data' => [
      'radius' => 3,
      'sigma' => 2,
    ],
  ];
  $this
    ->addEffectToTestStyle($effect);

  // 1. Test blurring red on green.
  $original_uri = $this
    ->getTestImageCopyUri('/tests/images/red-on-green.png', 'image_effects');
  $derivative_uri = $this->testImageStyle
    ->buildUri($original_uri);

  // Check that ::applyEffect generates image with expected blur.
  $this->testImageStyle
    ->createDerivative($original_uri, $derivative_uri);
  $image = $this->imageFactory
    ->get($derivative_uri, 'gd');
  $this
    ->assertColorsAreEqual($this->green, $this
    ->getPixelColor($image, 0, 0));
  $this
    ->assertColorsAreEqual($this->red, $this
    ->getPixelColor($image, 50, 50));

  // The upper-left corner of the inner red square has been blurred.
  // For fully opaque, we check an actual color.
  $this
    ->assertColorsAreEqual([
    94,
    161,
    0,
    0,
  ], $this
    ->getPixelColor($image, 25, 25));

  // 2. Test blurring red on transparent.
  $original_uri = $this
    ->getTestImageCopyUri('/tests/images/red-on-transparent.png', 'image_effects');
  $derivative_uri = $this->testImageStyle
    ->buildUri($original_uri);

  // Check that ::applyEffect generates image with expected blur.
  $this->testImageStyle
    ->createDerivative($original_uri, $derivative_uri);
  $image = $this->imageFactory
    ->get($derivative_uri, 'gd');
  $this
    ->assertColorsAreEqual($this->transparent, $this
    ->getPixelColor($image, 0, 0));
  $this
    ->assertColorsAreEqual($this->red, $this
    ->getPixelColor($image, 50, 50));

  // The upper-left corner of the inner red square has been blurred.
  // For fully transparent, the background color differs by toolkit. In this
  // case, we just check for the alpha channel value equal to 80.
  $this
    ->assertEqual(80, imagecolorsforindex($image
    ->getToolkit()
    ->getResource(), imagecolorat($image
    ->getToolkit()
    ->getResource(), 25, 25))['alpha']);
}