Version 1
[yaffs-website] / web / core / modules / system / src / Plugin / ImageToolkit / Operation / gd / CreateNew.php
1 <?php
2
3 namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
4
5 use Drupal\Component\Utility\Color;
6
7 /**
8  * Defines GD2 create_new image operation.
9  *
10  * @ImageToolkitOperation(
11  *   id = "gd_create_new",
12  *   toolkit = "gd",
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.")
16  * )
17  */
18 class CreateNew extends GDImageToolkitOperationBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function arguments() {
24     return [
25       'width' => [
26         'description' => 'The width of the image, in pixels',
27       ],
28       'height' => [
29         'description' => 'The height of the image, in pixels',
30       ],
31       'extension' => [
32         'description' => 'The extension of the image file (e.g. png, gif, etc.)',
33         'required' => FALSE,
34         'default' => 'png',
35       ],
36       'transparent_color' => [
37         'description' => 'The RGB hex color for GIF transparency',
38         'required' => FALSE,
39         'default' => '#ffffff',
40       ],
41       'is_temp' => [
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.',
43         'required' => FALSE,
44         'default' => FALSE,
45       ],
46     ];
47   }
48
49   /**
50    * {@inheritdoc}
51    */
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");
56     }
57
58     // Assure integers for width and height.
59     $arguments['width'] = (int) round($arguments['width']);
60     $arguments['height'] = (int) round($arguments['height']);
61
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");
65     }
66     if ($arguments['height'] <= 0) {
67       throw new \InvalidArgumentException("Invalid height ({$arguments['height']}) specified for the image 'create_new' operation");
68     }
69
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");
73     }
74
75     return $arguments;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   protected function execute(array $arguments) {
82     // Get the image type.
83     $type = $this->getToolkit()->extensionToImageType($arguments['extension']);
84
85     // Store the original GD resource.
86     $original_res = $this->getToolkit()->getResource();
87
88     // Create the resource.
89     if (!$res = imagecreatetruecolor($arguments['width'], $arguments['height'])) {
90       return FALSE;
91     }
92
93     // Fill the resource with transparency as possible.
94     switch ($type) {
95       case IMAGETYPE_PNG:
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);
101         break;
102
103       case IMAGETYPE_GIF:
104         if (empty($arguments['transparent_color'])) {
105           // No transparency color specified, fill white transparent.
106           $fill_color = imagecolorallocatealpha($res, 255, 255, 255, 127);
107         }
108         else {
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);
112         }
113         imagefill($res, 0, 0, $fill_color);
114         break;
115
116       case IMAGETYPE_JPEG:
117         imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
118         break;
119
120     }
121
122     // Update the toolkit properties.
123     $this->getToolkit()->setType($type);
124     $this->getToolkit()->setResource($res);
125
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);
129     }
130
131     return TRUE;
132   }
133
134 }