eab120ac89cb4652248c92f86fdd18548f84573d
[yaffs-website] / web / modules / contrib / imagemagick / src / Plugin / ImageToolkit / Operation / imagemagick / Rotate.php
1 <?php
2
3 namespace Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick;
4
5 use Drupal\Component\Utility\Color;
6 use Drupal\Component\Utility\Rectangle;
7
8 /**
9  * Defines imagemagick Rotate operation.
10  *
11  * @ImageToolkitOperation(
12  *   id = "imagemagick_rotate",
13  *   toolkit = "imagemagick",
14  *   operation = "rotate",
15  *   label = @Translation("Rotate"),
16  *   description = @Translation("Rotates an image by the given number of degrees.")
17  * )
18  */
19 class Rotate extends ImagemagickImageToolkitOperationBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function arguments() {
25     return [
26       'degrees' => [
27         'description' => 'The number of (clockwise) degrees to rotate the image',
28       ],
29       'background' => [
30         'description' => "A string specifying the hexadecimal color code to use as background for the uncovered area of the image after the rotation. E.g. '#000000' for black, '#ff00ff' for magenta, and '#ffffff' for white. For images that support transparency, this will default to transparent white",
31         'required' => FALSE,
32         'default' => NULL,
33       ],
34       'resize_filter' => [
35         'description' => 'An optional filter to apply for the resize',
36         'required' => FALSE,
37         'default' => '',
38       ],
39     ];
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function validateArguments(array $arguments) {
46     // Validate or set background color argument.
47     if (!empty($arguments['background'])) {
48       // Validate the background color.
49       if (!Color::validateHex($arguments['background'])) {
50         throw new \InvalidArgumentException("Invalid color '{$arguments['background']}' specified for the 'rotate' operation.");
51       }
52     }
53     else {
54       // Background color is not specified: use transparent.
55       $arguments['background'] = 'transparent';
56     }
57     return $arguments;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   protected function execute(array $arguments) {
64     // Rotate.
65     $arg = '-background ' . $this->escapeArgument($arguments['background']);
66     $arg .= ' -rotate ' . $arguments['degrees'];
67     $arg .= ' +repage';
68     $this->addArgument($arg);
69
70     // Need to resize the image after rotation to make sure it complies with
71     // the dimensions expected, calculated via the Rectangle class.
72     $box = new Rectangle($this->getToolkit()->getWidth(), $this->getToolkit()->getHeight());
73     $box = $box->rotate((float) $arguments['degrees']);
74     return $this->getToolkit()->apply('resize', [
75       'width' => $box->getBoundingWidth(),
76       'height' => $box->getBoundingHeight(),
77       'filter' => $arguments['resize_filter'],
78     ]);
79   }
80
81 }