You are here

public function ImageAPIOptimizeWebPPipeline::applyToImage in ImageAPI Optimize WebP 2.x

Same name and namespace in other branches
  1. 8 src/Entity/ImageAPIOptimizeWebPPipeline.php \Drupal\imageapi_optimize_webp\Entity\ImageAPIOptimizeWebPPipeline::applyToImage()

Creates a new image derivative based on this image optimize pipeline.

Generates an image derivative applying all image optimize processors and saving the resulting image.

Parameters

string $image_uri: Original image file URI.

Return value

bool TRUE if an image derivative was generated, or FALSE if the image derivative could not be generated.

Overrides ImageAPIOptimizePipeline::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';
      if (file_exists($temp_webp_uri)) {
        $temp_image_uri = \Drupal::service('file_system')
          ->copy($temp_webp_uri, $webp_uri, FileSystemInterface::EXISTS_RENAME);
        if ($temp_image_uri) {
          $this->temporaryFiles[] = $temp_webp_uri;
          break;
        }
      }
    }
  }
}