3 namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
5 use Drupal\Component\Utility\Color;
8 * Defines GD2 create_new image operation.
10 * @ImageToolkitOperation(
11 * id = "gd_create_new",
13 * operation = "create_new",
14 * label = @Translation("Set a new image"),
15 * description = @Translation("Creates a new transparent resource and sets it for the image.")
18 class CreateNew extends GDImageToolkitOperationBase {
23 protected function arguments() {
26 'description' => 'The width of the image, in pixels',
29 'description' => 'The height of the image, in pixels',
32 'description' => 'The extension of the image file (e.g. png, gif, etc.)',
36 'transparent_color' => [
37 'description' => 'The RGB hex color for GIF transparency',
39 'default' => '#ffffff',
42 'description' => 'If TRUE, this operation is being used to create a temporary image by another GD operation. After performing its function, the caller is responsible for destroying the original GD resource.',
52 protected function validateArguments(array $arguments) {
53 // Assure extension is supported.
54 if (!in_array($arguments['extension'], $this->getToolkit()->getSupportedExtensions())) {
55 throw new \InvalidArgumentException("Invalid extension ('{$arguments['extension']}') specified for the image 'convert' operation");
58 // Assure integers for width and height.
59 $arguments['width'] = (int) round($arguments['width']);
60 $arguments['height'] = (int) round($arguments['height']);
62 // Fail when width or height are 0 or negative.
63 if ($arguments['width'] <= 0) {
64 throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'create_new' operation");
66 if ($arguments['height'] <= 0) {
67 throw new \InvalidArgumentException("Invalid height ({$arguments['height']}) specified for the image 'create_new' operation");
70 // Assure transparent color is a valid hex string.
71 if ($arguments['transparent_color'] && !Color::validateHex($arguments['transparent_color'])) {
72 throw new \InvalidArgumentException("Invalid transparent color ({$arguments['transparent_color']}) specified for the image 'create_new' operation");
81 protected function execute(array $arguments) {
82 // Get the image type.
83 $type = $this->getToolkit()->extensionToImageType($arguments['extension']);
85 // Store the original GD resource.
86 $original_res = $this->getToolkit()->getResource();
88 // Create the resource.
89 if (!$res = imagecreatetruecolor($arguments['width'], $arguments['height'])) {
93 // Fill the resource with transparency as possible.
96 imagealphablending($res, FALSE);
97 $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
98 imagefill($res, 0, 0, $transparency);
99 imagealphablending($res, TRUE);
100 imagesavealpha($res, TRUE);
104 if (empty($arguments['transparent_color'])) {
105 // No transparency color specified, fill white transparent.
106 $fill_color = imagecolorallocatealpha($res, 255, 255, 255, 127);
109 $fill_rgb = Color::hexToRgb($arguments['transparent_color']);
110 $fill_color = imagecolorallocatealpha($res, $fill_rgb['red'], $fill_rgb['green'], $fill_rgb['blue'], 127);
111 imagecolortransparent($res, $fill_color);
113 imagefill($res, 0, 0, $fill_color);
117 imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
122 // Update the toolkit properties.
123 $this->getToolkit()->setType($type);
124 $this->getToolkit()->setResource($res);
126 // Destroy the original resource if it is not needed by other operations.
127 if (!$arguments['is_temp'] && is_resource($original_res)) {
128 imagedestroy($original_res);