getToolkit example

return $arguments;
  }

  /** * {@inheritdoc} */
  protected function execute(array $arguments = []) {
    // Create a new resource of the required dimensions, and copy and resize     // the original resource on it with resampling. Destroy the original     // resource upon success.     $original_resource = $this->getToolkit()->getResource();
    $data = [
      'width' => $arguments['width'],
      'height' => $arguments['height'],
      'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE),
      'transparent_color' => $this->getToolkit()->getTransparentColor(),
      'is_temp' => TRUE,
    ];
    if ($this->getToolkit()->apply('create_new', $data)) {
      if (imagecopyresampled($this->getToolkit()->getResource()$original_resource, 0, 0, 0, 0, $arguments['width']$arguments['height']imagesx($original_resource)imagesy($original_resource))) {
        imagedestroy($original_resource);
        return TRUE;
      }
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

  public function __construct(ImageToolkitInterface $toolkit$source = NULL) {
    $this->toolkit = $toolkit;
    if ($source) {
      $this->source = $source;
      $this->getToolkit()->setSource($this->source);
      // Defer image file validity check to the toolkit.       if ($this->getToolkit()->parseFile()) {
        $this->fileSize = filesize($this->source);
      }
    }
  }

  /** * {@inheritdoc} */
  public function isValid() {
    
    if ($actual[3] == 127 && $expected[3] == 127) {
      return;
    }
    $distance = pow(($actual[0] - $expected[0]), 2) + pow(($actual[1] - $expected[1]), 2) + pow(($actual[2] - $expected[2]), 2) + pow(($actual[3] - $expected[3]), 2);
    $this->assertLessThanOrEqual($tolerance$distance$message . " - Actual: {" . implode(',', $actual) . "}, Expected: {" . implode(',', $expected) . "}, Distance: " . $distance . ", Tolerance: " . $tolerance);
  }

  /** * Function for finding a pixel's RGBa values. */
  public function getPixelColor(ImageInterface $image, int $x, int $y): array {
    $toolkit = $image->getToolkit();
    $color_index = imagecolorat($toolkit->getResource()$x$y);

    $transparent_index = imagecolortransparent($toolkit->getResource());
    if ($color_index == $transparent_index) {
      return [0, 0, 0, 127];
    }

    return array_values(imagecolorsforindex($toolkit->getResource()$color_index));
  }

  /** * Data provider for ::testManipulations(). */
/** * {@inheritdoc} */
  protected function validateArguments(array $arguments) {
    // Assure at least one dimension.     if (empty($arguments['width']) && empty($arguments['height'])) {
      throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'crop' operation");
    }

    // Preserve aspect.     $aspect = $this->getToolkit()->getHeight() / $this->getToolkit()->getWidth();
    $arguments['height'] = empty($arguments['height']) ? $arguments['width'] * $aspect : $arguments['height'];
    $arguments['width'] = empty($arguments['width']) ? $arguments['height'] / $aspect : $arguments['width'];

    // Assure integers for all arguments.     foreach (['x', 'y', 'width', 'height'] as $key) {
      $arguments[$key] = (int) round($arguments[$key]);
    }

    // Fail when width or height are 0 or negative.     if ($arguments['width'] <= 0) {
      throw new \InvalidArgumentException("Invalid width ('{$arguments['width']}') specified for the image 'crop' operation");
    }
use Drupal\Core\ImageToolkit\ImageToolkitOperationBase;

abstract class GDImageToolkitOperationBase extends ImageToolkitOperationBase {

