function CreateNew::execute

Same name and namespace in other branches
  1. 8.9.x core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew::execute()
  2. 10 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 81

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']);
    // Store the original GD resource.
    $original_res = $this->getToolkit()
        ->getResource();
    // Create the resource.
    if (!($res = imagecreatetruecolor($arguments['width'], $arguments['height']))) {
        return FALSE;
    }
    // Fill the resource with transparency as possible.
    switch ($type) {
        case IMAGETYPE_PNG:
        case IMAGETYPE_WEBP:
            imagealphablending($res, FALSE);
            $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
            imagefill($res, 0, 0, $transparency);
            imagealphablending($res, TRUE);
            imagesavealpha($res, TRUE);
            break;
        case IMAGETYPE_GIF:
            if (empty($arguments['transparent_color'])) {
                // No transparency color specified, fill white transparent.
                $fill_color = imagecolorallocatealpha($res, 255, 255, 255, 127);
            }
            else {
                $fill_rgb = Color::hexToRgb($arguments['transparent_color']);
                $fill_color = imagecolorallocatealpha($res, $fill_rgb['red'], $fill_rgb['green'], $fill_rgb['blue'], 127);
                imagecolortransparent($res, $fill_color);
            }
            imagefill($res, 0, 0, $fill_color);
            break;
        case IMAGETYPE_JPEG:
            imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
            break;
    }
    // Update the toolkit properties.
    $this->getToolkit()
        ->setType($type);
    $this->getToolkit()
        ->setResource($res);
    // Destroy the original resource if it is not needed by other operations.
    if (!$arguments['is_temp'] && is_resource($original_res)) {
        imagedestroy($original_res);
    }
    return TRUE;
}

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