function ToolkitGdTest::testCreateImageFromScratch

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::testCreateImageFromScratch()
  2. 11.x core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::testCreateImageFromScratch()

Tests creation of image from scratch, and saving to storage.

@dataProvider providerSupportedImageTypes

File

core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php, line 408

Class

ToolkitGdTest
Tests for the GD image toolkit.

Namespace

Drupal\KernelTests\Core\Image

Code

public function testCreateImageFromScratch(int $type) : void {
    // Build an image from scratch.
    $image = $this->imageFactory
        ->get();
    $image->createNew(50, 20, image_type_to_extension($type, FALSE), '#ffff00');
    $file = 'from_null' . image_type_to_extension($type);
    $file_path = $this->directory . '/' . $file;
    $this->assertSame(50, $image->getWidth());
    $this->assertSame(20, $image->getHeight());
    $this->assertSame(image_type_to_mime_type($type), $image->getMimeType());
    $this->assertTrue($image->save($file_path), "Image '{$file}' should have been saved successfully, but it has not.");
    // Reload and check saved image.
    $image_reloaded = $this->imageFactory
        ->get($file_path);
    $this->assertTrue($image_reloaded->isValid());
    $this->assertSame(50, $image_reloaded->getWidth());
    $this->assertSame(20, $image_reloaded->getHeight());
    $this->assertSame(image_type_to_mime_type($type), $image_reloaded->getMimeType());
    if ($image_reloaded->getToolkit()
        ->getType() == IMAGETYPE_GIF) {
        $this->assertSame('#ffff00', $image_reloaded->getToolkit()
            ->getTransparentColor(), "Image '{$file}' after reload should have color channel set to #ffff00, but it has not.");
    }
    else {
        $this->assertNull($image_reloaded->getToolkit()
            ->getTransparentColor(), "Image '{$file}' after reload should have no color channel set, but it has.");
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.