You are here

public function ImageStylesWarmer::doWarmUp in Image Style Warmer 8

Do warm up of image styles for an image file.

Parameters

\Drupal\file\FileInterface $file: The file for which image styles should be created.

array $image_styles: List of image styles machine names.

Overrides ImageStylesWarmerInterface::doWarmUp

3 calls to ImageStylesWarmer::doWarmUp()
ImageStylesWarmer::initialWarmUp in src/ImageStylesWarmer.php
Initial warm up of images style from a file.
ImageStylesWarmer::queueWarmUp in src/ImageStylesWarmer.php
Queue warm up of images style from a file.
ImageStylesWarmer::warmUp in src/ImageStylesWarmer.php
Init warm up with configured image styles for an image file.

File

src/ImageStylesWarmer.php, line 93

Class

ImageStylesWarmer
Defines an images styles warmer.

Namespace

Drupal\image_style_warmer

Code

public function doWarmUp(FileInterface $file, array $image_styles) {
  if (empty($image_styles) || !$this
    ->validateImage($file)) {
    return;
  }

  /* @var \Drupal\Core\Image\Image $image */

  /* @var \Drupal\image\Entity\ImageStyle $style */

  // Create image derivatives if they not already exists.
  $styles = $this->imageStyles
    ->loadMultiple($image_styles);
  $image_uri = $file
    ->getFileUri();
  foreach ($styles as $style) {
    $derivative_uri = $style
      ->buildUri($image_uri);
    if (!file_exists($derivative_uri)) {
      $style
        ->createDerivative($image_uri, $derivative_uri);
    }
  }
}