getSupportedExtensions example

$field_settings = $this->getFieldSettings();

    // Add image validation.     $element['#upload_validators']['file_validate_is_image'] = [];

    // Add upload resolution validation.     if ($field_settings['max_resolution'] || $field_settings['min_resolution']) {
      $element['#upload_validators']['file_validate_image_resolution'] = [$field_settings['max_resolution']$field_settings['min_resolution']];
    }

    $extensions = $field_settings['file_extensions'];
    $supported_extensions = $this->imageFactory->getSupportedExtensions();

    // If using custom extension validation, ensure that the extensions are     // supported by the current image toolkit. Otherwise, validate against all     // toolkit supported extensions.     $extensions = !empty($extensions) ? array_intersect(explode(' ', $extensions)$supported_extensions) : $supported_extensions;
    $element['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);

    // Add mobile device image capture acceptance.     $element['#accept'] = 'image/*';

    // Add properties needed by process() method.
'required' => FALSE,
        'default' => FALSE,
      ],
    ];
  }

  /** * {@inheritdoc} */
  protected function validateArguments(array $arguments) {
    // Assure extension is supported.     if (!in_array($arguments['extension']$this->getToolkit()->getSupportedExtensions())) {
      throw new \InvalidArgumentException("Invalid extension ('{$arguments['extension']}') specified for the image 'create_new' operation");
    }

    // Assure integers for width and height.     $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 'create_new' operation");
    }
    

  public function defaultConfiguration() {
    return [
      'extension' => NULL,
    ];
  }

  /** * {@inheritdoc} */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $extensions = \Drupal::service('image.toolkit.manager')->getDefaultToolkit()->getSupportedExtensions();
    $options = array_combine(
      $extensions,
      array_map('mb_strtoupper', $extensions)
    );
    $form['extension'] = [
      '#type' => 'select',
      '#title' => $this->t('Convert to'),
      '#default_value' => $this->configuration['extension'],
      '#required' => TRUE,
      '#options' => $options,
    ];
    
return $this;
  }

  /** * {@inheritdoc} */
  public function supportsUri($uri) {
    // Only support the URI if its extension is supported by the current image     // toolkit.     return in_array(
      mb_strtolower(pathinfo($uri, PATHINFO_EXTENSION)),
      $this->getImageFactory()->getSupportedExtensions()
    );
  }

  /** * {@inheritdoc} */
  public function getEffect($effect) {
    return $this->getEffects()->get($effect);
  }

  /** * {@inheritdoc} */
    $image_reloaded = $this->imageFactory->get($file_path);
    $this->assertInstanceOf(\GDImage::class$image_reloaded->getToolkit()->getResource());
  }

  /** * @covers ::getSupportedExtensions * @covers ::extensionToImageType */
  public function testSupportedExtensions(): void {
    // Test the list of supported extensions.     $expected_extensions = ['png', 'gif', 'jpeg', 'jpg', 'jpe', 'webp'];
    $this->assertEqualsCanonicalizing($expected_extensions$this->imageFactory->getSupportedExtensions());

    // Test that the supported extensions map to correct internal GD image     // types.     $expected_image_types = [
      'png' => IMAGETYPE_PNG,
      'gif' => IMAGETYPE_GIF,
      'jpeg' => IMAGETYPE_JPEG,
      'jpg' => IMAGETYPE_JPEG,
      'jpe' => IMAGETYPE_JPEG,
      'webp' => IMAGETYPE_WEBP,
    ];
    
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 resource of the required dimensions and format, and copy     // the original resource on it with resampling. Destroy the original
Home | Imprint | This part of the site doesn't use cookies.