5 * Image module's drush integration.
7 * @todo image-build($field_name, $bundle, $style_name)
10 use Drush\Log\LogLevel;
13 * Implementation of hook_drush_command().
15 function image_drush_command() {
16 $items['image-flush'] = array(
17 'description' => 'Flush all derived images for a given style.',
18 'core' => array('7+'),
20 'style' => 'An image style machine name. If not provided, user may choose from a list of names.',
23 'all' => 'Flush all derived images',
26 'drush image-flush' => 'Pick an image style and then delete its images.',
27 'drush image-flush thumbnail' => 'Delete all thumbnail images.',
28 'drush image-flush --all' => 'Flush all derived images. They will be regenerated on the fly.',
30 'aliases' => array('if'),
32 $items['image-derive'] = array(
33 'description' => 'Create an image derivative.',
34 'core' => array('7+'),
35 'drupal dependencies' => array('image'),
37 'style' => 'An image style machine name.',
38 'source' => 'Path to a source image. Optionally prepend stream wrapper scheme.',
40 'required arguments' => TRUE,
43 'drush image-derive thumbnail themes/bartik/logo.png' => 'Save thumbnail sized derivative of logo image.',
45 'aliases' => array('id'),
51 * Implements hook_drush_help_alter().
53 function image_drush_help_alter(&$command) {
54 // Drupal 8+ customizations.
55 if ($command['command'] == 'image-derive' && drush_drupal_major_version() >= 8) {
56 unset($command['examples']);
57 $command['examples']['drush image-derive thumbnail core/themes/bartik/logo.png'] = 'Save thumbnail sized derivative of logo image.';
62 * Command argument complete callback.
65 * Array of available configuration files for editing.
67 function image_image_flush_complete() {
68 drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
69 drush_include_engine('drupal', 'image');
70 return array('values' => array_keys(drush_image_styles()));
73 function drush_image_flush_pre_validate($style_name = NULL) {
74 drush_include_engine('drupal', 'image');
75 if (!empty($style_name) && !$style = drush_image_style_load($style_name)) {
76 return drush_set_error(dt('Image style !style not recognized.', array('!style' => $style_name)));
80 function drush_image_flush($style_name = NULL) {
81 drush_include_engine('drupal', 'image');
82 if (drush_get_option('all')) {
86 if (empty($style_name)) {
87 $styles = array_keys(drush_image_styles());
88 $choices = array_combine($styles, $styles);
89 $choices = array_merge(array('all' => 'all'), $choices);
90 $style_name = drush_choice($choices, dt("Choose a style to flush."));
91 if ($style_name === FALSE) {
92 return drush_user_abort();
96 if ($style_name == 'all') {
97 foreach (drush_image_styles() as $style_name => $style) {
98 drush_image_flush_single($style_name);
100 drush_log(dt('All image styles flushed'), LogLevel::SUCCESS);
103 drush_image_flush_single($style_name);
107 function drush_image_derive_validate($style_name, $source) {
108 drush_include_engine('drupal', 'image');
109 if (!$style = drush_image_style_load($style_name)) {
110 return drush_set_error(dt('Image style !style not recognized.', array('!style' => $style_name)));
113 if (!file_exists($source)) {
114 return drush_set_error(dt('Source file not found - !file.', array('!file' => $source)));
119 * Command callback. Create an image derivative.
121 * @param string $style_name
122 * The name of an image style.
124 * @param string $source
125 * The path to a source image, relative to Drupal root.
127 function drush_image_derive($style_name, $source) {
128 drush_include_engine('drupal', 'image');
129 return _drush_image_derive($style_name, $source);