adaptive_image.module 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * Implements hook_init().
  12. */
  13. function adaptive_image_init() {
  14. // According to the documentation of hook_init() it should not be used to
  15. // load JS or CSS. The CSS case has been moved to the info file. But the JS is
  16. // here by intention, as we want it inline to prevent wait time while loading
  17. // the script
  18. // No need for drupal behaviours, jquery compatibility wrapper nor ready event
  19. $js = "document.cookie = 'adaptive_image=' + Math.max(screen.width, screen.height) + '; path=/';";
  20. drupal_add_js($js,
  21. // First-come, first-served
  22. array(
  23. 'type' => 'inline',
  24. 'scope' => 'header',
  25. 'group' => JS_LIBRARY,
  26. 'every_page' => TRUE,
  27. 'weight' => -500,
  28. )
  29. );
  30. }
  31. /**
  32. * Implements hook_menu().
  33. */
  34. function adaptive_image_menu() {
  35. $items = array();
  36. // Add image style generation paths adaptive URLs.
  37. if (module_exists('image')) {
  38. // Generate and deliver image derivatives of public files.
  39. $directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
  40. $items[$directory_path . '/styles/%image_style/adaptive-image'] = array(
  41. 'title' => 'Generate image style',
  42. 'page callback' => 'adaptive_image_style_deliver',
  43. 'page arguments' => array(count(explode('/', $directory_path)) + 1),
  44. 'access callback' => TRUE,
  45. 'type' => MENU_CALLBACK,
  46. 'file' => 'adaptive_image.image.inc',
  47. );
  48. // Generate and deliver image derivatives of private files.
  49. $items['system/files/styles/%image_style/adaptive-image'] = array(
  50. 'title' => 'Generate adaptive image style',
  51. 'page callback' => 'adaptive_image_style_deliver',
  52. 'page arguments' => array(3),
  53. 'access callback' => TRUE,
  54. 'type' => MENU_CALLBACK,
  55. 'file' => 'adaptive_image.image.inc',
  56. );
  57. }
  58. return $items;
  59. }
  60. /**
  61. * Implements hook_image_effect_info().
  62. */
  63. function adaptive_image_image_effect_info() {
  64. $effects = array();
  65. $effects['adaptive_image'] = array(
  66. 'label' => t('Adaptive'),
  67. 'help' => t('Adaptive image scale according to client resolution.'),
  68. 'effect callback' => 'image_scale_effect',
  69. 'dimensions callback' => 'image_scale_dimensions',
  70. 'form callback' => 'adaptive_image_scale_form',
  71. 'summary theme' => 'adaptive_image_scale_summary',
  72. );
  73. return $effects;
  74. }
  75. /**
  76. * Form structure for the image scale form.
  77. *
  78. * Note that this is not a complete form, it only contains the portion of the
  79. * form for configuring the scale options. Therefore it does not not need to
  80. * include metadata about the effect, nor a submit button.
  81. *
  82. * @param $data
  83. * The current configuration for this scale effect.
  84. */
  85. function adaptive_image_scale_form($data) {
  86. $form['resolutions'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('Resolutions'),
  89. '#default_value' => isset($data['resolutions']) ? $data['resolutions'] : '1382, 992, 768, 480',
  90. '#required' => TRUE,
  91. '#description' => t('The resolution break-points to use (screen widths, in pixels).'),
  92. );
  93. $form['mobile_first'] = array(
  94. '#type' => 'checkbox',
  95. '#title' => t('Mobile first'),
  96. '#default_value' => isset($data['mobile_first']) ? $data['mobile_first'] : TRUE,
  97. '#description' => t("Check this to send the smallest version when the resolution can not be determined."),
  98. );
  99. $resolutions = explode(',', str_replace(' ', '', $form['resolutions']['#default_value']));
  100. $resolution = adaptive_image_resolution($resolutions);
  101. // Provide needed defaults
  102. $form['height'] = array('#type' => 'hidden','#default_value' => NULL);
  103. $form['width'] = array('#type' => 'hidden','#default_value' => $resolution);
  104. $form['upscale'] = array('#type' => 'hidden','#default_value' => NULL);
  105. return $form;
  106. }
  107. /**
  108. * Implements hook_theme().
  109. */
  110. function adaptive_image_theme() {
  111. return array(
  112. 'adaptive_image_scale_summary' => array(
  113. 'variables' => array('data' => NULL),
  114. ),
  115. );
  116. }
  117. /**
  118. * Returns HTML for a summary of an image scale effect.
  119. *
  120. * @param $variables
  121. * An associative array containing:
  122. * - data: The current configuration for this scale effect.
  123. *
  124. * @ingroup themeable
  125. */
  126. function theme_adaptive_image_scale_summary($variables) {
  127. $data = $variables['data'];
  128. if ($data['resolutions']) {
  129. return check_plain($data['resolutions']);
  130. }
  131. }
  132. /**
  133. * Implements template_preprocess_image().
  134. *
  135. * Adds a class to adaptive images for max-width.
  136. */
  137. function adaptive_image_preprocess_image(&$variables) {
  138. global $base_url;
  139. if (isset($variables['style_name'])) {
  140. // Get image style settings
  141. $style = image_style_load($variables['style_name']);
  142. // Check if style contains the adaptive image effect
  143. if ($style && adaptive_image_contains_effect($style)) {
  144. $settings = adaptive_image_effect_settings($style);
  145. $resolutions = explode(',', $settings['resolutions']);
  146. $resolution = adaptive_image_resolution($resolutions);
  147. // Only construct direct path if not private
  148. if (!strpos($variables['path'], '/system/') && is_numeric($resolution)) {
  149. $path_parts = pathinfo($variables['path']);
  150. $derivative_uri = $path_parts['dirname'] . '/' . $resolution . '/' . $path_parts['basename'];
  151. }
  152. if (isset($derivative_uri) && file_exists(str_replace($base_url, '.', $derivative_uri))) {
  153. // Deliver existing path to bypass menu callback
  154. $variables['path'] = $derivative_uri;
  155. }
  156. else {
  157. // Reconstruct the image path to %/%style_name/adaptive-image/% to
  158. // trigger image generation or file access check
  159. $variables['path'] = str_replace('styles/' . $variables['style_name'], 'styles/' . $variables['style_name'] . '/adaptive-image', $variables['path']);
  160. }
  161. // Add class for styling
  162. $variables['attributes']['class'] = 'adaptive-image';
  163. // Remove fixed image dimensions
  164. unset($variables['height']);
  165. unset($variables['width']);
  166. }
  167. }
  168. }
  169. /**
  170. * Check for adaptive image effect from style
  171. */
  172. function adaptive_image_contains_effect($style) {
  173. foreach ($style['effects'] as $effect) {
  174. if ($effect['name'] == 'adaptive_image') {
  175. return TRUE;
  176. }
  177. }
  178. return FALSE;
  179. }
  180. /**
  181. * Get adaptive image effect from style settings
  182. */
  183. function adaptive_image_effect_settings($style) {
  184. $settings = array();
  185. foreach ($style['effects'] as $effect) {
  186. if ($effect['name'] == 'adaptive_image') {
  187. $settings = $effect['data'];
  188. }
  189. }
  190. return $settings;
  191. }
  192. /**
  193. * Determine current resolution
  194. */
  195. function adaptive_image_resolution($resolutions) {
  196. $resolution = '';
  197. /* Check to see if a valid cookie exists */
  198. if (count($resolutions) && isset($_COOKIE['adaptive_image'])) {
  199. if (is_numeric($_COOKIE['adaptive_image'])) {
  200. $client_width = (int) $_COOKIE['adaptive_image']; // store the cookie value in a variable
  201. /* the client width in the cookie is valid, now fit that number into the correct resolution break point */
  202. rsort($resolutions); // make sure the supplied break-points are in reverse size order
  203. $resolution = $resolutions[0]; // by default it's the largest supported break-point
  204. foreach ($resolutions as $break_point) { // filter down
  205. if ($client_width <= $break_point) {
  206. $resolution = $break_point;
  207. }
  208. }
  209. } else {
  210. setcookie("adaptive_image", "", time() -1); // delete the mangled cookie
  211. }
  212. }
  213. return $resolution;
  214. }