X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fsystem%2Fsrc%2FPlugin%2FImageToolkit%2FOperation%2Fgd%2FResize.php;fp=web%2Fcore%2Fmodules%2Fsystem%2Fsrc%2FPlugin%2FImageToolkit%2FOperation%2Fgd%2FResize.php;h=155bef24641d758b27f3ebeeb04673f6dad29ceb;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php b/web/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php new file mode 100644 index 000000000..155bef246 --- /dev/null +++ b/web/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php @@ -0,0 +1,81 @@ + [ + 'description' => 'The new width of the resized image, in pixels', + ], + 'height' => [ + 'description' => 'The new height of the resized image, in pixels', + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function validateArguments(array $arguments) { + // Assure integers for all arguments. + $arguments['width'] = (int) round($arguments['width']); + $arguments['height'] = (int) round($arguments['height']); + + // Fail when width or height are 0 or negative. + if ($arguments['width'] <= 0) { + throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'resize' operation"); + } + if ($arguments['height'] <= 0) { + throw new \InvalidArgumentException("Invalid height ('{$arguments['height']}') specified for the image 'resize' operation"); + } + + return $arguments; + } + + /** + * {@inheritdoc} + */ + protected function execute(array $arguments = []) { + // Create a new resource of the required dimensions, and copy and resize + // the original resource on it with resampling. Destroy the original + // resource upon success. + $original_resource = $this->getToolkit()->getResource(); + $data = [ + 'width' => $arguments['width'], + 'height' => $arguments['height'], + 'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE), + 'transparent_color' => $this->getToolkit()->getTransparentColor(), + 'is_temp' => TRUE, + ]; + if ($this->getToolkit()->apply('create_new', $data)) { + if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, 0, 0, $arguments['width'], $arguments['height'], imagesx($original_resource), imagesy($original_resource))) { + imagedestroy($original_resource); + return TRUE; + } + else { + // In case of failure, destroy the temporary resource and restore + // the original one. + imagedestroy($this->getToolkit()->getResource()); + $this->getToolkit()->setResource($original_resource); + } + } + return FALSE; + } + +}