3 namespace Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick;
6 * Defines imagemagick resize operation.
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).")
16 class Resize extends ImagemagickImageToolkitOperationBase {
21 protected function arguments() {
24 'description' => 'The new width of the resized image, in pixels',
27 'description' => 'The new height of the resized image, in pixels',
30 'description' => 'An optional filter to apply for the resize',
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']);
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");
49 if ($arguments['height'] <= 0) {
50 throw new \InvalidArgumentException("Invalid height ({$arguments['height']}) specified for the image 'resize' operation");
59 protected function execute(array $arguments = []) {
60 if (!empty($arguments['filter'])) {
61 $this->getToolkit()->addArgument('-filter ' . $arguments['filter']);
63 $this->getToolkit()->addArgument('-resize ' . $arguments['width'] . 'x' . $arguments['height'] . '!');
64 $this->getToolkit()->setWidth($arguments['width'])->setHeight($arguments['height']);