3 namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
6 * Defines GD2 convert operation.
8 * @ImageToolkitOperation(
11 * operation = "convert",
12 * label = @Translation("Convert"),
13 * description = @Translation("Instructs the toolkit to save the image with a specified extension.")
16 class Convert extends GDImageToolkitOperationBase {
21 protected function arguments() {
24 'description' => 'The new extension of the converted image',
32 protected function validateArguments(array $arguments) {
33 if (!in_array($arguments['extension'], $this->getToolkit()->getSupportedExtensions())) {
34 throw new \InvalidArgumentException("Invalid extension ({$arguments['extension']}) specified for the image 'convert' operation");
42 protected function execute(array $arguments) {
43 // Create a new resource of the required dimensions and format, and copy
44 // the original resource on it with resampling. Destroy the original
45 // resource upon success.
46 $width = $this->getToolkit()->getWidth();
47 $height = $this->getToolkit()->getHeight();
48 $original_resource = $this->getToolkit()->getResource();
49 $original_type = $this->getToolkit()->getType();
53 'extension' => $arguments['extension'],
54 'transparent_color' => $this->getToolkit()->getTransparentColor(),
57 if ($this->getToolkit()->apply('create_new', $data)) {
58 if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, 0, 0, $width, $height, $width, $height)) {
59 imagedestroy($original_resource);
62 // In case of error, reset resource and type to as it was.
63 $this->getToolkit()->setResource($original_resource);
64 $this->getToolkit()->setType($original_type);