-
Notifications
You must be signed in to change notification settings - Fork 4
/
adaptive_image.image.inc
127 lines (109 loc) · 3.98 KB
/
adaptive_image.image.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
<?php
/**
* @file
* Adaptive Image - Adaptive images for Backdrop
* @see http://adaptive-images.com/
*
* @author
* Stefan Auditor <[email protected]>
*/
/**
* Copy of image_style_deliver() for use with adaptive images.
*/
function adaptive_image_style_deliver($style, $scheme) {
$settings = $resolutions = array();
$settings = adaptive_image_effect_settings($style);
$mobile_first = (boolean) $settings['mobile_first'];
$resolutions = explode(',', $settings['resolutions']);
/* Do we need to switch mobile first off? */
if (adaptive_image_browser_detect()) {
$mobile_first = FALSE;
}
$resolution = adaptive_image_resolution($resolutions);
/* No resolution was found (no cookie or invalid cookie) */
if (!$resolution && count($resolutions)) {
// We send the lowest resolution for mobile-first approach, and highest otherwise
$resolution = $mobile_first ? min($resolutions) : max($resolutions);
}
// Check that the style is defined and the scheme is valid.
if (!$style || !file_stream_wrapper_valid_scheme($scheme)) {
backdrop_exit();
}
foreach ($style['effects'] as $id => $effect) {
if ($effect['name'] == 'adaptive_image') {
$style['effects'][$id]['data']['width'] = $resolution;
$style['effects'][$id]['data']['height'] = NULL;
break;
}
}
$args = func_get_args();
array_shift($args);
array_shift($args);
$target = implode('/', $args);
$image_uri = $scheme . '://' . $target;
$derivative_uri = image_style_path($style['name'], $image_uri);
if ($resolution) {
$path_parts = pathinfo($derivative_uri);
$derivative_uri = $path_parts['dirname'] . '/' . $resolution . '/' . $path_parts['basename'];
}
// If using the private scheme, let other modules provide headers and
// control access to the file.
if ($scheme == 'private') {
if (file_exists($derivative_uri)) {
file_download($scheme, file_uri_target($derivative_uri));
}
else {
$headers = module_invoke_all('file_download', $image_uri);
if (in_array(-1, $headers) || empty($headers)) {
return backdrop_access_denied();
}
if (count($headers)) {
foreach ($headers as $name => $value) {
backdrop_add_http_header($name, $value);
}
}
}
}
// Don't start generating the image if the derivative already exists or if
// generation is in progress in another thread.
$lock_name = 'image_style_deliver:' . $style['name'] . ':' . backdrop_hash_base64($image_uri);
if (!file_exists($derivative_uri)) {
$lock_acquired = lock_acquire($lock_name);
if (!$lock_acquired) {
// Tell client to retry again in 3 seconds. Currently no browsers are known
// to support Retry-After.
backdrop_add_http_header('Status', '503 Service Unavailable');
backdrop_add_http_header('Retry-After', 3);
print t('Image generation in progress. Try again shortly.');
backdrop_exit();
}
}
// Try to generate the image, unless another thread just did it while we were
// acquiring the lock.
$success = file_exists($derivative_uri) || image_style_create_derivative($style, $image_uri, $derivative_uri);
if (!empty($lock_acquired)) {
lock_release($lock_name);
}
if ($success) {
$image = image_load($derivative_uri);
file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
}
else {
watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $derivative_uri));
backdrop_add_http_header('Status', '500 Internal Server Error');
print t('Error generating image.');
backdrop_exit();
}
}
/**
* Check for common desktop patterns in the user agent
*/
function adaptive_image_browser_detect() {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
// Identify the OS platform. Match only desktop OSs
if (strpos($userAgent,'macintosh') ||
strpos($userAgent,'windows nt') ||
strpos($userAgent,'x11')) {
return TRUE;
}
}