You are here

function textimage_text_to_image in Textimage 5

Same name and namespace in other branches
  1. 5.2 textimage.module \textimage_text_to_image()
  2. 6.2 textimage.module \textimage_text_to_image()
  3. 7.2 textimage.utils.inc \textimage_text_to_image()

Generate an image containing text with the given parameters.

Return value

$image A GD image resource.

1 call to textimage_text_to_image()
textimage_image_from_preset in ./textimage.module
Loads the Text Image preset and generates the GD image resource.

File

./textimage.module, line 1001

Code

function textimage_text_to_image($text, $fontsize, $font, $foreground_color = '#000000', $background_color = NULL, $angle = 0, $maximum_width = 0, $fixed_width = 0, $align = ALIGN_LEFT) {

  // Perform text wrapping, if necessary.
  if ($maximum_width > 0) {
    $text = textimage_wrap_text($text, $fontsize, $font, $maximum_width);
  }

  // Get exact dimensions of text string
  $box = imageTTFBbox($fontsize, $angle, $font, $text);

  // Calculate text width and height
  $iw = abs($box[4] - $box[0]) + 4;
  $ih = abs($box[5] - $box[1]) + 4;
  if ($fixed_width) {
    $background_width = $maximum_width;
  }
  else {
    $background_width = $iw;
  }

  // Use a transparent background color
  if (empty($background_color)) {
    $img = _textimage_create_transparent_image($background_width, $ih);
  }
  else {
    $img = imagecreatetruecolor($background_width, $ih);
    list($r, $g, $b) = _textimage_hex2rgb($background_color);
    $back = imagecolorallocate($img, $r, $g, $b);
    imagefill($img, 0, 0, $back);
  }

  // Create the text image
  list($r, $g, $b) = _textimage_hex2rgb($foreground_color);
  $fore = imagecolorallocate($img, $r, $g, $b);
  switch ($align) {
    case ALIGN_RIGHT:
      $x = $maximum_width - $iw;
      break;
    case ALIGN_CENTER:
      $x = ($maximum_width - $iw) / 2;
      break;
    case ALIGN_LEFT:
    default:
      $x = 0;
      break;
  }
  imagettftext($img, $fontsize, $angle, $x, abs($box[5]), $fore, $font, $text);
  return $img;
}