function image_style_create_derivative

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

modules/image/image.module, line 913
Exposes global functionality for creating image styles.

Code

function image_style_create_derivative($style, $source, $destination) {
  // If the source file doesn't exist, return FALSE without creating folders.
  if (!$image = image_load($source)) {
    return FALSE;
  }

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

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

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_create_derivative/7.x