View source
<?php
namespace Drupal\imagick\Plugin\ImageToolkit\Operation\imagick;
use Imagick;
use ImagickPixel;
use ImagickDraw;
class RoundedCorners extends ImagickOperationBase {
protected function arguments() {
return [
'x_rounding' => [
'description' => 'The x rounding of the corners.',
],
'y_rounding' => [
'description' => 'The y rounding of the corners.',
],
'stroke_width' => [
'description' => 'The stroke width of the corners.',
],
'displace' => [
'description' => 'The displace of the corners.',
],
'size_correction' => [
'description' => 'The size correction of the corners.',
],
];
}
protected function process(Imagick $resource, array $arguments) {
if (method_exists($resource, 'roundCorners')) {
return $resource
->roundCorners($arguments['x_rounding'], $arguments['y_rounding'], $arguments['stroke_width'], $arguments['displace'], $arguments['size_correction']);
}
else {
$dimensions = $resource
->getImageGeometry();
$shape = new ImagickDraw();
$shape
->setFillColor(new ImagickPixel('black'));
$shape
->roundRectangle(0, 0, $dimensions['width'], $dimensions['height'], $arguments['x_rounding'], $arguments['y_rounding']);
$mask = new Imagick();
$mask
->newImage($dimensions['width'], $dimensions['height'], new ImagickPixel('transparent'), 'png');
$mask
->drawImage($shape);
return $resource
->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0);
}
}
}