You are here

function imagecache_drush_preset_build in ImageCache 6.2

Drush callback to perform actual imagecache preset build.

1 string reference to 'imagecache_drush_preset_build'
imagecache_drush_command in ./imagecache.drush.inc
Implementation of hook_drush_command().

File

./imagecache.drush.inc, line 99

Code

function imagecache_drush_preset_build() {
  $args = func_get_args();

  // Rebuild imagecache presets.
  foreach (imagecache_presets(TRUE) as $preset) {
    $preset_names[] = $preset['presetname'];
  }
  if (empty($args)) {
    $choice = drush_choice($preset_names, 'Enter a number to choose which preset to flush.');
    if ($choice !== FALSE) {
      $args[] = $preset_names[$choice];
    }
  }
  elseif ($args[0] == 'all') {

    // Implement 'all'
    $args = $preset_names;
  }

  // Remove any invalid preset names and report them as errors.
  $not_found = array_diff($args, $preset_names);
  $args = array_intersect($args, $preset_names);
  if ($not_found) {
    drush_log(dt('Preset(s) not found: @presets', array(
      '@presets' => implode($not_found, ' '),
    )), 'error');
  }
  if (empty($args)) {
    return FALSE;
  }

  // Get a list of files to processes.
  $file_query = db_query("SELECT filepath FROM {files} where filemime LIKE 'image%' ORDER BY fid DESC");
  $files = array();
  drush_log(dt('Generating file list...', array()), 'ok');
  while ($filepath = db_result($file_query)) {
    if (file_exists($filepath)) {
      $files[] = $filepath;
    }
  }
  if (empty($files)) {
    drush_log(dt('No images found in the files table.', array()), 'error');
    return FALSE;
  }
  $count = count($files);
  drush_log(dt('Done. @count files to process using these presets: @presets', array(
    '@count' => $count,
    '@presets' => implode(' ', $args),
  )), 'ok');

  // Generate the images.
  $counter = 0;
  $mod = round($count / 200);
  foreach ($files as $filepath) {
    foreach ($args as $arg) {
      $path = imagecache_create_path($arg, $filepath);
      if (!file_exists($path)) {
        imagecache_generate_image($arg, $filepath);
        if (file_exists($path)) {
          drush_log(dt('File "@file" created.', array(
            '@file' => $path,
          )), 'ok');
        }
        else {
          drush_log(dt('File "@file" not created.', array(
            '@file' => $path,
          )), 'error');
        }
      }
    }

    // Output progress.
    $counter++;
    if ($counter % $mod == 0) {
      drush_log(dt('@percent% done.', array(
        '@percent' => round($counter / $count * 100, 2),
      )), 'ok');
    }
  }
  return TRUE;
}