3 namespace Drush\Drupal\Commands\core;
5 use Consolidation\AnnotatedCommand\AnnotationData;
6 use Drupal\image\Entity\ImageStyle;
7 use Drush\Commands\DrushCommands;
8 use Drush\Utils\StringUtils;
9 use Symfony\Component\Console\Input\InputInterface;
11 class ImageCommands extends DrushCommands
15 * Flush all derived images for a given style.
17 * @command image:flush
18 * @param $style_names A comma delimited list of image style machine names. If not provided, user may choose from a list of names.
19 * @option all Flush all derived images
20 * @usage drush image:flush
21 * Pick an image style and then delete its derivatives.
22 * @usage drush image:flush thumbnail,large
23 * Delete all thumbnail and large derivatives.
24 * @usage drush image:flush --all
25 * Flush all derived images. They will be regenerated on demand.
26 * @validate-entity-load image_style style_names
27 * @validate-module-enabled image
28 * @aliases if,image-flush
30 public function flush($style_names, $options = ['all' => false])
32 foreach (ImageStyle::loadMultiple(StringUtils::csvToArray($style_names)) as $style_name => $style) {
34 $this->logger()->success(dt('Image style !style_name flushed', ['!style_name' => $style_name]));
39 * @hook interact image-flush
41 public function interactFlush($input, $output)
43 $styles = array_keys(ImageStyle::loadMultiple());
44 $style_names = $input->getArgument('style_names');
46 if (empty($style_names)) {
47 $styles_all = $styles;
48 array_unshift($styles_all, 'all');
49 $choices = array_combine($styles_all, $styles_all);
50 $style_names = $this->io()->choice(dt("Choose a style to flush"), $choices, 'all');
51 if ($style_names == 'all') {
52 $style_names = implode(',', $styles);
54 $input->setArgument('style_names', $style_names);
59 * @hook init image-flush
61 public function initFlush(InputInterface $input, AnnotationData $annotationData)
63 // Needed for non-interactive calls.
64 if ($input->getOption('all')) {
65 $styles = array_keys(ImageStyle::loadMultiple());
66 $input->setArgument('style_names', implode(",", $styles));
71 * Create an image derivative.
73 * @command image:derive
74 * @param $style_name An image style machine name.
75 * @param $source Path to a source image. Optionally prepend stream wrapper scheme.
76 * @usage drush image:derive thumbnail core/themes/bartik/screenshot.png
77 * Save thumbnail sized derivative of logo image.
78 * @validate-file-exists source
79 * @validate-entity-load image_style style_name
80 * @validate-module-enabled image
81 * @aliases id,image-derive
83 public function derive($style_name, $source)
85 $image_style = ImageStyle::load($style_name);
86 $derivative_uri = $image_style->buildUri($source);
87 if ($image_style->createDerivative($source, $derivative_uri)) {
88 return $derivative_uri;