3 namespace Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick;
6 * Defines imagemagick Scale operation.
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.")
16 class Scale extends Resize {
21 protected function arguments() {
24 'description' => 'The target width, in pixels. This value is omitted then the scaling will based only on the height value',
29 'description' => 'The target height, in pixels. This value is omitted then the scaling will based only on the width value',
34 'description' => 'Boolean indicating that files smaller than the dimensions will be scaled up. This generally results in a low quality image',
39 'description' => 'An optional filter to apply for the resize',
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");
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);
65 $arguments['width'] = (int) round($arguments['height'] / $aspect);
68 // Assure integers for all arguments.
69 $arguments['width'] = (int) round($arguments['width']);
70 $arguments['height'] = (int) round($arguments['height']);
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");
76 if ($arguments['height'] <= 0) {
77 throw new \InvalidArgumentException("Invalid height ('{$arguments['height']}') specified for the image 'scale' operation");
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);