Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / commands / core / image.drush.inc
1 <?php
2
3 /**
4  * @file
5  *  Image module's drush integration.
6  *
7  *  @todo image-build($field_name, $bundle, $style_name)
8  */
9
10 use Drush\Log\LogLevel;
11
12 /**
13  * Implementation of hook_drush_command().
14  */
15 function image_drush_command() {
16   $items['image-flush'] = array(
17     'description' => 'Flush all derived images for a given style.',
18     'core' => array('7+'),
19     'arguments' => array(
20       'style' => 'An image style machine name. If not provided, user may choose from a list of names.',
21     ),
22     'options' => array(
23       'all' => 'Flush all derived images',
24     ),
25     'examples' => array(
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.',
29     ),
30     'aliases' => array('if'),
31   );
32   $items['image-derive'] = array(
33     'description' => 'Create an image derivative.',
34     'core' => array('7+'),
35     'drupal dependencies' => array('image'),
36     'arguments' => array(
37       'style' => 'An image style machine name.',
38       'source' => 'Path to a source image. Optionally prepend stream wrapper scheme.',
39     ),
40     'required arguments' => TRUE,
41     'options' => array(),
42     'examples' => array(
43       'drush image-derive thumbnail themes/bartik/logo.png' => 'Save thumbnail sized derivative of logo image.',
44     ),
45     'aliases' => array('id'),
46   );
47   return $items;
48 }
49
50 /**
51  * Implements hook_drush_help_alter().
52  */
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.';
58   }
59 }
60
61 /**
62  * Command argument complete callback.
63  *
64  * @return
65  *   Array of available configuration files for editing.
66  */
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()));
71 }
72
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)));
77   }
78 }
79
80 function drush_image_flush($style_name = NULL) {
81   drush_include_engine('drupal', 'image');
82   if (drush_get_option('all')) {
83     $style_name = 'all';
84   }
85
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();
93     }
94   }
95
96   if ($style_name == 'all') {
97     foreach (drush_image_styles() as $style_name => $style) {
98       drush_image_flush_single($style_name);
99     }
100     drush_log(dt('All image styles flushed'), LogLevel::SUCCESS);
101   }
102   else {
103     drush_image_flush_single($style_name);
104   }
105 }
106
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)));
111   }
112
113   if (!file_exists($source)) {
114     return drush_set_error(dt('Source file not found - !file.', array('!file' => $source)));
115   }
116 }
117
118 /*
119  * Command callback. Create an image derivative.
120  *
121  * @param string $style_name
122  *   The name of an image style.
123  *
124  * @param string $source
125  *   The path to a source image, relative to Drupal root.
126  */
127 function drush_image_derive($style_name, $source) {
128   drush_include_engine('drupal', 'image');
129   return _drush_image_derive($style_name, $source);
130 }