Convert.php

Same filename in other branches
  1. 9 core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php
  2. 8.9.x core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php
  3. 10 core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php

Namespace

Drupal\system\Plugin\ImageToolkit\Operation\gd

File

core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php

View source
<?php

namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;

use Drupal\Core\ImageToolkit\Attribute\ImageToolkitOperation;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines GD2 convert operation.
 */
class Convert extends GDImageToolkitOperationBase {
    
    /**
     * {@inheritdoc}
     */
    protected function arguments() {
        return [
            'extension' => [
                'description' => 'The new extension of the converted image',
            ],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    protected function validateArguments(array $arguments) {
        if (!in_array($arguments['extension'], $this->getToolkit()
            ->getSupportedExtensions())) {
            throw new \InvalidArgumentException("Invalid extension ({$arguments['extension']}) specified for the image 'convert' operation");
        }
        return $arguments;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function execute(array $arguments) {
        // Create a new image of the required dimensions and format, and copy
        // the original image on it with resampling. Restore the original image upon
        // failure.
        $width = $this->getToolkit()
            ->getWidth();
        $height = $this->getToolkit()
            ->getHeight();
        $original_image = $this->getToolkit()
            ->getImage();
        $original_type = $this->getToolkit()
            ->getType();
        $data = [
            'width' => $width,
            'height' => $height,
            'extension' => $arguments['extension'],
            'transparent_color' => $this->getToolkit()
                ->getTransparentColor(),
            'is_temp' => TRUE,
        ];
        if ($this->getToolkit()
            ->apply('create_new', $data)) {
            if (imagecopyresampled($this->getToolkit()
                ->getImage(), $original_image, 0, 0, 0, 0, $width, $height, $width, $height)) {
                return TRUE;
            }
            // In case of error, reset image and type to as it was.
            $this->getToolkit()
                ->setImage($original_image);
            $this->getToolkit()
                ->setType($original_type);
        }
        return FALSE;
    }

}

Classes

Title Deprecated Summary
Convert Defines GD2 convert operation.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.