You are here

function imagecache_actions_calculate_relative_position in ImageCache Actions 8

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

Given two imageapi objects with dimensions, and some positioning values, calculate a new x,y for the layer to be placed at.

This is a different approach to imagecache_actions_keyword_filter() - more like css.

The $style is an array, and expected to have 'top, bottom, left, right' attributes set. These values may be positive, negative, or in %.

% is calculated relative to the base image dimensions. Using % requires that the layer is positioned CENTERED on that location, so some offsets are added to it. 'right-25%' is not lined up with a margin 25% in, it's centered at a point 25% in - which is therefore identical with left+75%

Parameters

$base:

$layer:

$style:

Return value

array A keyed array of absolute x,y co-ordinates to place the layer at.

1 call to imagecache_actions_calculate_relative_position()
imagecache_testsuite_positioning in tests/imagecache_testsuite.module
Display a page demonstrating a number of positioning tests
2 string references to 'imagecache_actions_calculate_relative_position'
imagecache_canvasactions.module in canvasactions/imagecache_canvasactions.module
imagecache_coloractions.module in coloractions/imagecache_coloractions.module

File

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

Code

function imagecache_actions_calculate_relative_position($base, $layer, $style) {

  // Both images should now have size info available.
  if (isset($style['bottom'])) {
    $ypos = imagecache_actions_calculate_offset('bottom', $style['bottom'], $base->info['height'], $layer->info['height']);
  }
  if (isset($style['top'])) {
    $ypos = imagecache_actions_calculate_offset('top', $style['top'], $base->info['height'], $layer->info['height']);
  }
  if (isset($style['right'])) {
    $xpos = imagecache_actions_calculate_offset('right', $style['right'], $base->info['width'], $layer->info['width']);
  }
  if (isset($style['left'])) {
    $xpos = imagecache_actions_calculate_offset('left', $style['left'], $base->info['width'], $layer->info['width']);
  }
  if (!isset($ypos)) {

    // Assume center.
    $ypos = $base->info['height'] / 2 - $layer->info['height'] / 2;
  }
  if (!isset($xpos)) {

    // Assume center.
    $xpos = $base->info['width'] / 2 - $layer->info['width'] / 2;
  }

  // dpm(__FUNCTION__ . " Calculated offsets");
  // dpm(get_defined_vars());
  return array(
    'x' => $xpos,
    'y' => $ypos,
  );
}