1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Adaptive Image - Adaptive images for Drupal
|
6
|
* @see http://adaptive-images.com/
|
7
|
*
|
8
|
* @author
|
9
|
* Stefan Auditor <stefan.auditor@erdfisch.de>
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Copy of image_style_deliver() for use with adaptive images.
|
14
|
*/
|
15
|
function adaptive_image_style_deliver($style, $scheme) {
|
16
|
$settings = $resolutions = array();
|
17
|
$settings = adaptive_image_effect_settings($style);
|
18
|
|
19
|
$mobile_first = (boolean) $settings['mobile_first'];
|
20
|
$resolutions = explode(',', $settings['resolutions']);
|
21
|
|
22
|
/* Do we need to switch mobile first off? */
|
23
|
if (adaptive_image_browser_detect()) {
|
24
|
$mobile_first = FALSE;
|
25
|
}
|
26
|
|
27
|
$resolution = adaptive_image_resolution($resolutions);
|
28
|
|
29
|
/* No resolution was found (no cookie or invalid cookie) */
|
30
|
if (!$resolution && count($resolutions)) {
|
31
|
// We send the lowest resolution for mobile-first approach, and highest otherwise
|
32
|
$resolution = $mobile_first ? min($resolutions) : max($resolutions);
|
33
|
}
|
34
|
|
35
|
// Check that the style is defined and the scheme is valid.
|
36
|
if (!$style || !file_stream_wrapper_valid_scheme($scheme)) {
|
37
|
drupal_exit();
|
38
|
}
|
39
|
|
40
|
foreach ($style['effects'] as $id => $effect) {
|
41
|
if ($effect['name'] == 'adaptive_image') {
|
42
|
$style['effects'][$id]['data']['width'] = $resolution;
|
43
|
$style['effects'][$id]['data']['height'] = NULL;
|
44
|
break;
|
45
|
}
|
46
|
}
|
47
|
|
48
|
$args = func_get_args();
|
49
|
array_shift($args);
|
50
|
array_shift($args);
|
51
|
$target = implode('/', $args);
|
52
|
|
53
|
$image_uri = $scheme . '://' . $target;
|
54
|
$derivative_uri = image_style_path($style['name'], $image_uri);
|
55
|
|
56
|
if ($resolution) {
|
57
|
$path_parts = pathinfo($derivative_uri);
|
58
|
$derivative_uri = $path_parts['dirname'] . '/' . $resolution . '/' . $path_parts['basename'];
|
59
|
}
|
60
|
|
61
|
// If using the private scheme, let other modules provide headers and
|
62
|
// control access to the file.
|
63
|
if ($scheme == 'private') {
|
64
|
if (file_exists($derivative_uri)) {
|
65
|
file_download($scheme, file_uri_target($derivative_uri));
|
66
|
}
|
67
|
else {
|
68
|
$headers = module_invoke_all('file_download', $image_uri);
|
69
|
if (in_array(-1, $headers) || empty($headers)) {
|
70
|
return drupal_access_denied();
|
71
|
}
|
72
|
if (count($headers)) {
|
73
|
foreach ($headers as $name => $value) {
|
74
|
drupal_add_http_header($name, $value);
|
75
|
}
|
76
|
}
|
77
|
}
|
78
|
}
|
79
|
|
80
|
// Don't start generating the image if the derivative already exists or if
|
81
|
// generation is in progress in another thread.
|
82
|
$lock_name = 'image_style_deliver:' . $style['name'] . ':' . drupal_hash_base64($image_uri);
|
83
|
if (!file_exists($derivative_uri)) {
|
84
|
$lock_acquired = lock_acquire($lock_name);
|
85
|
if (!$lock_acquired) {
|
86
|
// Tell client to retry again in 3 seconds. Currently no browsers are known
|
87
|
// to support Retry-After.
|
88
|
drupal_add_http_header('Status', '503 Service Unavailable');
|
89
|
drupal_add_http_header('Retry-After', 3);
|
90
|
print t('Image generation in progress. Try again shortly.');
|
91
|
drupal_exit();
|
92
|
}
|
93
|
}
|
94
|
|
95
|
// Try to generate the image, unless another thread just did it while we were
|
96
|
// acquiring the lock.
|
97
|
$success = file_exists($derivative_uri) || image_style_create_derivative($style, $image_uri, $derivative_uri);
|
98
|
|
99
|
if (!empty($lock_acquired)) {
|
100
|
lock_release($lock_name);
|
101
|
}
|
102
|
|
103
|
if ($success) {
|
104
|
$image = image_load($derivative_uri);
|
105
|
file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
|
106
|
}
|
107
|
else {
|
108
|
watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $derivative_uri));
|
109
|
drupal_add_http_header('Status', '500 Internal Server Error');
|
110
|
print t('Error generating image.');
|
111
|
drupal_exit();
|
112
|
}
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Check for common desktop patterns in the user agent
|
117
|
*/
|
118
|
function adaptive_image_browser_detect() {
|
119
|
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
|
120
|
|
121
|
// Identify the OS platform. Match only desktop OSs
|
122
|
if (strpos($userAgent,'macintosh') ||
|
123
|
strpos($userAgent,'windows nt') ||
|
124
|
strpos($userAgent,'x11')) {
|
125
|
return TRUE;
|
126
|
}
|
127
|
}
|