  /** * The correctly typed image toolkit for GD operations. * * @return \Drupal\system\Plugin\ImageToolkit\GDToolkit */
  protected function getToolkit() {
    return parent::getToolkit();
  }

}
protected function validateArguments(array $arguments) {
    // Assure at least one dimension.     if (empty($arguments['width']) && empty($arguments['height'])) {
      throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'scale' operation");
    }

    // Calculate one of the dimensions from the other target dimension,     // ensuring the same aspect ratio as the source dimensions. If one of the     // target dimensions is missing, that is the one that is calculated. If both     // are specified then the dimension calculated is the one that would not be     // calculated to be bigger than its target.     $aspect = $this->getToolkit()->getHeight() / $this->getToolkit()->getWidth();
    if (($arguments['width'] && !$arguments['height']) || ($arguments['width'] && $arguments['height'] && $aspect < $arguments['height'] / $arguments['width'])) {
      $arguments['height'] = (int) round($arguments['width'] * $aspect);
    }
    else {
      $arguments['width'] = (int) round($arguments['height'] / $aspect);
    }

    // Assure integers for all arguments.     $arguments['width'] = (int) round($arguments['width']);
    $arguments['height'] = (int) round($arguments['height']);

    

  public function testGetFileSize() {
    $this->getTestImage(FALSE);
    $this->assertEquals(3905, $this->image->getFileSize());
  }

  /** * Tests \Drupal\Core\Image\Image::getToolkit()->getType(). */
  public function testGetType() {
    $this->getTestImage(FALSE);
    $this->assertEquals(IMAGETYPE_PNG, $this->image->getToolkit()->getType());
  }

  /** * Tests \Drupal\Core\Image\Image::getMimeType(). */
  public function testGetMimeType() {
    $this->getTestImage(FALSE);
    $this->assertEquals('image/png', $this->image->getMimeType());
  }

  /** * Tests \Drupal\Core\Image\Image::isValid(). */

      'height' => [
        'description' => 'The target height, in pixels',
      ],
    ];
  }

  /** * {@inheritdoc} */
  protected function validateArguments(array $arguments) {
    $actualWidth = $this->getToolkit()->getWidth();
    $actualHeight = $this->getToolkit()->getHeight();

    $scaleFactor = max($arguments['width'] / $actualWidth$arguments['height'] / $actualHeight);

    $arguments['x'] = isset($arguments['x']) ?
      (int) round($arguments['x']) :
      (int) round(($actualWidth * $scaleFactor - $arguments['width']) / 2);
    $arguments['y'] = isset($arguments['y']) ?
      (int) round($arguments['y']) :
      (int) round(($actualHeight * $scaleFactor - $arguments['height']) / 2);
    $arguments['resize'] = [
      
'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");
    }
    
protected function arguments() {
    // This operation does not use any parameters.     return [];
  }

  /** * {@inheritdoc} */
  protected function execute(array $arguments) {
    // PHP installations using non-bundled GD do not have imagefilter.     if (!function_exists('imagefilter')) {
      $this->logger->notice("The image '@file' could not be desaturated because the imagefilter() function is not available in this PHP installation.", ['@file' => $this->getToolkit()->getSource()]);
      return FALSE;
    }

    return imagefilter($this->getToolkit()->getResource(), IMG_FILTER_GRAYSCALE);
  }

}
// Validate or set background color argument.     if (!empty($arguments['background'])) {
      // Validate the background color: Color::hexToRgb does so for us.       $background = Color::hexToRgb($arguments['background']) + ['alpha' => 0];
    }
    else {
      // Background color is not specified: use transparent white as background.       $background = ['red' => 255, 'green' => 255, 'blue' => 255, 'alpha' => 127];
    }
    // Store the color index for the background as that is what GD uses.     $arguments['background_idx'] = imagecolorallocatealpha($this->getToolkit()->getResource()$background['red']$background['green']$background['blue']$background['alpha']);

    if ($this->getToolkit()->getType() === IMAGETYPE_GIF) {
      // GIF does not work with a transparency channel, but can define 1 color       // in its palette to act as transparent.
      // Get the current transparent color, if any.       $gif_transparent_id = imagecolortransparent($this->getToolkit()->getResource());
      if ($gif_transparent_id !== -1) {
        // The gif already has a transparent color set: remember it to set it on         // the rotated image as well.         $arguments['gif_transparent_color'] = imagecolorsforindex($this->getToolkit()->getResource()$gif_transparent_id);

        
Home | Imprint | This part of the site doesn't use cookies.