You are here

function imagecache_actions_calculate_offset in ImageCache Actions 8

Same name and namespace in other branches
  1. 6.2 utility.inc \imagecache_actions_calculate_offset()
  2. 7 utility.inc \imagecache_actions_calculate_offset()

Calculates an offset from an edge.

Positive numbers are IN from the edge, negative offsets are OUT.

Examples:

  • left, 20, 200, 100 = 20
  • right, 20, 200, 100 = 80 (object 100 wide placed 20px from the right)
  • top, 50%, 200, 100 = 50 (Object is centered when using %)
  • top, 20%, 200, 100 = -10
  • bottom, -20, 200, 100 = 220
  • right, -25%, 200, 100 = 200 (this ends up just off screen)

Also, the value can be a string, eg "bottom-100", or "center+25%"

Parameters

string $keyword: The edge to calculate the offset from. Can be one of: left, right, top, bottom, middle, center.

string $value: The value to offset with: can be:

  • a numeric value: offset in pixels
  • a percentage value: offset with a percentage of the $base_size.
  • a keyword indicating a pxiel size: left, right, top, bottom, middle, center.
  • an expression of the format: keyword +|- value[%], e.g. center+25%.

int $base_size: The size of the dimension. Used to calculate percentages of.

int $layer_size: The size of the canvas in the current dimension. The value to take for $keyword will be based on this value.

Return value

int

2 calls to imagecache_actions_calculate_offset()
imagecache_actions_calculate_relative_position in ./utility.inc
Given two imageapi objects with dimensions, and some positioning values, calculate a new x,y for the layer to be placed at.
imagecache_actions_keyword_filter in ./utility.inc
Accept a keyword (center, top, left, etc) and return it as an offset in pixels. Called on either the x or y values.

File

./utility.inc, line 310
utility.inc: uitility form, conversion and rendering functions for image processing.

Code

function imagecache_actions_calculate_offset($keyword, $value, $base_size, $layer_size) {
  $offset = 0;

  // Used to account for dimensions of the placed object.
  $direction = 1;
  $base = 0;
  if ($keyword == 'right' || $keyword == 'bottom') {
    $direction = -1;
    $offset = -1 * $layer_size;
    $base = $base_size;
  }
  if ($keyword == 'middle' || $keyword == 'center') {
    $base = $base_size / 2;
    $offset = -1 * ($layer_size / 2);
  }

  // Keywords may be used to stand in for numeric values.
  switch ($value) {
    case 'left':
    case 'top':
      $value = 0;
      break;
    case 'middle':
    case 'center':
      $value = $base_size / 2;
      break;
    case 'bottom':
    case 'right':
      $value = $base_size;
  }

  // Handle keyword-number cases like top+50% or bottom-100px,
  // @see imagecache_actions_keyword_filter().
  if (preg_match('/^([a-z]+) *([+-]) *(\\d+)((px|%)?)$/', $value, $results)) {
    list(, $value_key, $value_mod, $mod_value, $mod_unit) = $results;
    if ($mod_unit == '%') {
      $mod_value = $mod_value / 100 * $base_size;
    }
    $mod_direction = $value_mod == '-' ? -1 : +1;
    switch ($value_key) {
      case 'left':
      case 'top':
      default:
        $mod_base = 0;
        break;
      case 'middle':
      case 'center':
        $mod_base = $base_size / 2;
        break;
      case 'bottom':
      case 'right':
        $mod_base = $base_size;
        break;
    }
    $modified_value = $mod_base + $mod_direction * $mod_value;
    return $modified_value;
  }

  // Handle % values.
  if (substr($value, strlen($value) - 1, 1) == '%') {
    $value = intval($value / 100 * $base_size);
    $offset = -1 * ($layer_size / 2);
  }
  $value = $base + $direction * $value;

  // Add any extra offset to position the item.
  return $value + $offset;
}