Rotate.php in ImageMagick 8
File
src/Plugin/ImageToolkit/Operation/imagemagick/Rotate.php
View source
<?php
namespace Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick;
use Drupal\Component\Utility\Color;
use Drupal\Component\Utility\Rectangle;
class Rotate extends ImagemagickImageToolkitOperationBase {
protected function arguments() {
return [
'degrees' => [
'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' => '',
],
];
}
protected function validateArguments(array $arguments) {
if (!empty($arguments['background'])) {
if (!Color::validateHex($arguments['background'])) {
throw new \InvalidArgumentException("Invalid color '{$arguments['background']}' specified for the 'rotate' operation.");
}
}
else {
$arguments['background'] = 'transparent';
}
return $arguments;
}
protected function execute(array $arguments) {
$arg = '-background ' . $this
->getToolkit()
->escapeShellArg($arguments['background']);
$arg .= ' -rotate ' . $arguments['degrees'];
$arg .= ' +repage';
$this
->getToolkit()
->addArgument($arg);
$box = new Rectangle($this
->getToolkit()
->getWidth(), $this
->getToolkit()
->getHeight());
$box = $box
->rotate((double) $arguments['degrees']);
return $this
->getToolkit()
->apply('resize', [
'width' => $box
->getBoundingWidth(),
'height' => $box
->getBoundingHeight(),
'filter' => $arguments['resize_filter'],
]);
}
}
Classes
Name |
Description |
Rotate |
Defines imagemagick Rotate operation. |