brandfolder-8.x-1.x-dev/src/Plugin/ImageToolkit/Operation/brandfolder/Resize.php
src/Plugin/ImageToolkit/Operation/brandfolder/Resize.php
<?php
namespace Drupal\brandfolder\Plugin\ImageToolkit\Operation\brandfolder;
/**
* Defines Brandfolder resize operation.
*
* @ImageToolkitOperation(
* id = "brandfolder_resize",
* toolkit = "brandfolder",
* operation = "resize",
* label = @Translation("Resize"),
* description = @Translation("Resizes an image to the given dimensions (ignoring aspect ratio).")
* )
*/
class Resize extends BrandfolderImageToolkitOperationBase {
/**
* {@inheritdoc}
*/
protected function arguments() {
return [
'width' => [
'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 = []) {
$new_width = $arguments['width'];
$new_height = $arguments['height'];
$params = [
'width' => $new_width,
'height' => $new_height,
];
$toolkit = $this->getToolkit();
$toolkit->setCdnUrlParams($params);
$toolkit->setFileDataItem('width', $new_width);
$toolkit->setFileDataItem('height', $new_height);
$toolkit->recordOperation("resize", $arguments);
return TRUE;
}
}
