-
Notifications
You must be signed in to change notification settings - Fork 3
/
image_derive_all.drush.inc
155 lines (133 loc) · 4.88 KB
/
image_derive_all.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* @file
* Implementation for the image-derive-all drush command.
*
* Generate image derivatives for given image styles.
*/
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\image\Entity\ImageStyle;
/**
* Implements hook_drush_command().
*/
function image_derive_all_drush_command() {
$items = [];
$items['image-derive-all'] = [
'description' => "Create image derivatives.",
'arguments' => [
'styles' => 'A comma delimited list of image style machine name(s).',
],
'options' => [
'exclude' => [
'description' => 'A comma delimited list of all image styles to be excluded.',
'example_value' => 'large',
],
'dir' => [
'description' => 'Images derivates will be generated of all images inside this directory (relative to public files directory). Default will generate derivates of all public images.',
'example_value' => 'xyz',
],
'purge' => [
'description' => 'Regenerate existing image derivatives.',
],
],
'examples' => [
'drush image-derive-all' => 'Generates image derivatives for every image style of every image in the public file system.',
'drush image-derive-all thumbnail,teaser --dir=xyz' => 'Generates image derivatives for the image style "thumbnail" and "teaser" of images in the "xyz" directory in the public file directory.',
'drush image-derive-all --exclude=thumbnail --dir=public' => 'Generates image derivatives for all image styles except "thumbnail" for every file within the public file system directory but not in subdirectories.',
'drush image-derive-all thumbnail --dir=public --purge' => 'Generates image derivatives for the "thumbnail" style only and overwriting images if they exist, for every file within the public file system directory but not in subdirectories.',
],
'core' => ['8', '9'],
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'callback' => 'image_derive_all_drush_action',
'aliases' => ['ida'],
];
return $items;
}
/**
* Command callback. Create image derivatives for give styles.
*/
function image_derive_all_drush_action() {
$purge = drush_get_option('purge', FALSE);
$files = image_derive_all_get_relevant_files(drush_get_option('dir'));
$excludes = explode(",", drush_get_option('exclude'));
$includes = explode(",", func_get_args()[0]);
$imageStyles = ImageStyle::loadMultiple();
/* @var string $name Name of Style */
/* @var ImageStyle $style ImageStyle Object */
foreach ($imageStyles as $name => $style) {
if (!empty($excludes) && in_array($name, $excludes)) {
drush_print('Excluding ' . $name);
continue;
}
if (empty($includes[0]) || in_array($name, $includes)) {
drush_print("Processing Style $name");
$count = 1;
foreach ($files as $file) {
$destination = $style->buildUri($file->uri);
if ($purge) {
if (file_exists($destination)) {
unlink($destination);
}
}
if (!file_exists($destination)) {
$style->createDerivative($file->uri, $destination);
image_derive_all_show_progress($count, count($files), $name);
}
$count++;
}
drush_print("Style $name Processed");
}
}
}
/**
* Fetch files from database.
*/
function image_derive_all_get_relevant_files($directory) {
$extensions = ['jpeg', 'jpg', 'gif', 'png'];
$mimetypes = ['image/jpeg', 'image/jpg', 'image/gif', 'image/png'];
$dir = rtrim($directory, '/');
if ($dir == 'public') {
$file_pattern = ".*";
}
else {
$file_pattern = $dir ? $dir . ".+" : ".+";
}
$regex = "^public:\/\/(" . $file_pattern . ")\.(" . implode('|', $extensions) . ")$";
// Query the database for files that match this pattern.
$query = Database::getConnection()
->select('file_managed', 'f');
$andGroup = $query->andConditionGroup()
->condition('filemime', $mimetypes, 'IN')
->condition('uri', $regex, 'REGEXP');
$query->condition($andGroup);
$total_count = $query
->countQuery()
->execute()
->fetchField();
drush_print(\Drupal::translation()->formatPlural($total_count,
'1 entry is to have its image derivatives created.',
'@count entries are to have their image derivatives created.')
);
// Select the files to have derivatives created.
return $query->fields('f', array('fid', 'filename', 'uri'))
->execute()
->fetchAll();
}
/**
* Provide progress info.
*/
function image_derive_all_show_progress($count, $total, $name) {
if (!isset($display)) {
static $display = [];
}
if (!array_key_exists($name, $display)) {
// Integer steps to display <= 100.
$display[$name] = range(25, 100, 25);
}
$progress = ($count / $total) * 100;
if (count($display[$name]) and $progress >= $display[$name][0]) {
drush_print("Style $name progress " . $display[$name][0] . "%");
array_shift($display[$name]);
}
}