You are here

function imageinfo_cache_create_image_styles in Imageinfo Cache 7.3

Generates all given presets given a file uri.

Parameters

mixed $uri: String URI from a file object; or array of URIs.

array $instance_field: Field info from field_info_instances().

array $styles: (optional) Styles to use from image_styles().

bool $entity_available: (optional) TRUE if coming from entity save.

Return value

array An array of what was done.

2 calls to imageinfo_cache_create_image_styles()
drush_imageinfo_cache_image_generate in ./imageinfo_cache.drush.inc
Drush callback.
imageinfo_cache_create_image_styles_fids_call in ./imageinfo_cache.module
Generates all given presets given a file uri.

File

./imageinfo_cache.inc, line 371
Imageinfo Cache module. Helper functions.

Code

function imageinfo_cache_create_image_styles($uri, array $instance_field = array(), array $styles = array(), $entity_available = TRUE) {
  $return = array();

  // Use files array.
  $files = array();
  if (is_string($uri)) {
    $files[] = $uri;
  }
  else {
    $files = $uri;
  }
  if (empty($styles) && !empty($instance_field)) {

    // Get image styles used by this field.
    $styles = imageinfo_cache_get_styles_in_use($instance_field);
  }

  // Skip styles that need context.
  if (!$entity_available) {
    foreach ($styles as $style_name => $style_info) {
      if (!empty($style_info['#needs_context'])) {
        unset($styles[$style_name]);
      }
    }
  }

  // Do nothing if no styles are here for the image.
  if (empty($styles)) {
    return $return;
  }
  foreach ($files as $uri) {
    foreach ($styles as $style_name => $style_info) {
      $destination = image_style_path($style_name, $uri);

      // If the file does not exist, create it.
      if (!file_exists($destination)) {

        // Don't start generating the image if the derivative already exists or
        // if generation is in progress in another thread.
        $lock_name = 'image_style_deliver:' . $style_name . ':' . drupal_hash_base64($uri);
        $lock_acquired = lock_acquire($lock_name);
        if ($lock_acquired && image_style_create_derivative($style_info, $uri, $destination)) {
          lock_release($lock_name);
          $return[$destination] = $style_name;
          if (variable_get('imageinfo_cache_getimagesize', IMAGEINFO_CACHE_GETIMAGESIZE)) {

            // Prime get_info cache.
            image_get_info($destination);
          }
        }
      }
      else {
        $return[$destination] = FALSE;
      }
    }
    if (variable_get('imageinfo_cache_getimagesize', IMAGEINFO_CACHE_GETIMAGESIZE)) {

      // Prime get_info cache.
      image_get_info($uri);
    }
  }
  return $return;
}