3 namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
6 * Defines GD2 resize operation.
8 * @ImageToolkitOperation(
11 * operation = "resize",
12 * label = @Translation("Resize"),
13 * description = @Translation("Resizes an image to the given dimensions (ignoring aspect ratio).")
16 class Resize extends GDImageToolkitOperationBase {
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',
35 protected function validateArguments(array $arguments) {
36 // Assure integers for all arguments.
37 $arguments['width'] = (int) round($arguments['width']);
38 $arguments['height'] = (int) round($arguments['height']);
40 // Fail when width or height are 0 or negative.
41 if ($arguments['width'] <= 0) {
42 throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'resize' operation");
44 if ($arguments['height'] <= 0) {
45 throw new \InvalidArgumentException("Invalid height ('{$arguments['height']}') specified for the image 'resize' operation");
54 protected function execute(array $arguments = []) {
55 // Create a new resource of the required dimensions, and copy and resize
56 // the original resource on it with resampling. Destroy the original
57 // resource upon success.
58 $original_resource = $this->getToolkit()->getResource();
60 'width' => $arguments['width'],
61 'height' => $arguments['height'],
62 'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE),
63 'transparent_color' => $this->getToolkit()->getTransparentColor(),
66 if ($this->getToolkit()->apply('create_new', $data)) {
67 if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, 0, 0, $arguments['width'], $arguments['height'], imagesx($original_resource), imagesy($original_resource))) {
68 imagedestroy($original_resource);
72 // In case of failure, destroy the temporary resource and restore
74 imagedestroy($this->getToolkit()->getResource());
75 $this->getToolkit()->setResource($original_resource);