1b0c087b7735407e6b707559b680334b5cf575cd
[yaffs-website] / web / modules / contrib / imagemagick / src / Plugin / ImageToolkit / Operation / imagemagick / CreateNew.php
1 <?php
2
3 namespace Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick;
4
5 use Drupal\Component\Utility\Color;
6
7 /**
8  * Defines imagemagick CreateNew operation.
9  *
10  * @ImageToolkitOperation(
11  *   id = "imagemagick_create_new",
12  *   toolkit = "imagemagick",
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 ImagemagickImageToolkitOperationBase {
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     ];
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function validateArguments(array $arguments) {
48     // Assure extension is supported.
49     if (!in_array($arguments['extension'], $this->getToolkit()->getSupportedExtensions())) {
50       throw new \InvalidArgumentException("Invalid extension ('{$arguments['extension']}') specified for the image 'create_new' operation");
51     }
52
53     // Assure integers for width and height.
54     $arguments['width'] = (int) round($arguments['width']);
55     $arguments['height'] = (int) round($arguments['height']);
56
57     // Fail when width or height are 0 or negative.
58     if ($arguments['width'] <= 0) {
59       throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'create_new' operation");
60     }
61     if ($arguments['height'] <= 0) {
62       throw new \InvalidArgumentException("Invalid height ({$arguments['height']}) specified for the image 'create_new' operation");
63     }
64
65     // Assure transparent color is a valid hex string.
66     if ($arguments['transparent_color'] && !Color::validateHex($arguments['transparent_color'])) {
67       throw new \InvalidArgumentException("Invalid transparent color ({$arguments['transparent_color']}) specified for the image 'create_new' operation");
68     }
69
70     return $arguments;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   protected function execute(array $arguments) {
77     $this->getToolkit()
78       ->resetArguments()
79       ->setSourceLocalPath('')
80       ->setSourceFormatFromExtension($arguments['extension'])
81       ->setWidth($arguments['width'])
82       ->setHeight($arguments['height'])
83       ->setExifOrientation(NULL)
84       ->setFrames(NULL);
85     $arg = '-size ' . $arguments['width'] . 'x' . $arguments['height'];
86
87     // Transparent color syntax for GIF files differs by package.
88     if ($arguments['extension'] === 'gif') {
89       switch ($this->getToolkit()->getPackage()) {
90         case 'imagemagick':
91           $arg .= ' xc:transparent -transparent-color ' . $this->getToolkit()->escapeShellArg($arguments['transparent_color']);
92           break;
93
94         case 'graphicsmagick':
95           $arg .= ' xc:' . $this->getToolkit()->escapeShellArg($arguments['transparent_color']) . ' -transparent ' . $this->getToolkit()->escapeShellArg($arguments['transparent_color']);
96           break;
97
98       }
99     }
100     else {
101       $arg .= ' xc:transparent';
102     }
103
104     $this->getToolkit()->addArgument($arg);
105     return TRUE;
106   }
107
108 }