You are here

function image_effects_text_get_offset in ImageCache Actions 7

Same name and namespace in other branches
  1. 8 image_effects_text/image_effects_text.inc \image_effects_text_get_offset()

Convert a position into an offset in pixels.

Position may be a number of additions and/or subtractions of:

  • An value, positive or negative, in pixels.
  • A, positive or negative, percentage (%). The given percentage of the current dimension will be taken.
  • 1 of the keywords:
    • top: 0
    • bottom: the height of the current image
    • left: 0
    • right: the width of the current image
    • center: 50% (of the current dimension)

Examples: 0, 20, -20, 90%, 33.3% + 10, right, center - 20, 300 - center, bottom - 50. Note: The algorithm will accept many more situations, though the result may be hard to predict.

Parameters

string $position: The string defining the position.

int $width: The length of the horizontal dimension.

int $height: The length of the vertical dimension.

int $length: The length of the current dimension (should be either width or height).

Return value

number The computed offset in pixels.

1 call to image_effects_text_get_offset()
image_effects_text_effect_inc in image_effects_text/image_effects_text.inc
Image effect callback for the text effect.

File

image_effects_text/image_effects_text.inc, line 621

Code

function image_effects_text_get_offset($position, $width, $height, $length) {
  $value = 0;
  $tokens = preg_split('/ *(-|\\+) */', $position, 0, PREG_SPLIT_DELIM_CAPTURE);
  $sign = 1;
  foreach ($tokens as $token) {
    switch ($token) {
      case '+':

        // Ignore, doesn't change the sign
        break;
      case '-':

        // Flip the sign.
        $sign = -$sign;
        break;
      case 'top':
      case 'left':

        // Actually, top and left are a no-op.
        $value += $sign * 0;
        $sign = 1;
        break;
      case 'bottom':

        // Use height of the image, even if this is for the horizontal position.
        $value += $sign * $height;
        $sign = 1;
        break;
      case 'right':

        // Use width of the image, even if this is for the vertical position.
        $value += $sign * $width;
        $sign = 1;
        break;
      case 'center':

        // half the current dimension as provided by $length.
        $value += $sign * $length / 2;
        $sign = 1;
        break;
      default:

        // Value: absolute or percentage
        if (substr($token, -strlen('%')) === '%') {
          $percentage = (double) substr($token, 0, -strlen('%')) / 100.0;
          $value += $sign * ($percentage * $length);
        }
        else {
          $value += $sign * (double) $token;
        }
        $sign = 1;
        break;
    }
  }
  return $value;
}