You are here

function _image_build_derivatives in Image 5.2

Same name and namespace in other branches
  1. 5 image.module \_image_build_derivatives()
  2. 6 image.module \_image_build_derivatives()

Generate image derivatives.

2 calls to _image_build_derivatives()
image_prepare in ./image.module
Implementation of hook_prepare().
image_update in ./image.module
Implementation of hook_update

File

./image.module, line 986

Code

function _image_build_derivatives(&$node, $temp = FALSE) {

  // sanity check:
  if (!_image_check_settings()) {
    return FALSE;
  }
  $original_path = file_create_path($node->images[IMAGE_ORIGINAL]);

  // Figure out which sizes we need to generate.
  $all_sizes = image_get_sizes();
  $needed_sizes = image_get_derivative_sizes($original_path);
  $unneeded_sizes = array_diff(array_keys($all_sizes), array_keys($needed_sizes));

  // Images that don't need a derivative image get set to the original.
  foreach ($unneeded_sizes as $key) {
    $node->images[$key] = $original_path;
  }

  // Resize for the necessary sizes.
  $image_info = image_get_info($original_path);
  foreach ($needed_sizes as $key => $size) {
    $destination = _image_filename($original_path, $key, $temp);
    $status = FALSE;
    switch ($size['operation']) {

      // Depending on the operation, the image will be scaled or resized & cropped
      case 'scale':
        $status = image_scale($original_path, $destination, $size['width'], $size['height']);
        break;
      case 'scale_crop':

        // This is based on Drupal 6's image_scale_and_crop(). Scales the
        // image so that the image's smaller dimension matches the
        // requested size and the crops any overlap on the image's larger
        // dimension.
        $scale = max($size['width'] / $image_info['width'], $size['height'] / $image_info['height']);
        $x = round(($image_info['width'] * $scale - $size['width']) / 2);
        $y = round(($image_info['height'] * $scale - $size['height']) / 2);
        if (image_resize($original_path, $destination, $image_info['width'] * $scale, $image_info['height'] * $scale)) {
          $status = image_crop($destination, $destination, $x, $y, $size['width'], $size['height']);
        }
        break;
    }
    if (!$status) {
      drupal_set_message(t('Unable to create scaled %label image', array(
        '%label' => $size['label'],
      )), 'error');
      return FALSE;
    }

    // Set standard file permissions for webserver-generated files
    @chmod($destination, 0664);
    $node->images[$key] = $destination;
    module_invoke_all('image_alter', $node, $destination, $key);
  }
}