e1c25831d2ea48e2351ed22ddbb65d04ce06e288
[yaffs-website] / web / modules / contrib / imagemagick / src / Plugin / ImageToolkit / Operation / imagemagick / Resize.php
1 <?php
2
3 namespace Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick;
4
5 /**
6  * Defines imagemagick resize operation.
7  *
8  * @ImageToolkitOperation(
9  *   id = "imagemagick_resize",
10  *   toolkit = "imagemagick",
11  *   operation = "resize",
12  *   label = @Translation("Resize"),
13  *   description = @Translation("Resizes an image to the given dimensions (ignoring aspect ratio).")
14  * )
15  */
16 class Resize extends ImagemagickImageToolkitOperationBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function arguments() {
22     return [
23       'width' => [
24         'description' => 'The new width of the resized image, in pixels',
25       ],
26       'height' => [
27         'description' => 'The new height of the resized image, in pixels',
28       ],
29       'filter' => [
30         'description' => 'An optional filter to apply for the resize',
31         'required' => FALSE,
32         'default' => '',
33       ],
34     ];
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function validateArguments(array $arguments) {
41     // Assure integers for all arguments.
42     $arguments['width'] = (int) round($arguments['width']);
43     $arguments['height'] = (int) round($arguments['height']);
44
45     // Fail when width or height are 0 or negative.
46     if ($arguments['width'] <= 0) {
47       throw new \InvalidArgumentException("Invalid width ({$arguments['width']}) specified for the image 'resize' operation");
48     }
49     if ($arguments['height'] <= 0) {
50       throw new \InvalidArgumentException("Invalid height ({$arguments['height']}) specified for the image 'resize' operation");
51     }
52
53     return $arguments;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function execute(array $arguments = []) {
60     if (!empty($arguments['filter'])) {
61       $this->getToolkit()->addArgument('-filter ' . $arguments['filter']);
62     }
63     $this->getToolkit()->addArgument('-resize ' . $arguments['width'] . 'x' . $arguments['height'] . '!');
64     $this->getToolkit()->setWidth($arguments['width'])->setHeight($arguments['height']);
65     return TRUE;
66   }
67
68 }