You are here

function CollageFormatter::collageformatter_scale_box in Collage Formatter 8

Recursive function to scale the box using only one dimension.

Parameters

$box: array - array of images boxed as configured

  • @param $dimensions array - array containing dimensions i.e. - width and height (if any) of the box
2 calls to CollageFormatter::collageformatter_scale_box()
CollageFormatter::collageformatter_layout_box in src/Plugin/Field/FieldFormatter/CollageFormatter.php
Recursive function to build the layout.
CollageFormatter::collageformatter_render_collage in src/Plugin/Field/FieldFormatter/CollageFormatter.php
Returns renderable array of collages.

File

src/Plugin/Field/FieldFormatter/CollageFormatter.php, line 649
Contains \Drupal\collageformatter\src\Plugin\Field\FieldFormatter\CollageFormatter.

Class

CollageFormatter
Plugin implementation of the 'collageformatter' formatter.

Namespace

Drupal\collageformatter\Plugin\Field\FieldFormatter

Code

function collageformatter_scale_box($box, $dimensions) {

  // If it is an image - just scale it (change dimensions).
  if ($box['box_type'] == 'image') {
    if (array_key_exists('width', $dimensions)) {
      $box['total_height'] = $dimensions['width'] / $box['total_width'] * $box['total_height'];
      $box['total_width'] = $dimensions['width'];
    }
    elseif (array_key_exists('height', $dimensions)) {
      $box['total_width'] = $dimensions['height'] / $box['total_height'] * $box['total_width'];
      $box['total_height'] = $dimensions['height'];
    }
    return $box;
  }

  // If it is a box - then it should consist of two box elements;
  // Determine sizes of elements and scale them.
  if (array_key_exists('width', $dimensions)) {

    // Vertical box type; horizontal contact.
    if ($box['box_orientation'] == 'vertical') {
      $dimensions1 = $dimensions2 = $dimensions;
    }
    elseif ($box['box_orientation'] == 'horizontal') {
      $dimensions1 = [
        'width' => $box[1]['total_width'] / ($box[1]['total_width'] + $box[2]['total_width']) * $dimensions['width'],
      ];
      $dimensions2 = [
        'width' => $box[2]['total_width'] / ($box[1]['total_width'] + $box[2]['total_width']) * $dimensions['width'],
      ];
    }
  }
  elseif (array_key_exists('height', $dimensions)) {

    // Vertical box type; horizontal contact.
    if ($box['box_orientation'] == 'vertical') {
      $dimensions1 = [
        'height' => $box[1]['total_height'] / ($box[1]['total_height'] + $box[2]['total_height']) * $dimensions['height'],
      ];
      $dimensions2 = [
        'height' => $box[2]['total_height'] / ($box[1]['total_height'] + $box[2]['total_height']) * $dimensions['height'],
      ];
    }
    elseif ($box['box_orientation'] == 'horizontal') {
      $dimensions1 = $dimensions2 = $dimensions;
    }
  }
  $box[1] = $this
    ->collageformatter_scale_box($box[1], $dimensions1);
  $box[2] = $this
    ->collageformatter_scale_box($box[2], $dimensions2);
  if ($box['box_orientation'] == 'vertical') {
    $box['total_height'] = $box[1]['total_height'] + $box[2]['total_height'];
    $box['total_width'] = $box[1]['total_width'];
  }
  elseif ($box['box_orientation'] == 'horizontal') {
    $box['total_width'] = $box[1]['total_width'] + $box[2]['total_width'];
    $box['total_height'] = $box[1]['total_height'];
  }
  $box[1]['parent_total_width'] = $box[2]['parent_total_width'] = $box['total_width'];
  $box[1]['parent_total_height'] = $box[2]['parent_total_height'] = $box['total_height'];
  $box[1]['siblings_total_width'] = $box[2]['total_width'];
  $box[1]['siblings_total_height'] = $box[2]['total_height'];
  $box[2]['siblings_total_width'] = $box[1]['total_width'];
  $box[2]['siblings_total_height'] = $box[1]['total_height'];
  return $box;
}