'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(); // Because we are running via cli, imageapi does not work with GD or ImageMagick, // So we need to push this through apache. To do that we need the URL (domain) of the drupal installation // Try to determine the base URL if (isset($args[1])) { $base_url = $args[1]; } else { if (base_path() != '/') { $base_url = base_path(); } else { drush_log(dt('Sadly, drush could not determine the URL for your drupal installation. Try again with "drush imagecache build @a1 www.example.com"', array('@a1' => $args[0])), 'error'); return; } } if (substr($base_url,0,7) != 'http://') { $base_url = 'http://'.$base_url; } 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. 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']; $file_query = db_query("SELECT fid, filename, filepath FROM {files} where filemime LIKE 'image%'"); $files = array(); while ($row = db_fetch_object($file_query)) { $files[$row->fid] = array ( 'filename' => $row->filename, 'filepath' => $row->filepath, ); } foreach ($files as $fid => $file) { $path = imagecache_create_path($arg, $file['filepath']); if (!file_exists($path)) { $url = $base_url .'/'.$path; $handle = fopen($url, "r"); fclose($handle); } else { $url = $base_url .'/'.$path; drush_print($url); } } } else { drush_log(dt('Preset "@preset" not found.', array('@preset' => $arg)), 'error'); } } } return TRUE; }