You are here

function imagecache_actions_calculate_offset in ImageCache Actions 6.2

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

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

$keyword, $value, $base_size, $layer_size eg left,20 200, 100 = 20 right,20 200, 100 = 80 (object 100 wide placed 20 px from the right = x=80)

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 offscreen)

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

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 69
Utility form, conversion and rendering functions for image processes

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
  // @see imagecache_actions_keyword_filter
  // top+50% or bottom-100px
  if (preg_match('/^(.+)([\\+\\-])(\\d+)([^\\d]*)$/', $value, $results)) {
    list($match, $value_key, $value_mod, $mod_value, $mod_unit) = $results;
    if ($mod_unit == '%') {
      $mod_value = $mod_value / 100 * $base_size;
      $mod_unit = 'px';
    }
    $mod_direction = $value_mod == '-' ? -1 : +1;
    switch ($value_key) {
      case 'left':
      case 'top':
        $mod_base = 0;
        break;
      case 'middle':
      case 'center':
        $mod_base = $base_size / 2;
        break;
      case 'bottom':
      case 'right':
        $mod_base = $base_size;
    }
    $modified_value = $mod_base + $mod_direction * $mod_value;
    return $modified_value;
  }

  #dpm(get_defined_vars());

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

  #dpm(__FUNCTION__ . " Placing an object $layer_size big on a range of $base_size at a position of $value , $offset");

  #dpm(get_defined_vars());

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