adaptive_image.image.inc 4.0 KB

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