You are here

function drush_imageinfo_cache_image_generate in Imageinfo Cache 7.3

Drush callback.

Parameters

string $field_name: Name of the field containing the images.

string $style_name: Style name to generate.

1 string reference to 'drush_imageinfo_cache_image_generate'
imageinfo_cache_drush_command in ./imageinfo_cache.drush.inc
Implements hook_drush_command().

File

./imageinfo_cache.drush.inc, line 45
Image module's drush integration.

Code

function drush_imageinfo_cache_image_generate($field_name = NULL, $style_name = NULL) {
  module_load_include('inc', 'imageinfo_cache', 'imageinfo_cache');
  $image_fields = imageinfo_cache_get_image_fields();
  $image_styles = image_styles();
  if (empty($field_name)) {

    // Output help.
    $choices = array_merge(array(
      'all' => 'all',
    ), drupal_map_assoc(array_keys($image_fields)));
    $field_name = drush_choice($choices, dt("Choose a target field to generate."));
  }
  if ($field_name !== 'all' && empty($image_fields[$field_name])) {

    // Bail if field name is bad.
    return drush_set_error(dt("Image field !field not recognized. Valid options:\n!fields", array(
      '!field' => $field_name,
      '!fields' => implode(', ', array_keys($image_fields)),
    )));
  }
  if (empty($style_name)) {

    // Output help.
    $choices = array_merge(array(
      'all' => 'all',
      'inuse' => 'inuse',
    ), drupal_map_assoc(array_keys($image_styles)));
    $style_name = drush_choice($choices, dt("Choose an image style to generate."));
  }
  if ($style_name !== 'all' && $style_name !== 'inuse' && empty($image_styles[$style_name])) {

    // Bail if style name is bad.
    return drush_set_error(dt("Image style !style not recognized. Valid options:\n!styles", array(
      '!style' => $style_name,
      '!styles' => implode(', ', array_keys($image_styles)),
    )));
  }

  // Get list of files.
  if ($field_name === 'all') {
    $files = imageinfo_cache_get_all_images_in_all_fields($image_fields);
  }
  else {
    $files[$field_name] = imageinfo_cache_get_all_images_in_field($image_fields, $field_name);
  }

  // Get list of image styles to generate.
  foreach ($files as $field_name => $uris) {
    if (empty($uris)) {
      drush_print(dt("!field_name - has no images attached to it.\n", array(
        '!field_name' => $field_name,
      )));
      continue;
    }
    if ($style_name === 'inuse') {
      foreach ($uris as $uri) {
        $image_styles = $image_fields[$field_name];
        unset($image_styles['#field_info']);
      }
    }
    elseif ($style_name !== 'all') {
      $image_styles = array(
        $style_name => $image_styles[$style_name],
      );
    }
    drush_print(dt("!field_name - Image styles: !style_names\n", array(
      '!field_name' => $field_name,
      '!style_names' => implode(", ", array_keys($image_styles)),
    )));
    foreach ($uris as $fid => $uri) {
      $return[$field_name][$fid] = imageinfo_cache_create_image_styles($uri, array(), $image_styles);
      if (!empty($return[$field_name][$fid])) {
        drush_log(dt('!uri derivatives generated.', array(
          '!uri' => $uri,
        )), 'success');
      }
      elseif (isset($return[$field_name][$fid])) {
        drush_log(dt('!uri already generated.', array(
          '!uri' => $uri,
        )), 'success');
      }
      else {
        drush_log(dt('Error generating image.', array(
          '!uri' => $uri,
        )), 'error');
      }
    }
  }
}