function image_style_create_derivative

Creates a new image derivative based on an image style.

Generates an image derivative by creating the destination folder (if it does not already exist), applying all image effects defined in $style->effects, and saving a cached version of the resulting image.

Parameters

$style: An image style array.

$source: Path of the source file.

$destination: Path or URI of the destination file.

Return value

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

See also

image_style_load()

3 calls to image_style_create_derivative()
FileMoveTest::testNormal in drupal/core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php
Tests moving a randomly generated image.
image_style_deliver in drupal/core/modules/image/image.module
Menu callback; Given a style and image path, generate a derivative.
theme_image_style_preview in drupal/core/modules/image/image.admin.inc
Returns HTML for a preview of an image style.

File

drupal/core/modules/image/image.module, line 750
Exposes global functionality for creating image styles.

Code

function image_style_create_derivative($style, $source, $destination) {

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

  // Build the destination folder tree if it doesn't already exist.
  if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    watchdog('image', 'Failed to create style directory: %directory', array(
      '%directory' => $directory,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  if (!($image = image_load($source))) {
    return FALSE;
  }
  if (!empty($style->effects)) {
    foreach ($style->effects as $effect) {
      image_effect_apply($image, $effect);
    }
  }
  if (!image_save($image, $destination)) {
    if (file_exists($destination)) {
      watchdog('image', 'Cached image file %destination already exists. There may be an issue with your rewrite configuration.', array(
        '%destination' => $destination,
      ), WATCHDOG_ERROR);
    }
    return FALSE;
  }
  return TRUE;
}