You are here

public function ImageStyle::createDerivative in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/image/src/Entity/ImageStyle.php \Drupal\image\Entity\ImageStyle::createDerivative()

Creates a new image derivative based on this image style.

Generates an image derivative applying all image effects and saving the resulting image.

Parameters

string $original_uri: Original image file URI.

string $derivative_uri: Derivative image file URI.

Return value

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

Overrides ImageStyleInterface::createDerivative

File

core/modules/image/src/Entity/ImageStyle.php, line 278
Contains \Drupal\image\Entity\ImageStyle.

Class

ImageStyle
Defines an image style configuration entity.

Namespace

Drupal\image\Entity

Code

public function createDerivative($original_uri, $derivative_uri) {

  // If the source file doesn't exist, return FALSE without creating folders.
  $image = \Drupal::service('image.factory')
    ->get($original_uri);
  if (!$image
    ->isValid()) {
    return FALSE;
  }

  // Get the folder for the final location of this style.
  $directory = drupal_dirname($derivative_uri);

  // Build the destination folder tree if it doesn't already exist.
  if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    \Drupal::logger('image')
      ->error('Failed to create style directory: %directory', array(
      '%directory' => $directory,
    ));
    return FALSE;
  }
  foreach ($this
    ->getEffects() as $effect) {
    $effect
      ->applyEffect($image);
  }
  if (!$image
    ->save($derivative_uri)) {
    if (file_exists($derivative_uri)) {
      \Drupal::logger('image')
        ->error('Cached image file %destination already exists. There may be an issue with your rewrite configuration.', array(
        '%destination' => $derivative_uri,
      ));
    }
    return FALSE;
  }
  return TRUE;
}