function CreateNew::execute

Same name in other branches
  1. 9 core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew::execute()
  2. 8.9.x core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew::execute()
  3. 11.x core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew::execute()

Overrides ImageToolkitOperationBase::execute

File

core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php, line 82

Class

CreateNew
Defines GD2 create_new image operation.

Namespace

Drupal\system\Plugin\ImageToolkit\Operation\gd

Code

protected function execute(array $arguments) {
    // Get the image type.
    $type = $this->getToolkit()
        ->extensionToImageType($arguments['extension']);
    // Create the image.
    if (!($image = imagecreatetruecolor($arguments['width'], $arguments['height']))) {
        return FALSE;
    }
    // Fill the image with transparency as possible.
    switch ($type) {
        case IMAGETYPE_PNG:
        case IMAGETYPE_WEBP:
            imagealphablending($image, FALSE);
            $transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
            imagefill($image, 0, 0, $transparency);
            imagealphablending($image, TRUE);
            imagesavealpha($image, TRUE);
            break;
        case IMAGETYPE_GIF:
            if (empty($arguments['transparent_color'])) {
                // No transparency color specified, fill white transparent.
                $fill_color = imagecolorallocatealpha($image, 255, 255, 255, 127);
            }
            else {
                $fill_rgb = Color::hexToRgb($arguments['transparent_color']);
                $fill_color = imagecolorallocatealpha($image, $fill_rgb['red'], $fill_rgb['green'], $fill_rgb['blue'], 127);
                imagecolortransparent($image, $fill_color);
            }
            imagefill($image, 0, 0, $fill_color);
            break;
        case IMAGETYPE_JPEG:
            imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255));
            break;
    }
    // Update the toolkit properties.
    $this->getToolkit()
        ->setType($type);
    $this->getToolkit()
        ->setImage($image);
    return TRUE;
}

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