d80f0b4aeabcf59604d5e2d5a0d52e5265628bba
[yaffs-website] / Scale.php
1 <?php
2
3 namespace Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick;
4
5 /**
6  * Defines imagemagick Scale operation.
7  *
8  * @ImageToolkitOperation(
9  *   id = "imagemagick_scale",
10  *   toolkit = "imagemagick",
11  *   operation = "scale",
12  *   label = @Translation("Scale"),
13  *   description = @Translation("Scales an image while maintaining aspect ratio. The resulting image can be smaller for one or both target dimensions.")
14  * )
15  */
16 class Scale extends Resize {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function arguments() {
22     return [
23       'width' => [
24         'description' => 'The target width, in pixels. This value is omitted then the scaling will based only on the height value',
25         'required' => FALSE,
26         'default' => NULL,
27       ],
28       'height' => [
29         'description' => 'The target height, in pixels. This value is omitted then the scaling will based only on the width value',
30         'required' => FALSE,
31         'default' => NULL,
32       ],
33       'upscale' => [
34         'description' => 'Boolean indicating that files smaller than the dimensions will be scaled up. This generally results in a low quality image',
35         'required' => FALSE,
36         'default' => FALSE,
37       ],
38       'filter' => [
39         'description' => 'An optional filter to apply for the resize',
40         'required' => FALSE,
41         'default' => '',
42       ],
43     ];
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function validateArguments(array $arguments) {
50     // Assure at least one dimension.
51     if (empty($arguments['width']) && empty($arguments['height'])) {
52       throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'scale' operation");
53     }
54
55     // Calculate one of the dimensions from the other target dimension,
56     // ensuring the same aspect ratio as the source dimensions. If one of the
57     // target dimensions is missing, that is the one that is calculated. If both
58     // are specified then the dimension calculated is the one that would not be
59     // calculated to be bigger than its target.
60     $aspect = $this->getToolkit()->getHeight() / $this->getToolkit()->getWidth();
61     if (($arguments['width'] && !$arguments['height']) || ($arguments['width'] && $arguments['height'] && $aspect < $arguments['height'] / $arguments['width'])) {
62       $arguments['height'] = (int) round($arguments['width'] * $aspect);
63     }
64     else {
65       $arguments['width'] = (int) round($arguments['height'] / $aspect);
66     }
67
68     // Assure integers for all arguments.
69     $arguments['width'] = (int) round($arguments['width']);
70     $arguments['height'] = (int) round($arguments['height']);
71
72     // Fail when width or height are 0 or negative.
73     if ($arguments['width'] <= 0) {
74       throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'scale' operation");
75     }
76     if ($arguments['height'] <= 0) {
77       throw new \InvalidArgumentException("Invalid height ('{$arguments['height']}') specified for the image 'scale' operation");
78     }
79
80     return $arguments;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   protected function execute(array $arguments = []) {
87     // Don't scale if we don't change the dimensions at all.
88     if ($arguments['width'] !== $this->getToolkit()->getWidth() || $arguments['height'] !== $this->getToolkit()->getHeight()) {
89       // Don't upscale if the option isn't enabled.
90       if ($arguments['upscale'] || ($arguments['width'] <= $this->getToolkit()->getWidth() && $arguments['height'] <= $this->getToolkit()->getHeight())) {
91         return parent::execute($arguments);
92       }
93     }
94     return TRUE;
95   }
96
97 }