View source
<?php
namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
use Drupal\Component\Utility\Color;
class Rotate extends GDImageToolkitOperationBase {
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,
],
];
}
protected function validateArguments(array $arguments) {
$arguments['degrees'] -= floor($arguments['degrees'] / 360) * 360;
if (!empty($arguments['background'])) {
$background = Color::hexToRgb($arguments['background']) + [
'alpha' => 0,
];
}
else {
$background = [
'red' => 255,
'green' => 255,
'blue' => 255,
'alpha' => 127,
];
}
$arguments['background_idx'] = imagecolorallocatealpha($this
->getToolkit()
->getResource(), $background['red'], $background['green'], $background['blue'], $background['alpha']);
if ($this
->getToolkit()
->getType() === IMAGETYPE_GIF) {
$gif_transparent_id = imagecolortransparent($this
->getToolkit()
->getResource());
if ($gif_transparent_id !== -1) {
$arguments['gif_transparent_color'] = imagecolorsforindex($this
->getToolkit()
->getResource(), $gif_transparent_id);
if ($background['alpha'] >= 127) {
$arguments['background_idx'] = $gif_transparent_id;
}
}
else {
if ($background['alpha'] >= 127) {
$arguments['gif_transparent_color'] = $background;
}
}
}
return $arguments;
}
protected function execute(array $arguments) {
if (!function_exists('imagerotate')) {
$this->logger
->notice('The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', [
'%file' => $this
->getToolkit()
->getSource(),
]);
return FALSE;
}
$original_res = $this
->getToolkit()
->getResource();
if ($new_res = imagerotate($this
->getToolkit()
->getResource(), 360 - $arguments['degrees'], $arguments['background_idx'])) {
$this
->getToolkit()
->setResource($new_res);
imagedestroy($original_res);
if (!empty($arguments['gif_transparent_color'])) {
$transparent_idx = imagecolorexactalpha($this
->getToolkit()
->getResource(), $arguments['gif_transparent_color']['red'], $arguments['gif_transparent_color']['green'], $arguments['gif_transparent_color']['blue'], $arguments['gif_transparent_color']['alpha']);
imagecolortransparent($this
->getToolkit()
->getResource(), $transparent_idx);
}
return TRUE;
}
return FALSE;
}
}