Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Convert.php

class Convert

Defines GD2 convert operation.

Hierarchy

  • class \Drupal\Component\Plugin\PluginBase implements \Drupal\Component\Plugin\PluginInspectionInterface, \Drupal\Component\Plugin\DerivativeInspectionInterface
    • class \Drupal\Core\Plugin\PluginBase extends \Drupal\Component\Plugin\PluginBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Messenger\MessengerTrait
      • class \Drupal\Core\ImageToolkit\ImageToolkitOperationBase extends \Drupal\Core\Plugin\PluginBase implements \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
        • class \Drupal\system\Plugin\ImageToolkit\Operation\gd\GDImageToolkitOperationBase extends \Drupal\Core\ImageToolkit\ImageToolkitOperationBase
          • class \Drupal\system\Plugin\ImageToolkit\Operation\gd\Convert extends \Drupal\system\Plugin\ImageToolkit\Operation\gd\GDImageToolkitOperationBase

Expanded class hierarchy of Convert

1 string reference to 'Convert'
image.schema.yml in core/modules/image/config/schema/image.schema.yml
core/modules/image/config/schema/image.schema.yml

File

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

Namespace

Drupal\system\Plugin\ImageToolkit\Operation\gd
View source
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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Convert::arguments protected function Returns the definition of the operation arguments. Overrides ImageToolkitOperationBase::arguments
Convert::execute protected function Performs the actual manipulation on the image. Overrides ImageToolkitOperationBase::execute
Convert::validateArguments protected function Validates the arguments. Overrides ImageToolkitOperationBase::validateArguments
GDImageToolkitOperationBase::getToolkit protected function The correctly typed image toolkit for GD operations. Overrides ImageToolkitOperationBase::getToolkit
ImageToolkitOperationBase::$logger protected property A logger instance.
ImageToolkitOperationBase::$toolkit protected property The image toolkit.
ImageToolkitOperationBase::apply final public function Applies a toolkit specific operation to an image. Overrides ImageToolkitOperationInterface::apply
ImageToolkitOperationBase::prepareArguments protected function Checks for required arguments and adds optional argument defaults.
ImageToolkitOperationBase::__construct public function Constructs an image toolkit operation plugin.
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 5
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 2

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal