You are here

class ImageAPIOptimizeWebPPipeline in ImageAPI Optimize WebP 8

Same name and namespace in other branches
  1. 2.x src/Entity/ImageAPIOptimizeWebPPipeline.php \Drupal\imageapi_optimize_webp\Entity\ImageAPIOptimizeWebPPipeline

Wrap ImageAPIOptimizePipeline to copy webp derivative to proper directory.

This wrapper allows for .webp image derivatives to be copied to the correct directory after the webp image_api handler takes place.

Class ImageAPIOptimizeWebPPipeline

@package Drupal\imageapi_optimize_webp\Entity

Hierarchy

Expanded class hierarchy of ImageAPIOptimizeWebPPipeline

File

src/Entity/ImageAPIOptimizeWebPPipeline.php, line 20

Namespace

Drupal\imageapi_optimize_webp\Entity
View source
class ImageAPIOptimizeWebPPipeline extends ImageAPIOptimizePipeline {

  /**
   * {@inheritdoc}
   */
  public function applyToImage($image_uri) {
    parent::applyToImage($image_uri);

    // If the source file doesn't exist, return FALSE.
    $image = \Drupal::service('image.factory')
      ->get($image_uri);
    if (!$image
      ->isValid()) {
      return FALSE;
    }
    if (count($this
      ->getProcessors())) {
      $webp_uri = $image_uri . '.webp';
      foreach ($this->temporaryFiles as $temp_image_uri) {
        $temp_webp_uri = $temp_image_uri . '.webp';
        $temp_image_uri = FileSystemInterface::copy($temp_webp_uri, $webp_uri, FILE_EXISTS_RENAME);
        if ($temp_image_uri) {
          $this->temporaryFiles[] = $temp_webp_uri;
          break;
        }
      }
    }
  }

}

Members