You are here

protected function CreateNew::execute in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 86
Contains \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew.

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:
      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.
        $fill_color = imagecolorallocate($res, 255, 255, 255);
      }
      else {
        $fill_rgb = Color::hexToRgb($arguments['transparent_color']);
        $fill_color = imagecolorallocate($res, $fill_rgb['red'], $fill_rgb['green'], $fill_rgb['blue']);
        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;
}