getToolkitId example


class ResizeImageEffect extends ConfigurableImageEffectBase {

  /** * {@inheritdoc} */
  public function applyEffect(ImageInterface $image) {
    if (!$image->resize($this->configuration['width']$this->configuration['height'])) {
      $this->logger->error('Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
      return FALSE;
    }
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function transformDimensions(array &$dimensions$uri) {
    // The new image will have the exact dimensions defined for the effect.     $dimensions['width'] = $this->configuration['width'];
    
$this->assertArrayHasKey('test:derived_toolkit', $toolkits);
    $this->assertArrayNotHasKey('broken', $toolkits);
    $this->assertToolkitOperationsCalled([]);
  }

  /** * Tests Image's methods. */
  public function testLoad() {
    $image = $this->getImage();
    $this->assertInstanceOf(ImageInterface::class$image);
    $this->assertEquals('test', $image->getToolkitId());
    $this->assertToolkitOperationsCalled(['parseFile']);
  }

  /** * Tests the Image::save() function. */
  public function testSave() {
    $this->assertFalse($this->image->save());
    $this->assertToolkitOperationsCalled(['save']);
  }

  
public function testIsValid() {
    $this->getTestImage(FALSE);
    $this->assertTrue($this->image->isValid());
    $this->assertFileIsReadable($this->image->getSource());
  }

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

  /** * Tests \Drupal\Core\Image\Image::save(). */
  public function testSave() {
    $this->getTestImage();
    // This will fail if save() method isn't called on the toolkit.     $toolkit = $this->getToolkitMock();
    $toolkit->expects($this->once())
      ->method('save')
      
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->installConfig(['system']);

    // Set the image factory service.     $this->imageFactory = $this->container->get('image.factory');
    $this->assertEquals('gd', $this->imageFactory->getToolkitId(), 'The image factory is set to use the \'gd\' image toolkit.');

    // Prepare a directory for test file results.     $this->directory = 'public://imagetest';
    \Drupal::service('file_system')->prepareDirectory($this->directory, FileSystemInterface::CREATE_DIRECTORY);
  }

  /** * Assert two colors are equal by RGBA, net of full transparency. * * @param int[] $expected * The expected RGBA array. * @param int[] $actual * The actual RGBA array. * @param int $tolerance * The acceptable difference between the colors. * @param string $message * The assertion message. */

class DesaturateImageEffect extends ImageEffectBase {

  /** * {@inheritdoc} */
  public function applyEffect(ImageInterface $image) {
    if (!$image->desaturate()) {
      $this->logger->error('Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
      return FALSE;
    }
    return TRUE;
  }

}
/** * {@inheritdoc} */
  public function applyEffect(ImageInterface $image) {
    if (!empty($this->configuration['random'])) {
      $degrees = abs((float) $this->configuration['degrees']);
      $this->configuration['degrees'] = rand(-$degrees$degrees);
    }

    if (!$image->rotate($this->configuration['degrees']$this->configuration['bgcolor'])) {
      $this->logger->error('Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
      return FALSE;
    }
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function transformDimensions(array &$dimensions$uri) {
    // If the rotate is not random and current dimensions are set,     // then the new dimensions can be determined.

class ScaleImageEffect extends ResizeImageEffect {

  /** * {@inheritdoc} */
  public function applyEffect(ImageInterface $image) {
    if (!$image->scale($this->configuration['width']$this->configuration['height']$this->configuration['upscale'])) {
      $this->logger->error('Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
      return FALSE;
    }
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function transformDimensions(array &$dimensions$uri) {
    if ($dimensions['width'] && $dimensions['height']) {
      Image::scaleDimensions($dimensions$this->configuration['width']$this->configuration['height']$this->configuration['upscale']);
    }
// Minimum size.     $errors = file_validate_image_resolution($this->image);
    $this->assertCount(0, $errors, 'No errors for an image when there is no minimum or maximum resolution.');
    $errors = file_validate_image_resolution($this->image, 0, '200x1');
    $this->assertCount(1, $errors, 'Got an error for an image that was not wide enough.');
    $errors = file_validate_image_resolution($this->image, 0, '1x200');
    $this->assertCount(1, $errors, 'Got an error for an image that was not tall enough.');
    $errors = file_validate_image_resolution($this->image, 0, '200x200');
    $this->assertCount(1, $errors, 'Small images report an error.');

    // Maximum size.     if ($this->container->get('image.factory')->getToolkitId()) {
      // Copy the image so that the original doesn't get resized.       copy('core/misc/druplicon.png', 'temporary://druplicon.png');
      $this->image->setFileUri('temporary://druplicon.png');

      $errors = file_validate_image_resolution($this->image, '10x5');
      $this->assertCount(0, $errors, 'No errors should be reported when an oversized image can be scaled down.');

      $image = $this->container->get('image.factory')->get($this->image->getFileUri());
      // Verify that the image was scaled to the correct width and height.       $this->assertLessThanOrEqual(10, $image->getWidth());
      $this->assertLessThanOrEqual(5, $image->getHeight());

      

class CropImageEffect extends ResizeImageEffect {

  /** * {@inheritdoc} */
  public function applyEffect(ImageInterface $image) {
    [$x$y] = explode('-', $this->configuration['anchor']);
    $x = image_filter_keyword($x$image->getWidth()$this->configuration['width']);
    $y = image_filter_keyword($y$image->getHeight()$this->configuration['height']);
    if (!$image->crop($x$y$this->configuration['width']$this->configuration['height'])) {
      $this->logger->error('Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
      return FALSE;
    }
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function getSummary() {
    $summary = [
      '#theme' => 'image_crop_summary',
      

  public function applyEffect(ImageInterface $image) {
    $width = $this->configuration['width'];
    $height = $this->configuration['height'];
    $scale = max($width / $image->getWidth()$height / $image->getHeight());

    [$x$y] = explode('-', $this->configuration['anchor']);
    $x = image_filter_keyword($x$image->getWidth() * $scale$width);
    $y = image_filter_keyword($y$image->getHeight() * $scale$height);

    if (!$image->apply('scale_and_crop', ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height])) {
      $this->logger->error('Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
      return FALSE;
    }
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function getSummary() {
    $summary = [
      '#theme' => 'image_scale_and_crop_summary',
      

class ConvertImageEffect extends ConfigurableImageEffectBase {

  /** * {@inheritdoc} */
  public function applyEffect(ImageInterface $image) {
    if (!$image->convert($this->configuration['extension'])) {
      $this->logger->error('Image convert failed using the %toolkit toolkit on %path (%mimetype)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType()]);
      return FALSE;
    }
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function getDerivativeExtension($extension) {
    return $this->configuration['extension'];
  }

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