-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudinary.module
574 lines (513 loc) · 16.9 KB
/
cloudinary.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
/**
* @file
* File for the Cloudinary module.
*/
/**
* Sample file on cloudinary for image effect preview.
*/
define('CLOUDINARY_SAMPLE', 'cloudinary://drupal_sample.png');
/**
* Url prefix of cloudinary preview image.
*/
define('CLOUDINARY_PREVIEW_IMAGE_PREFIX', 'https://res.cloudinary.com/demo/image/upload/');
/**
* Effects for effects param visible states.
*/
define('CLOUDINARY_VISIBLE_STATES_EFFECT', 'sepia,brightness,saturation,hue,oil_paint,vignette,pixelate,pixelate_faces,gradient_fade,blur,tilt_shift,sharpen,unsharp_mask,pixelate_region,red,blue,green,contrast,vibrance,fill_light,blur_region,blur_faces,make_transparent,trim,shadow,colorize');
/**
* Crop mode for gravity visible states.
*/
define('CLOUDINARY_VISIBLE_STATES_CROP', 'fill,crop,thumb,pad,lfill,lpad,mpad');
/**
* Implements hook_theme().
*/
function cloudinary_theme() {
return [
'cloudinary_crop_summary' => [
'variables' => [
'data' => NULL,
],
],
];
}
/**
* Theme function for Cloudinary effect.
*/
function theme_cloudinary_crop_summary($variables) {
$attributes = 'None';
$data = cloudinary_preprocess_transformation($variables['data']);
if (!empty($data)) {
$attribute = [];
foreach ($data as $key => $value) {
if (is_array($value)) {
$value = implode('.', array_filter($value));
}
$attribute[] = "$key: $value";
}
$attributes = implode(', ', $attribute);
}
return t('Image will have cloudinary effects applied with attributes : @attributes', ['@attributes' => $attributes]);
}
/**
* Implements hook_theme_registry_alter().
*/
function cloudinary_theme_registry_alter(&$theme_registry) {
foreach ($theme_registry['image_style_preview']['preprocess functions'] as &$function) {
if ($function == 'template_preprocess_image_style_preview') {
$function = 'cloudinary_preprocess_image_style_preview';
break;
}
}
}
/**
* Returns HTML for a cloudinary preview of an image style.
*
* @ingroup themeable
*/
function cloudinary_preprocess_image_style_preview(&$variables) {
// Style information.
$style = $variables['style'];
$variables['style_id'] = $style->id();
$variables['style_name'] = $style->label();
// Cache bypass token.
$variables['cache_bypass'] = REQUEST_TIME;
// Get sample image path from Cloudinary demo sandbox.
$original = CLOUDINARY_PREVIEW_IMAGE_PREFIX . 'sample.jpg';
// Generate image style for provided sample image.
$data = cloudinary_stream_wrapper_transformation($variables['style_id'], NULL);
$derivative_width = !empty($data['width']) && ($data['width'] > 0) ? $data['width'] : 160;
$derivative_height = !empty($data['height']) && ($data['height'] > 0) ? $data['height'] : 160;
if (empty($data)) {
$data = [];
}
// Unset named transformations since they are specific to Cloudinary accounts,
// and will not work with a demo sandbox.
if (isset($data['transformation'])) {
unset($data['transformation']);
}
$trans = \Cloudinary::generate_transformation_string($data);
$preview = CLOUDINARY_PREVIEW_IMAGE_PREFIX . $trans . '/sample.jpg';
$variables['derivative']['rendered']['#uri'] = $preview;
$variables['derivative']['url'] = file_create_url($preview);
// Sample image info.
$sample_width = 160;
$sample_height = 160;
// Set up original file information.
$variables['original'] = [
'url' => file_create_url($original),
'width' => 864,
'height' => 576,
];
if ($variables['original']['width'] > $variables['original']['height']) {
$variables['preview']['original']['width'] = min($variables['original']['width'], $sample_width);
$variables['preview']['original']['height'] = round($variables['preview']['original']['width'] / $variables['original']['width'] * $variables['original']['height']);
}
else {
$variables['preview']['original']['height'] = min($variables['original']['height'], $sample_height);
$variables['preview']['original']['width'] = round($variables['preview']['original']['height'] / $variables['original']['height'] * $variables['original']['width']);
}
// Set up derivative file information.
$variables['derivative'] = [
'url' => file_create_url($preview),
'width' => $derivative_width,
'height' => $derivative_height,
];
if ($variables['derivative']['width'] > $variables['derivative']['height']) {
$variables['preview']['derivative']['width'] = min($variables['derivative']['width'], $sample_width);
$variables['preview']['derivative']['height'] = round($variables['preview']['derivative']['width'] / $variables['derivative']['width'] * $variables['derivative']['height']);
}
else {
$variables['preview']['derivative']['height'] = min($variables['derivative']['height'], $sample_height);
$variables['preview']['derivative']['width'] = round($variables['preview']['derivative']['height'] / $variables['derivative']['height'] * $variables['derivative']['width']);
}
// Build the preview of the original image.
$variables['original']['rendered'] = [
'#theme' => 'image',
'#uri' => $original,
'#alt' => t('Sample original image'),
'#title' => '',
'#attributes' => [
'width' => $variables['original']['width'],
'height' => $variables['original']['height'],
'style' => 'width: ' . $variables['preview']['original']['width'] . 'px; height: ' . $variables['preview']['original']['height'] . 'px;',
],
];
// Build the preview of the image style derivative. Timestamps are added
// to prevent caching of images on the client side.
$variables['derivative']['rendered'] = [
'#theme' => 'image',
'#uri' => $variables['derivative']['url'] . '?cache_bypass=' . $variables['cache_bypass'],
'#alt' => t('Sample modified image'),
'#title' => '',
'#attributes' => [
'width' => 'auto',
'height' => 'auto',
'style' => 'width: ' . $variables['preview']['derivative']['width'] . 'px; height: ' . $variables['preview']['derivative']['height'] . 'px;',
],
];
}
/**
* Implements hook_cloudinary_stream_wrapper_transformation().
*/
function cloudinary_cloudinary_stream_wrapper_transformation() {
$path = drupal_get_path('module', 'cloudinary');
return [
'cloudinary_crop' => [
'title' => t('Cloudinary crop'),
'callback' => 'cloudinary_transformation_cloudinary_crop',
'file' => $path . '/includes/cloudinary.transformation.cloudinary.inc',
],
'cloudinary_named_transformation' => [
'title' => t('Cloudinary named transformation'),
'callback' => 'cloudinary_transformation_cloudinary_named_transformation',
'file' => $path . '/includes/cloudinary.transformation.cloudinary.inc',
],
'image_crop' => [
'title' => t('Crop'),
'callback' => 'cloudinary_transformation_image_crop',
'file' => $path . '/includes/cloudinary.transformation.drupal.inc',
],
'image_desaturate' => [
'title' => t('Desaturate'),
'callback' => 'cloudinary_transformation_image_desaturate',
],
'image_resize' => [
'title' => t('Resize'),
'callback' => 'cloudinary_transformation_image_resize',
],
'image_rotate' => [
'title' => t('Rotate'),
'callback' => 'cloudinary_transformation_image_rotate',
],
'image_scale' => [
'title' => t('Scale'),
'callback' => 'cloudinary_transformation_image_scale',
],
'image_scale_and_crop' => [
'title' => t('Scale and crop'),
'callback' => 'cloudinary_transformation_image_scale_and_crop',
],
'focal_point_scale_and_crop' => [
'title' => t('Focal Point Scale and Crop'),
'callback' => 'cloudinary_transformation_image_scale_and_crop',
'file' => $path . '/includes/cloudinary.transformation.drupal.inc',
],
];
}
/**
* Convert image effect to special structure for cloudinary style.
*/
function cloudinary_transformation_image($data, $extra = NULL) {
if ($extra) {
if (is_array($extra)) {
$data = array_merge($data, $extra);
}
else {
$data['crop'] = $extra;
}
}
$type = CLOUDINARY_STREAM_WRAPPER_TRANSFORMATION_NEW;
if (isset($data['type'])) {
$type = $data['type'];
unset($data['type']);
}
return ['type' => $type, 'data' => $data];
}
/**
* Ajax callback for cloudinary image preview on effect edit form.
*/
function cloudinary_crop_form_preview_callback($form, $form_state) {
$data = cloudinary_from_image_effect_prepare($form_state['values']['data']);
$form['data']['cloudinary']['preview']['thumbnail']['#markup'] = cloudinary_crop_form_preview($data);
return $form['data']['cloudinary']['preview']['thumbnail'];
}
/**
* Generate cloudinary image preview for effect edit form.
*/
function cloudinary_crop_form_preview($data) {
$filename = 'sample.jpg';
if (isset($data['gravity'])) {
switch ($data['gravity']) {
case 'face':
case 'face:center':
case 'rek_face':
$filename = 'bike.jpg';
break;
case 'faces':
case 'faces:center':
case 'rek_faces':
$filename = 'couple.jpg';
break;
}
}
if (isset($data['effect'])) {
switch ($data['effect']) {
case 'redeye':
case 'rek_redeye':
$filename = 'itaib_redeye_msjmif.jpg';
break;
case 'pixelate_faces':
case 'blur_faces':
$filename = 'couple.jpg';
break;
}
}
$original = CLOUDINARY_PREVIEW_IMAGE_PREFIX . $filename;
$preview = $original;
$data = cloudinary_prepare_transformation($data);
$trans = \Cloudinary::generate_transformation_string($data);
if ($trans) {
$preview = CLOUDINARY_PREVIEW_IMAGE_PREFIX . trim($trans, '/') . '/' . $filename;
}
$styles = ['original' => $original, 'preview' => $preview];
// @FIXME
// theme() has been renamed to _theme() and should NEVER be called directly.
// Calling _theme() directly can alter the expected output and potentially
// introduce security issues (see https://www.drupal.org/node/2195739). You
// should use renderable arrays instead.
//
//
// @see https://www.drupal.org/node/2195739
return _theme('cloudinary_image_style_preview', $styles);
}
/**
* Prepare cloudinary transformation with attributes.
*
* Return multiple transformations if angle set.
*/
function cloudinary_prepare_transformation($data, $only = TRUE) {
$data = cloudinary_preprocess_transformation($data);
// Rotation Angle.
if (isset($data['angle']) || isset($data['angles'])) {
if (isset($data['crop']) && $data['crop'] != 'crop') {
unset($data['crop']);
}
if (isset($data['angle']) && count($data) > 1) {
$angle = $data['angle'];
unset($data['angle']);
if (isset($data['angles'])) {
$data['angle'] = array_values(array_filter($data['angles']));
unset($data['angles']);
}
$new_data = [$data];
$new_data[] = ['angle' => $angle];
if ($only !== TRUE) {
$new_data['multiple'] = TRUE;
}
return $new_data;
}
}
return $data;
}
/**
* Preprocess cloudinary transformation.
*
* Unset empty value fields.
*
* Merge field value to cloudinary format.
*/
function cloudinary_preprocess_transformation($data) {
$data = array_filter($data);
// Merge border with and border color.
if (isset($data['border_width'])) {
$border = $data['border_width'] . 'px_solid_rgb:';
$border_color = '000';
// Check and convert short #FFFFFF syntax to full #FFF syntax.
if (isset($data['border_color'])) {
$bc = $data['border_color'];
$len = strlen($bc);
if ($len == 7 || $len == 4) {
$border_color = substr($bc, 1);
if ($len == 7 && $bc[0] == $bc[1] && $bc[2] == $bc[3] && $bc[4] == $bc[5]) {
$border_color = $bc[0] . $bc[2] . $bc[4];
}
}
}
// Append border color to border.
$border .= $border_color;
$data['border'] = $border;
// Unset unused attributes.
unset($data['border_width'], $data['border_color']);
}
// Meger effect with effects param.
if (isset($data['effect']) && isset($data['effects_param'])) {
$data['effect'] .= ':' . $data['effects_param'];
}
return $data;
}
/**
* Prepare cloudinary image effect before save.
*/
function cloudinary_from_image_effect_prepare($data) {
// Unset border_color if border_width is empty.
if (empty($data['border_width'])) {
$data['border_color'] = '';
}
// Unset angles if automatic_rotation unchecked.
if (empty($data['automatic_rotation'])) {
$data['angles'] = [];
}
// Unset effects_param if effect doesn't have param.
$effect_param_effects = explode(',', CLOUDINARY_VISIBLE_STATES_EFFECT);
if (!in_array($data['effect'], $effect_param_effects)) {
$data['effects_param'] = '';
}
// Unset gravity if mode doesn't have.
$gravity_corp_modes = explode(',', CLOUDINARY_VISIBLE_STATES_CROP);
if (!in_array($data['crop'], $gravity_corp_modes)) {
$data['gravity'] = '';
}
// Unset x,y if crop mode isn't crop.
if ($data['crop'] != 'crop') {
$data['x'] = $data['y'] = '';
}
return $data;
}
/**
* Submit handler for adding a cloudinary effect to an image style.
*/
function cloudinary_form_image_effect_submit($form, &$form_state) {
$form_state['values']['data'] = cloudinary_from_image_effect_prepare($form_state['values']['data']);
}
/**
* Get value from array by key with default value.
*/
function cloudinary_value_get($data, $key, $default = '') {
return isset($data[$key]) ? $data[$key] : $default;
}
/**
* Build options for corp modes.
*/
function _cloudinary_options_crop($key = NULL) {
$data = [
'' => t('None'),
'scale' => t('Scale'),
'limit' => t('Limit'),
'fill' => t('Fill'),
'fit' => t('Fit'),
'crop' => t('Crop'),
'thumb' => t('Thumb'),
'pad' => t('Pad'),
'lfill' => t('Limited Fill'),
'lpad' => t('Limit & Pad'),
'mfit' => t('Fit (Scale Up)'),
'mpad' => t('Pad (No Scale)'),
'imagga_crop' => t('Imagga crop'),
'imagga_scale' => t('Imagga scale'),
];
if (!is_null($key) && isset($data[$key])) {
return $data[$key];
}
return $data;
}
/**
* Build options for gravity.
*/
function _cloudinary_options_gravity($key = NULL) {
$data = [
'' => t('None'),
'face' => t('Face'),
'faces' => t('Faces'),
'north_west' => t('North West'),
'north' => t('North'),
'north_east' => t('North East'),
'east' => t('East'),
'center' => t('Center'),
'west' => t('West'),
'south_west' => t('South West'),
'south' => t('South'),
'south_east' => t('South East'),
'face:center' => t('Face (Center)'),
'faces:center' => t('Faces (Center)'),
'custom' => t('Custom'),
'xy_center' => t('XY Center'),
'rek_face' => t('ReKognition: Face'),
'rek_faces' => t('ReKognition: Faces'),
'rek_eyes' => t('ReKognition: Eyes'),
];
if (!is_null($key) && isset($data[$key])) {
return $data[$key];
}
return $data;
}
/**
* Build options for angles.
*/
function _cloudinary_options_angles($key = NULL) {
$data = [
'auto_left' => t('Auto left'),
'auto_right' => t('Auto right'),
'exif' => t('Use EXIF data'),
'hflip' => t('Horizontal flip'),
'ignore' => t('Ignore'),
'vflip' => t('Vertical flip'),
];
if (!is_null($key) && isset($data[$key])) {
return $data[$key];
}
return $data;
}
/**
* Build options for effect.
*/
function _cloudinary_options_effect($key = NULL) {
$data = [
'' => t('None'),
'grayscale' => t('Grayscale'),
'blackwhite' => t('Blackwhite'),
'sepia' => t('Sepia'),
'brightness' => t('Brightness'),
'saturation' => t('Saturation'),
'hue' => t('Hue'),
'oil_paint' => t('Oil paint'),
'vignette' => t('Vignette'),
'pixelate' => t('Pixelate'),
'pixelate_faces' => t('Pixelate faces'),
'gradient_fade' => t('Gradient fade'),
'blur' => t('Blur'),
'improve' => t('Improve'),
'tilt_shift' => t('Tilt shift'),
'sharpen' => t('Sharpen'),
'unsharp_mask' => t('Unsharp mask'),
'pixelate_region' => t('Pixelate region'),
'red' => t('Red'),
'blue' => t('Blue'),
'green' => t('Green'),
'contrast' => t('Contrast'),
'vibrance' => t('Vibrance'),
'auto_color' => t('Auto color'),
'auto_brightness' => t('Auto brightness'),
'auto_contrast' => t('Auto contrast'),
'fill_light' => t('Fill light'),
'blur_region' => t('Blur region'),
'blur_faces' => t('Blur faces'),
'make_transparent' => t('Make transparent'),
'trim' => t('Trim'),
'mask' => t('Mask'),
'shadow' => t('Shadow'),
'negate' => t('Negate'),
'screen' => t('Screen'),
'multiply' => t('Multiply'),
'colorize' => t('Colorize'),
'redeye' => t('Remove red eyes'),
'rek_redeye' => t('ReKognition: Remove red eyes'),
'overlay' => t('Overlay'),
'gamma' => t('Gamma'),
];
if (!is_null($key) && isset($data[$key])) {
return $data[$key];
}
return $data;
}
/**
* Build group conditions of visible states.
*/
function _cloudinary_build_visible_states($visible) {
$data = [];
$datas = explode(',', $visible);
foreach ($datas as $value) {
$data[] = ['value' => $value];
}
return $data;
}