function image_style_create_derivative in Drupal 7
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
4 calls to image_style_create_derivative()
- ImageFileMoveTest::testNormal in modules/
simpletest/ tests/ image.test - Tests moving a randomly generated image.
- ImageStyleFlushTest::createSampleImage in modules/
image/ image.test - Given an image style and a wrapper, generate an image.
- image_style_deliver in modules/
image/ image.module - Page callback: Generates a derivative, given a style and image path.
- theme_image_style_preview in modules/
image/ image.admin.inc - Returns HTML for a preview of an image style.
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;
}