image.inc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @file
  4. * Insert support for ImageField module.
  5. */
  6. /**
  7. * Implementation of hook_insert_widgets().
  8. */
  9. function image_insert_widgets() {
  10. return array(
  11. 'image_image' => array(
  12. 'element_type' => 'managed_file',
  13. 'wrapper' => '.image-widget',
  14. 'fields' => array(
  15. 'alt' => 'input[name$="[alt]"], textarea[name$="[alt]"]',
  16. 'title' => 'input[name$="[title]"], textarea[name$="[title]"]',
  17. 'description' => 'input[name$="[description]"], textarea[name$="[description]"]',
  18. ),
  19. ),
  20. );
  21. }
  22. /**
  23. * Implementation of hook_insert_styles().
  24. */
  25. function image_insert_styles() {
  26. $image_styles = image_styles();
  27. $insert_styles = array();
  28. foreach ($image_styles as $style) {
  29. $insert_styles['image_' . $style['name']] = array(
  30. 'label' => t($style['name']),
  31. );
  32. }
  33. return $insert_styles;
  34. }
  35. /**
  36. * Implementation of hook_insert_content().
  37. */
  38. function image_insert_content($item, $style, $widget) {
  39. $style_name = preg_replace('/^image_/', '', $style['name']);
  40. return theme(array('image_insert_image__' . str_replace('-', '_', $style_name), 'image_insert_image'), array('item' => $item, 'widget' => $widget, 'style_name' => $style_name));
  41. }
  42. /**
  43. * Theme the content that will be inserted for Image styles.
  44. */
  45. function template_preprocess_image_insert_image(&$vars) {
  46. $vars['file'] = file_load($vars['item']['fid']);
  47. // Determine dimensions of the image after the image style transformations.
  48. $image_info = @image_get_info($vars['file']->uri);
  49. $vars['width'] = isset($image_info['width']) ? $image_info['width'] : NULL;
  50. $vars['height'] = isset($image_info['height']) ? $image_info['height'] : NULL;
  51. image_style_transform_dimensions($vars['style_name'], $vars);
  52. $vars['uri'] = image_style_path($vars['style_name'], $vars['file']->uri);
  53. $absolute = isset($vars['widget']['settings']['insert_absolute']) ? $vars['widget']['settings']['insert_absolute'] : NULL;
  54. $vars['url'] = insert_create_url($vars['uri'], $absolute, variable_get('clean_url'));
  55. // http://drupal.org/node/1923336
  56. if (function_exists('image_style_path_token')) {
  57. $token_query = array(IMAGE_DERIVATIVE_TOKEN => image_style_path_token($vars['style_name'], $vars['file']->uri));
  58. $vars['url'] .= (strpos($vars['url'], '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
  59. }
  60. $vars['class'] = !empty($vars['widget']['settings']['insert_class']) ? $vars['widget']['settings']['insert_class'] : '';
  61. }