Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ImageStyle.php

function ImageStyle::createDerivative

Overrides ImageStyleInterface::createDerivative

File

core/modules/image/src/Entity/ImageStyle.php, line 322

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 = $this->getImageFactory()
        ->get($original_uri);
    if (!$image->isValid()) {
        return FALSE;
    }
    // Get the folder for the final location of this style.
    $directory = \Drupal::service('file_system')->dirname($derivative_uri);
    // Build the destination folder tree if it doesn't already exist.
    if (!\Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
        \Drupal::logger('image')->error('Failed to create style directory: %directory', [
            '%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.', [
                '%destination' => $derivative_uri,
            ]);
        }
        return FALSE;
    }
    return TRUE;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal