You are here

function collageformatter_render_collage in Collage Formatter 7

Returns renderable array of collages.

2 calls to collageformatter_render_collage()
collageformatter_field_formatter_view in ./collageformatter.module
Implements hook_field_formatter_view().
collageformatter_plugin_style_collage::render in views/collageformatter_plugin_style_collage.inc
Render the display in this style.

File

./collageformatter.module, line 419
Main file for Collage Formatter module.

Code

function collageformatter_render_collage($images, $settings) {
  $collage = array();

  // Remove images to skip.
  if ($settings['images_to_skip']) {
    $images = array_slice($images, $settings['images_to_skip']);
  }

  // Prepare images.
  foreach ($images as $delta => &$image) {
    if (!isset($image['width']) || !isset($image['height'])) {
      if ($image_info = image_get_info($image['uri'])) {
        $image += $image_info;
      }
    }
    $image += array(
      'box_type' => 'image',
      'delta' => $delta,
      'total_width' => $image['width'] + 2 * $settings['border_size'] + $settings['gap_size'],
      'total_height' => $image['height'] + 2 * $settings['border_size'] + $settings['gap_size'],
    );
  }

  // Determine number of collages and how many images per collage.
  $collage_number = $settings['collage_number'];
  $images_per_collage = $settings['images_per_collage'] ? $settings['images_per_collage'] : round(count($images) / $collage_number);

  // Generate collages.
  while ($collage_number > 0) {
    $collage_number--;

    // If last collage and all images option - take all images.
    if ($collage_number == 0 && !$settings['images_per_collage']) {
      $collage_images = $images;
    }
    else {
      $collage_images = array_slice($images, 0, $images_per_collage);

      // Update images array and set as the last collage if there are no more images.
      if (!($images = array_slice($images, $images_per_collage))) {
        $collage_number = 0;
      }
    }

    // Generate collage layout.
    $box = _collageformatter_layout_box($collage_images, $settings['collage_orientation']);

    // Scale the collage.
    if ($settings['collage_width']) {
      $box['parent_total_width'] = $settings['collage_width'] - 2 * $settings['collage_border_size'];
      $dimensions = array(
        'width' => $box['parent_total_width'] - $settings['gap_size'],
      );
      $box = _collageformatter_scale_box($box, $dimensions);
      $box['parent_total_height'] = $box['total_height'] + $settings['gap_size'];
    }
    elseif ($settings['collage_height']) {
      $box['parent_total_height'] = $settings['collage_height'] - 2 * $settings['collage_border_size'];
      $dimensions = array(
        'height' => $box['parent_total_height'] - $settings['gap_size'],
      );
      $box = _collageformatter_scale_box($box, $dimensions);
      $box['parent_total_width'] = $box['total_width'] + $settings['gap_size'];
    }
    else {
      $box['parent_total_width'] = $box['total_width'] + $settings['gap_size'];
      $box['parent_total_height'] = $box['total_height'] + $settings['gap_size'];
    }

    // Resize the collage if both with and height are set.
    if ($settings['collage_width'] && $settings['collage_height']) {
      $box['parent_total_width'] = $settings['collage_width'] - 2 * $settings['collage_border_size'];
      $box['parent_total_height'] = $settings['collage_height'] - 2 * $settings['collage_border_size'];
      $dimensions = array(
        'width' => $box['parent_total_width'] - $settings['gap_size'],
        'height' => $box['parent_total_height'] - $settings['gap_size'],
      );
      $box = _collageformatter_resize_box($box, $dimensions);
    }

    // Check for upscaled images and prevent upscaling.
    if ($settings['prevent_upscale']) {
      $scale = _collageformatter_upscaling_check($box, $settings);
      if ($scale < 1) {
        $dimensions = array(
          'width' => $scale * $box['total_width'],
        );
        $box = _collageformatter_scale_box($box, $dimensions);
        $box['parent_total_width'] = $box['total_width'] + $settings['gap_size'];
        $box['parent_total_height'] = $box['total_height'] + $settings['gap_size'];
      }
    }
    $collage_wrapper_style = array();
    $collage_wrapper_style[] = 'max-width: ' . round($box['parent_total_width'] + 2 * $settings['collage_border_size'] - 0.5) . 'px;';

    // $collage_wrapper_style[] = 'box-sizing: border-box; -webkit-box-sizing: border-box; -moz-box-sizing: border-box;';
    $collage_style = array();

    // $collage_style[] = 'box-sizing: border-box; -webkit-box-sizing: border-box; -moz-box-sizing: border-box;';
    if ($settings['collage_border_size']) {
      $border = 'border: ' . $settings['collage_border_size'] . 'px solid';
      $border .= $settings['collage_border_color'] ? ' ' . $settings['collage_border_color'] : '';
      $collage_style[] = $border . ';';
    }
    if ($settings['gap_color']) {
      $collage_style[] = 'background-color: ' . $settings['gap_color'] . ';';
    }
    $collage_wrapper_class = array(
      'collageformatter-collage-wrapper',
    );
    if ($settings['image_link_modal'] == 'photoswipe') {
      $collage_wrapper_class[] = 'photoswipe-gallery';
    }
    $collage[] = array(
      '#theme' => 'collageformatter_collage',
      '#collage' => _collageformatter_render_box($box, $settings),
      '#collage_wrapper_class' => implode(' ', $collage_wrapper_class),
      '#collage_wrapper_style' => implode(' ', $collage_wrapper_style),
      '#collage_style' => implode(' ', $collage_style),
      '#collage_bottom_style' => 'clear: both; margin-bottom: ' . 100 * ($settings['gap_size'] / round($box['parent_total_width'] - 0.5)) . '%',
    );
  }
  return $collage;
}