You are here

function _img_assist_build_derivatives in Image Assist 6

Same name and namespace in other branches
  1. 5.3 img_assist.module \_img_assist_build_derivatives()
  2. 5 img_assist.module \_img_assist_build_derivatives()
  3. 5.2 img_assist.module \_img_assist_build_derivatives()
  4. 6.2 img_assist.module \_img_assist_build_derivatives()

Generate a image derivative

Parameters

$node:

$size: An array containing the keys 'label', 'width', 'height'.

See also

_image_build_derivatives() in image.module

Related topics

1 call to _img_assist_build_derivatives()
img_assist_display in ./img_assist.module
Create an IMG tag for an image.

File

./img_assist.module, line 1191
Image Assist module

Code

function _img_assist_build_derivatives(&$node, $size = NULL) {

  // Verify the image module and toolkit settings.
  if (!_image_check_settings()) {
    return FALSE;
  }
  $info = image_get_info(file_create_path($node->images[IMAGE_ORIGINAL]));

  // Custom size.
  if (is_array($size) && !empty($size['key']) && !empty($size['width']) && !empty($size['height'])) {
    $sizes = array(
      $size['key'] => $size,
    );
  }
  elseif ($size) {

    // Size can be an array without the width and/or height.
    if (is_array($size)) {
      $size = $size['key'];
    }
    $sizes = image_get_sizes();
    $sizes = array(
      $size => $sizes[$size],
    );
  }
  else {
    $sizes = image_get_sizes();
  }
  foreach ($sizes as $key => $size) {
    $size['key'] = $key;
    _img_assist_remove($node, $size);
    if (is_array($size) && $size['label'] && $size['width'] && $size['height']) {
      if ($info['width'] > $size['width'] || $info['height'] > $size['height']) {
        $source = file_create_path($node->images[IMAGE_ORIGINAL]);
        $destination = _image_filename(basename($source), $key, FALSE);
        $destination_path = file_create_path($destination);
        if (!image_scale($source, $destination_path, $size['width'], $size['height'])) {
          drupal_set_message(t('Unable to create %label image', array(
            '%label' => $size['label'],
          )), 'error');
        }
        else {

          // Set default file permissions for webserver-generated files.
          @chmod($destination_path, 0664);
          $node->images[$key] = $destination;
          _image_insert($node, $key, $destination_path);
        }
      }
      else {
        $node->images[$key] = $node->images[IMAGE_ORIGINAL];
      }
    }
  }
}