Photo / Image Collage Formatter

Public

This is an image field formatter which builds an image collage using a predefined template for six images. Example: http://customformatters.com/sites/customformatters.com/files/styles/form...

Put this code into your custom module.

The code is taken from custom formatters project page: http://customformatters.com/formatters/12/collageformatter

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_field_formatter_info().
  3.  */
  4. function MYMODULE_field_formatter_info() {
  5. return array(
  6. 'MYMODULE_collage_formatter' => array(
  7. 'label' => t('Collage formatter'),
  8. 'field types' => array('image'),
  9. ),
  10. );
  11. }
  12. /**
  13.  * Implements hook_field_formatter_view().
  14.  */
  15. function MYMODULE_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display) {
  16. $element = array();
  17. // Build variables array for formatter.
  18. $variables = array(
  19. '#obj_type' => $obj_type,
  20. '#object' => $object,
  21. '#field' => $field,
  22. '#instance' => $instance,
  23. '#langcode' => $langcode,
  24. '#items' => $items,
  25. '#display' => $display,
  26. );
  27. if (function_exists($function = "{$display['module']}_field_formatter_{$display['type']}")) {
  28. $element[0] = array(
  29. '#markup' => $function($variables),
  30. );
  31. }
  32. return $element;
  33. }
  34. /**
  35.  * Field Formatter; Collage formatter.
  36.  */
  37. function MYMODULE_field_formatter_MYMODULE_collage_formatter($variables) {
  38. // Collage template: Width, Height, Top, Left
  39. $template = array(
  40. array(378, 92, 0, 0),
  41. array(184, 92, 0, 103),
  42. array(184, 92, 0, 204),
  43. array(184, 92, 388, 204),
  44. array(184, 194, 194, 103),
  45. array(184,194, 388, 0),
  46. );
  47.  
  48. list($entity_id, $vid, $bundle_name) = entity_extract_ids($variables['#obj_type'], $variables['#object']);
  49. $collage_id = "{$variables['#obj_type']}-{$bundle_name}-{$entity_id}";
  50.  
  51. $output = '';
  52. $delta = 0;
  53. for ($index = 0; $index < 6; $index++) {
  54. $delta = isset($variables['#items'][$delta]) ? $delta : 0;
  55.  
  56. $image = image_load($variables['#items'][$delta]['uri']);
  57. image_effect_apply($image, array(
  58. 'effect callback' => 'image_scale_and_crop_effect',
  59. 'data' => array(
  60. 'width' => $template[$index][0],
  61. 'height' => $template[$index][1],
  62. ),
  63. ));
  64.  
  65. $directory = "public://collage_formatter/{$collage_id}";
  66. if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
  67. image_save($image, "{$directory}/{$index}.png");
  68. $output .= theme('image', array('path' => "{$directory}/{$index}.png", 'attributes' => array('style' => "position: absolute; left: {$template[$index][2]}px; top: {$template[$index][3]}px;")));
  69. }
  70.  
  71. $delta++;
  72. }
  73.  
  74. return "<div style='position: relative; height: 296px; width: 572px;'>{$output}</div>";
  75. }