You are here

trait ImagickOperationTrait in Imagick 8

Class ImagickOperationTrait

Hierarchy

File

src/Plugin/ImageToolkit/Operation/imagick/ImagickOperationTrait.php, line 10

Namespace

Drupal\imagick\Plugin\ImageToolkit\Operation\imagick
View source
trait ImagickOperationTrait {

  /**
   * {@inheritdoc}
   */
  protected function execute(array $arguments) {
    return $this
      ->processFrames($arguments);
  }

  /**
   * Process image frames for GIFs
   *
   * @param array $arguments
   * @return bool
   */
  protected function processFrames(array $arguments = []) {

    /* @var $resource Imagick */
    $resource = $this
      ->getToolkit()
      ->getResource();

    // If preferred format is set, use it as prefix for writeImage
    // If not this will throw a ImagickException exception
    try {
      $image_format = $resource
        ->getImageFormat();
    } catch (ImagickException $e) {
    }
    $success = TRUE;
    if (isset($image_format) && in_array($image_format, [
      'GIF',
    ])) {

      // Get each frame in the GIF
      $resource = $resource
        ->coalesceImages();
      do {
        if (!$this
          ->process($resource, $arguments)) {
          $success = FALSE;
          break;
        }
      } while ($resource
        ->nextImage());
      $resource
        ->deconstructImages();
    }
    else {
      $success = $this
        ->process($resource, $arguments);
    }

    // Set the processed resource
    $this
      ->getToolkit()
      ->setResource($resource);
    return $success;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImagickOperationTrait::execute protected function 3
ImagickOperationTrait::processFrames protected function Process image frames for GIFs