1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Copy of image_style_deliver() for use with remote images.
|
5
|
*/
|
6
|
function remote_stream_wrapper_image_style_deliver($style, $scheme) {
|
7
|
$args = func_get_args();
|
8
|
array_shift($args);
|
9
|
array_shift($args);
|
10
|
$target = implode('/', $args);
|
11
|
|
12
|
$image_uri = $scheme . '://' . $target;
|
13
|
$derivative_uri = remote_stream_wrapper_image_style_path($style['name'], $image_uri);
|
14
|
|
15
|
// Prevent only the remote files that exist in {file_managed} from having
|
16
|
// image style derivatives generated. Otherwise this could open up the site
|
17
|
// to allowing any remote file to be processed.
|
18
|
if (!remote_stream_wrapper_file_load_by_uri($image_uri)) {
|
19
|
return MENU_ACCESS_DENIED;
|
20
|
}
|
21
|
|
22
|
// Don't start generating the image if the derivative already exists or if
|
23
|
// generation is in progress in another thread.
|
24
|
$lock_name = 'image_style_deliver:' . $style['name'] . ':' . drupal_hash_base64($image_uri);
|
25
|
if (!is_file($derivative_uri)) {
|
26
|
$lock_acquired = lock_acquire($lock_name);
|
27
|
if (!$lock_acquired) {
|
28
|
// Tell client to retry again in 3 seconds. Currently no browsers are known
|
29
|
// to support Retry-After.
|
30
|
drupal_add_http_header('Status', '503 Service Unavailable');
|
31
|
drupal_add_http_header('Retry-After', 3);
|
32
|
print t('Image generation in progress. Try again shortly.');
|
33
|
drupal_exit();
|
34
|
}
|
35
|
}
|
36
|
|
37
|
// Try to generate the image, unless another thread just did it while we were
|
38
|
// acquiring the lock.
|
39
|
$success = is_file($derivative_uri) || image_style_create_derivative($style, $image_uri, $derivative_uri);
|
40
|
|
41
|
if (!empty($lock_acquired)) {
|
42
|
lock_release($lock_name);
|
43
|
}
|
44
|
|
45
|
if ($success) {
|
46
|
$image = image_load($derivative_uri);
|
47
|
file_transfer($image->source, array('Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size']));
|
48
|
}
|
49
|
else {
|
50
|
watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $derivative_uri));
|
51
|
drupal_add_http_header('Status', '500 Internal Server Error');
|
52
|
print t('Error generating image.');
|
53
|
drupal_exit();
|
54
|
}
|
55
|
}
|