You are here

function textactions_create_font_image in ImageCache Actions 5.2

Same name and namespace in other branches
  1. 5.3 textactions.inc \textactions_create_font_image()
  2. 6 textactions.inc \textactions_create_font_image()

Creates an image containing only the text - used to calculate the true bounding box.

1 call to textactions_create_font_image()
textactions_text2canvas_image in ./textactions.inc
Place the source image on the current background

File

./textactions.inc, line 250

Code

function textactions_create_font_image($size, $angle, $font, $char) {
  $rect = imagettfbbox($size, 0, $font, $char);
  if (!$rect) {
    return NULL;
  }
  if (0 == $angle) {
    $imh = $rect[1] - $rect[7];
    $imw = $rect[2] - $rect[0];
    $bx = -1 - $rect[0];
    $by = -1 - $rect[7];
  }
  else {
    $rad = deg2rad($angle);
    $sin = sin($rad);
    $cos = cos($rad);
    if ($angle > 0) {
      $tmp = $rect[6] * $cos + $rect[7] * $sin;
      $bx = -1 - round($tmp);
      $imw = round($rect[2] * $cos + $rect[3] * $sin - $tmp);
      $tmp = $rect[5] * $cos - $rect[4] * $sin;
      $by = -1 - round($tmp);
      $imh = round($rect[1] * $cos - $rect[0] * $sin - $tmp);
    }
    else {
      $tmp = $rect[0] * $cos + $rect[1] * $sin;
      $bx = -1 - round($tmp);
      $imw = round($rect[4] * $cos + $rect[5] * $sin - $tmp);
      $tmp = $rect[7] * $cos - $rect[6] * $sin;
      $by = -1 - round($tmp);
      $imh = round($rect[3] * $cos - $rect[2] * $sin - $tmp);
    }
  }
  return array(
    'width' => $imw,
    'height' => $imh,
    'bx' => $bx,
    'by' => $by,
  );
}