function CreateNew::execute

Same name and namespace 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()

Performs the actual manipulation on the image.

Image toolkit operation implementers must implement this method. This method is responsible for actually performing the operation on the image. When this method gets called, the implementer may assume all arguments, also the optional ones, to be available, validated and corrected.

Parameters

array $arguments: An associative array of arguments to be used by the toolkit operation.

Return value

bool TRUE if the operation was performed successfully, FALSE otherwise.

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.