You are here

function imagecache_build_derivative in ImageCache 6.2

Same name and namespace in other branches
  1. 5.2 imagecache.module \imagecache_build_derivative()

Create a new image based on an image preset.

Parameters

$preset: An image preset array.

$source: Path of the source file.

$destination: Path of the destination file.

Return value

TRUE if an image derivative is generated, FALSE if no image derivative is generated. NULL if the derivative is being generated.

2 calls to imagecache_build_derivative()
imagecache_generate_image in ./imagecache.module
Generate a derivative image given presetname and filepath.
_imagecache_cache in ./imagecache.module
Handle request validation and responses to ImageCache requests.

File

./imagecache.module, line 599
Dynamic image resizer and image cacher.

Code

function imagecache_build_derivative($actions, $src, $dst) {

  // get the folder for the final location of this preset...
  $dir = dirname($dst);

  // Build the destination folder tree if it doesn't already exists.
  if (!file_check_directory($dir, FILE_CREATE_DIRECTORY) && !mkdir($dir, 0775, TRUE)) {
    watchdog('imagecache', 'Failed to create imagecache directory: %dir', array(
      '%dir' => $dir,
    ), WATCHDOG_ERROR);
    return FALSE;
  }

  // file_check_directory() has an annoying habit of displaying "directory ...
  // has been created" status messages. To avoid confusing visitors we clear
  // out all the status messages for non-ImageCache admins. This might affect
  // some other messages but errors and warnings should still be displayed.
  if (!user_access('administer imagecache')) {
    drupal_get_messages('status', TRUE);
  }

  // Simply copy the file if there are no actions.
  if (empty($actions)) {
    return file_copy($src, $dst, FILE_EXISTS_REPLACE);
  }
  if (!($image = imageapi_image_open($src))) {
    return FALSE;
  }
  if (file_exists($dst)) {
    watchdog('imagecache', 'Cached image file %dst already exists but is being regenerated. There may be an issue with your rewrite configuration.', array(
      '%dst' => $dst,
    ), WATCHDOG_WARNING);
  }
  foreach ($actions as $action) {
    if (!empty($action['data'])) {

      // Make sure the width and height are computed first so they can be used
      // in relative x/yoffsets like 'center' or 'bottom'.
      if (isset($action['data']['width'])) {
        $action['data']['width'] = _imagecache_percent_filter($action['data']['width'], $image->info['width']);
      }
      if (isset($action['data']['height'])) {
        $action['data']['height'] = _imagecache_percent_filter($action['data']['height'], $image->info['height']);
      }
      if (isset($action['data']['xoffset'])) {
        $action['data']['xoffset'] = _imagecache_keyword_filter($action['data']['xoffset'], $image->info['width'], $action['data']['width']);
      }
      if (isset($action['data']['yoffset'])) {
        $action['data']['yoffset'] = _imagecache_keyword_filter($action['data']['yoffset'], $image->info['height'], $action['data']['height']);
      }
    }
    if (!_imagecache_apply_action($action, $image)) {
      watchdog('imagecache', 'action(id:%id): %action failed for %src', array(
        '%id' => $action['actionid'],
        '%action' => $action['action'],
        '%src' => $src,
      ), WATCHDOG_ERROR);
      return FALSE;
    }
  }
  if (!imageapi_image_close($image, $dst)) {
    watchdog('imagecache', 'There was an error saving the new image file %dst.', array(
      '%dst' => $dst,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  return TRUE;
}