You are here

function drush_imageapi_optimize in Image Optimize (or ImageAPI Optimize) 7.2

The ImageAPI Optimize Drush command.

File

./imageapi_optimize.drush.inc, line 70

Code

function drush_imageapi_optimize() {
  $cwd = drush_cwd();

  // Shell globbing of paths might deliver us lots of directories.
  $paths = func_get_args();
  $recursive = (bool) drush_get_option('recursive', TRUE);
  $pipeline = drush_get_option('pipeline');
  $image_extensions = explode(',', drush_get_option('extensions', 'png,jpg,jpeg'));
  $file_mask = '/\\.(' . implode('|', array_map('preg_quote', array_map('trim', $image_extensions))) . ')/i';
  $files = array();
  foreach ($paths as $rpath) {
    $path = realpath($cwd . DIRECTORY_SEPARATOR . $rpath);
    if (is_file($path)) {
      if (preg_match($file_mask, $path)) {
        $filesize = filesize($path);
        $files[$path] = array(
          'size' => $filesize,
        );
      }
    }
    elseif (is_dir($path)) {
      $scan = file_scan_directory($path, $file_mask, array(
        'recurse' => $recursive,
      ));
      foreach ($scan as $found) {
        $filesize = filesize($found->uri);
        $files[$found->uri] = array(
          'size' => $filesize,
        );
      }
    }
  }
  if (empty($files)) {
    return drush_set_error('No files found that match the file mask');
  }
  $total_size = 0;
  foreach ($files as $file) {
    $total_size += $file['size'];
  }
  if (!drush_confirm(dt('Are you sure you want to optimize the @count files found (total size !size)', array(
    '@count' => count($files),
    '!size' => format_size($total_size),
  )))) {
    return drush_set_error('Operation aborted.');
  }
  $backup = !drush_get_option('no-backup', FALSE);
  if ($backup) {
    $backup_engine = drush_include_engine('version_control', 'backup');
    $backup_dir = $backup_engine
      ->prepare_backup_dir('imageapi_optimize');
  }
  else {
    $backup_dir = NULL;
  }
  foreach ($files as $file => $info) {
    $batch['operations'][] = array(
      '_drush_imageapi_optimize_batch_optimize_single',
      array(
        $file,
        $pipeline,
        $backup_dir,
      ),
    );
  }
  $batch['finished'] = '_drush_imageapi_optimize_batch_finished';
  batch_set($batch);
  $batch =& batch_get();
  $batch['progressive'] = FALSE;
  drush_backend_batch_process();
}