1 image.module image_style_create_derivative($style, $source, $destination)

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()

File

core/modules/image/image.module, line 860
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 = backdrop_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;
  }

  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;
}