X-Git-Url: https://yaffs.net/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fimagemagick%2Fsrc%2FPlugin%2FImageToolkit%2FOperation%2Fimagemagick%2FRotate.php;fp=web%2Fmodules%2Fcontrib%2Fimagemagick%2Fsrc%2FPlugin%2FImageToolkit%2FOperation%2Fimagemagick%2FRotate.php;h=f54eeacabcdf08f9ebebd2b8b7b6c72a9503b602;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/imagemagick/src/Plugin/ImageToolkit/Operation/imagemagick/Rotate.php b/web/modules/contrib/imagemagick/src/Plugin/ImageToolkit/Operation/imagemagick/Rotate.php new file mode 100644 index 000000000..f54eeacab --- /dev/null +++ b/web/modules/contrib/imagemagick/src/Plugin/ImageToolkit/Operation/imagemagick/Rotate.php @@ -0,0 +1,76 @@ + [ + 'description' => 'The number of (clockwise) degrees to rotate the image', + ], + 'background' => [ + 'description' => "A string specifying the hexadecimal color code to use as background for the uncovered area of the image after the rotation. E.g. '#000000' for black, '#ff00ff' for magenta, and '#ffffff' for white. For images that support transparency, this will default to transparent white", + 'required' => FALSE, + 'default' => NULL, + ], + 'resize_filter' => [ + 'description' => 'An optional filter to apply for the resize', + 'required' => FALSE, + 'default' => '', + ], + ]; + } + + /** + * {@inheritdoc} + */ + protected function validateArguments(array $arguments) { + // Validate or set background color argument. + if (!empty($arguments['background'])) { + // Validate the background color. + if (!Color::validateHex($arguments['background'])) { + throw new \InvalidArgumentException("Invalid color '{$arguments['background']}' specified for the 'rotate' operation."); + } + } + else { + // Background color is not specified: use transparent. + $arguments['background'] = 'transparent'; + } + return $arguments; + } + + /** + * {@inheritdoc} + */ + protected function execute(array $arguments) { + // Rotate. + $arg = '-background ' . $this->getToolkit()->escapeShellArg($arguments['background']); + $arg .= ' -rotate ' . $arguments['degrees']; + $arg .= ' +repage'; + $this->getToolkit()->addArgument($arg); + + // Need to resize the image after rotation to make sure it complies with + // the dimensions expected, calculated via the Rectangle class. + $box = new Rectangle($this->getToolkit()->getWidth(), $this->getToolkit()->getHeight()); + $box = $box->rotate((float) $arguments['degrees']); + return $this->getToolkit()->apply('resize', ['width' => $box->getBoundingWidth(), 'height' => $box->getBoundingHeight(), 'filter' => $arguments['resize_filter']]); + } +}