'imagecache_drush_preset_flush', 'description' => dt('Flush an imagecache preset.'), 'examples' => array( 'drush imagecache flush foobar' => dt('Flush the ImageCache preset "foobar".'), ), 'aliases' => array('icf'), ); $items['imagecache-build'] = array( 'callback' => 'imagecache_drush_preset_build', 'description' => dt('Build imagecache derivates for all images for a given preset.'), 'examples' => array( 'drush imagecache build foobar www.example.com' => dt('Build all images for preset "foobar".'), ), 'aliases' => array('icb'), ); return $items; } /** * Implementation of hook_drush_help(). */ function imagecache_drush_help($section) { switch ($section) { case 'drush:imagecache-flush': return dt('Flush a given preset.'); case 'drush:imagecache-build': return dt('Build derivative images for a given preset.'); } } /** * Drush callback to perform actual imagecache preset flush. */ function imagecache_drush_preset_flush() { $args = func_get_args(); if (empty($args)) { foreach (imagecache_presets(TRUE) as $preset) { $preset_names[] = $preset['presetname']; } drush_set_error(dt('You must specify a preset name to flush or specify "all".'. "\n".'Presets are: ').implode($preset_names,' ')); } else { // Implement 'all' if (count($args) == 1 && $args[0] == 'all') { $args = array(); foreach (imagecache_presets(TRUE) as $preset) { $args[] = $preset['presetname']; } } foreach ($args as $arg) { // Load preset. if ($preset = imagecache_preset_by_name($arg)) { // This mimics the logic inside of the function // imagecache_preset_flush(), but without the access check. $presetdir = DRUPAL_ROOT . base_path() . file_directory_path() .'/imagecache/'. $preset['presetname']; if (is_dir($presetdir)) { _imagecache_recursive_delete($presetdir); drush_log(dt('Flushed "@preset" preset.', array('@preset' => $arg)), 'ok'); } else { drush_log(dt('Cache for preset "@preset" was already empty.', array('@preset' => $arg)), 'ok'); } } else { drush_log(dt('Preset "@preset" not found.', array('@preset' => $arg)), 'error'); } } } return TRUE; } /** * Drush callback to perform actual imagecache preset build. */ function imagecache_drush_preset_build() { $args = func_get_args(); // Rebuild imagecache presets imagecache_presets(TRUE); // Change the running dir $root = array_pop($args); if (is_dir($root)) { $_SERVER['DOCUMENT_ROOT'] = $root; } else { $args[] = $root; } if (empty($args)) { foreach (imagecache_presets(TRUE) as $preset) { $preset_names[] = $preset['presetname']; } drush_set_error(dt('You must specify a preset name to build or specify "all".'. "\n".'Presets are: ').implode($preset_names,' ')); } else { // Implement 'all' if ($args[0] == 'all') { $args = array(); foreach (imagecache_presets(TRUE) as $preset) { $args[] = $preset['presetname']; } } foreach ($args as $arg) { // Load preset. $arg = trim($arg); if ($preset = imagecache_preset_by_name($arg)) { $file_query = db_query("SELECT filepath FROM {files} where filemime LIKE 'image%' ORDER BY fid DESC"); $files = array(); while ($filepath = db_result($file_query)) { if (file_exists($filepath)) { $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'); } } } } } else { drush_log(dt('Preset "@preset" not found.', array('@preset' => $arg)), 'error'); } } } return TRUE; }