You are here

public function ImageAPIOptimizeWebPPipeline::applyToImage 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::applyToImage()

File

src/Entity/ImageAPIOptimizeWebPPipeline.php, line 25

Class

ImageAPIOptimizeWebPPipeline
Wrap ImageAPIOptimizePipeline to copy webp derivative to proper directory.

Namespace

Drupal\imageapi_optimize_webp\Entity

Code

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;
      }
    }
  }
}