/
smush_it.module
420 lines (375 loc) · 12.1 KB
/
smush_it.module
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
define('BC_LOADED', extension_loaded('bcmath'));
/**
* @Author SeanJA <http://seanja.com>
* @license MIT
*/
module_load_include('inc', 'smush_it', 'smush_it.class');
module_load_include('inc', 'smush_it', 'smush_it.functions');
if (BC_LOADED) {
module_load_include('inc', 'smush_it', 'smush_it.bcfunctions');
}
/**
* Implements hook_nodeapi()
* @staticvar string $image_mimes
* @param node $node The node being manipulated
* @param string $op The operation being performed
* @param string $teaser
*/
function smush_it_nodeapi(&$node, $op, $teaser) {
static $image_mimes;
if (!$image_mimes) {
$image_mimes = array('image/jpeg', 'image/png', 'image/gif');
}
$smush_on_create_update = variable_get('_smush_it_on_create_update', false);
if ($smush_on_create_update) {
switch ($op) {
case 'insert':
case 'update':
if (user_access('upload files')) {
foreach ($node->files as &$file) {
if ($file['new'] && in_array($file['filemime'], $image_mimes)) {
smush_it::smush_image($file['fid']);
}
}
}
break;
}
}
}
/**
* Gets the information from the smush.it settings to run the cron job
* if cron_count == 0 or cron_active == false then the cron job will not run
* implementation of hook_cron()
*/
function smush_it_cron() {
$cron_active = variable_get('_smush_it_cron_active', false);
$cron_count = variable_get('_smush_it_cron_count', 0);
if ($cron_active && $cron_count > 0) {
_process_cron($cron_count);
}
}
/**
* Actually run the unprocessed images through smush.it
* @param int $cron_count
*/
function _process_cron($cron_count) {
//select the files where there is no equivalent smush_it entry
$to_process = db_query('SELECT files.fid FROM {files}
LEFT JOIN {smush_it} ON files.fid = smush_it.fid
WHERE smush_it.smushid IS NULL
AND filemime IN (\'image/jpeg\', \'image/png\', \'image/gif\')
LIMIT 0 , %d', $cron_count);
while ($file = db_fetch_array($to_process)) {
smush_it::smush_image($file['fid']);
}
}
/**
* Add the smush.it settings menu item
* implementation of hook_menu()
* @return array
*/
function smush_it_menu() {
//smush it settings
$items['admin/settings/smush-it'] = array(
'title' => 'Smush.it',
'description' => t('The settings for smush.it'),
'page callback' => 'drupal_get_form',
'page arguments' => array('smush_it_settings_page'),
'access callback' => 'user_access',
'access arguments' => array('administer upload'),
'type' => MENU_NORMAL_ITEM,
);
//this seems redundant
$items['admin/settings/smush-it/settings'] = $items['admin/settings/smush-it'];
//replace values that are different for this menu item (it is a tab)
$items['admin/settings/smush-it/settings']['type'] = MENU_DEFAULT_LOCAL_TASK;
$items['admin/settings/smush-it/settings']['weight'] = -10;
$items['admin/settings/smush-it/settings']['title'] = 'Settings';
//file listing
$items['admin/settings/smush-it/files'] = array(
'title' => 'Files',
'access callback' => 'user_access',
'page callback' => 'smush_it_file_listing',
'access arguments' => array('administer upload'),
'type' => MENU_LOCAL_TASK,
'weight' => '0',
);
//stats
if (BC_LOADED) {
$items['admin/settings/smush-it/stats'] = array(
'title' => 'Stats',
'access callback' => 'user_access',
'page callback' => 'smush_it_stats_page',
'access arguments' => array('administer upload'),
'type' => MENU_LOCAL_TASK,
'weight' => '10',
);
}
//smush an individual image
$items['admin/settings/smush-it/smush/%'] = array(
'page callback' => 'smush_it_manually',
'page arguments' => array(4),
'access callback' => 'user_access',
'access arguments' => array('administer upload'),
'type' => MENU_CALLBACK,
);
//revert an image
$items['admin/settings/smush-it/revert/%'] = array(
'page callback' => 'smush_it_revert',
'page arguments' => array(4),
'access callback' => 'user_access',
'access arguments' => array('administer upload'),
'type' => MENU_CALLBACK,
);
//ignore an image
$items['admin/settings/smush-it/ignore/%'] = array(
'page callback' => 'smush_it_ignore',
'page arguments' => array(4),
'access callback' => 'user_access',
'access arguments' => array('administer upload'),
'type' => MENU_CALLBACK,
);
return $items;
}
if (BC_LOADED) {
/**
* The smush.it stats page
* @return string
*/
function smush_it_stats_page() {
$saved_bytes = smush_it::_total_saved();
$smushed_bytes = smush_it::_image_size();
$count_smushed = smush_it::_smushed_count();
$count_images = smush_it::_image_count();
//these numbers could end up being extremely big...
$avg_bytes = 0;
if ($count_smushed > 0) {
$avg_bytes = sprintf('%.0f', bcdiv($saved_bytes, $count_smushed));
}
$original_bytes = sprintf('%.0f', bcadd($smushed_bytes, $saved_bytes));
$count_to_process = sprintf('%.0f', bcsub($count_images, $count_smushed));
$rows[] = array(
'<h4>' . t('Original Image Size:') . '</h4>',
smush_it::byte_format($original_bytes)
);
$rows[] = array(
'<h4>' . t('Bytes Saved:') . '</h4>',
smush_it::byte_format($saved_bytes)
);
$rows[] = array(
'<h4>' . t('Smushed Size:') . '</h4>',
smush_it::byte_format($smushed_bytes)
);
$rows[] = array(
'<h4>' . t('Images Smushed:') . '</h4>',
$count_smushed
);
$rows[] = array(
'<h4>' . t('Average Bytes Saved:') . '</h4>',
smush_it::byte_format($avg_bytes)
);
$rows[] = array(
'<h4>' . t('Images:') . '</h4>',
$count_images
);
$rows[] = array(
'<h4>' . t('Images To Process:') . '</h4>',
$count_to_process
);
$output = theme('table', null, $rows);
return $output;
}
}
/**
* Smush a file manually, then redirect to the files page
* @param int $file_id
*/
function smush_it_manually($file_id, $redirect = true) {
if (smush_it::smush_image($file_id)) {
drupal_set_message($file['filename'] . ' has been smushed.');
}
if ($redirect) {
//@BUG: should go back to previous page the user was on
// ie: admin/settings/smush-it/files?page=10
drupal_goto('admin/settings/smush-it/files');
}
}
/**
* Revert a file, then redirect to the files page
* @param int $file_id
*/
function smush_it_revert($file_id, $redirect = true) {
if (smush_it::revert_image($file_id)) {
drupal_set_message($file['filename'] . " has been reverted to it's original version.");
}
if ($redirect) {
//@BUG: should go back to previous page the user was on
// ie: admin/settings/smush-it/files?page=10
drupal_goto('admin/settings/smush-it/files');
}
}
/**
* Revert a file, then redirect to the files page
* @param int $file_id
*/
function smush_it_ignore($file_id, $redirect = true) {
if (smush_it::ignore_image($file_id)) {
drupal_set_message($file['filename'] . ' will be ignored by cron jobs.');
}
if ($redirect) {
//@BUG: should go back to previous page the user was on
// ie: admin/settings/smush-it/files?page=10
drupal_goto('admin/settings/smush-it/files');
}
}
/**
* Displays the images you have uploaded, lets you smush them one by one if you want to
* @return type
*/
function smush_it_file_listing() {
$header = array(
array(
'data' => t('Filename'),
'field' => 'files.filename',
'sort' => 'asc'
),
array(
'data' => t('Mime'),
'field' => 'files.filemime',
),
array(
'data' => t('Orignal'),
),
array(
'data' => t('Smushed'),
'field' => 'files.filesize',
),
array(
'data' => t('Saved'),
'field' => 'smush_it.space_saved',
),
array('data' => t('Operations'), 'colspan' => '3')
);
$q = 'SELECT files.*, smush_it.space_saved, smush_it.smushid FROM {files}
LEFT JOIN {smush_it} ON files.fid = smush_it.fid
AND filemime IN (\'image/jpeg\', \'image/png\', \'image/gif\')';
$limit = 25;
$q .= tablesort_sql($header);
$result = pager_query($q, $limit);
$table_rows = array();
while ($file = db_fetch_object($result)) {
$row = array(
array('data' => l($file->filename, $file->filepath)),
array('data' => $file->filemime),
array('data' => smush_it::byte_format($file->filesize + $file->space_saved)),
);
if ($file->smushid) {
$row[] = array('data' => smush_it::byte_format($file->filesize));
$row[] = array('data' => smush_it::byte_format($file->space_saved));
} else {
$row[] = '';
$row[] = '';
}
//if the file has not been smushed, this will not be set, so show them a link to smush the file
if (empty($file->smushid)) {
$row[] = array(
'data' => l('smush', "admin/settings/smush-it/smush/$file->fid") . ' | ' . l('ignore', "admin/settings/smush-it/ignore/$file->fid")
);
} else {
$row[] = array('data' => l('revert', "admin/settings/smush-it/revert/$file->fid"));
}
$table_rows[] = $row;
}
$output = '<h3>' . t('Files') . '</h3>';
$tags = array(
'first',
'<prev',
'',
'next>',
'last',
);
$output .= theme('table', $header, $table_rows);
$output .= theme('pager', $tags, $limit);
return $output;
}
/**
* The smush.it admin settings form
* @return array
*/
function smush_it_settings_page() {
//in this example i'm creating a checkbox for each existing content type
$form['cron_count'] = array(
'#type' => 'textfield',
'#title' => t('Number of images processed during cron'),
'#description' => t('If your cron jobs are running too long, make this a smaller number.'),
'#default_value' => variable_get('_smush_it_cron_count', 10)
);
$form['cron_active'] = array(
'#type' => 'checkbox',
'#title' => t('Process images during cron'),
'#description' => t('If you don\'t want smush.it to run when the cron job fires, leave this un-checked.'),
'#default_value' => variable_get('_smush_it_cron_active', false)
);
$form['on_create_update'] = array(
'#type' => 'checkbox',
'#title' => t('Smush files on create/update'),
'#description' => t('If you want the cron job to handle smushing of images, check this off to ignore the create / update node action.'),
'#default_value' => variable_get('_smush_it_on_create_update', false)
);
$form['test_mode'] = array(
'#type' => 'checkbox',
'#title' => t('Testing Mode'),
'#description' => t('If you want to test things locally.'),
'#default_value' => variable_get('_smush_it_testing', false)
);
$form['test_image'] = array(
'#type' => 'textfield',
'#title' => t('Testing Image'),
'#description' => t('The url to an image that you want to test with. Be nice, don\'t eat somone elses bandwidth up. !!Note: Your local images will be replaced by this one.!!'),
'#default_value' => variable_get('_smush_it_test_image', '')
);
$form['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#executes_submit_callback' => true
);
$form['#submit'] = array(
'_smush_it_form_submit'
);
return $form;
}
/**
* Save the submitted values
* @param type $form
* @param type $form_state
*/
function _smush_it_form_submit($form, &$form_state) {
if ($form['cron_active']['#value']) {
variable_set('_smush_it_cron_active', true);
} else {
variable_set('_smush_it_cron_active', false);
}
$cron_count_value = (int) $form['cron_count']['#value'];
if ($cron_count_value && $cron_count_value >= 0) {
variable_set('_smush_it_cron_count', (int) $cron_count_value);
} elseif ($cron_count_value && $cron_count_value < 0) {
form_set_error('', t('Cron count should be an integer >= 0.'));
}
if ($form['test_mode']['#value']) {
variable_set('_smush_it_testing', true);
} else {
variable_set('_smush_it_testing', false);
}
if ($form['on_create_update']['#value']) {
variable_set('_smush_it_on_create_update', true);
} else {
variable_set('_smush_it_on_create_update', false);
}
if ($form['test_image']['#value']) {
variable_set('_smush_it_test_image', $form['test_image']['#value']);
} else {
variable_set('_smush_it_testing', '');
}
drupal_set_message('Smush.it settings updated', 'status');
}