You are here

function textimage_text_to_image in Textimage 5.2

Same name and namespace in other branches
  1. 5 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 Textimage preset and generates the GD image resource.

File

./textimage.module, line 662

Code

function textimage_text_to_image($text, $fontsize, $font, $color = array(
  'hex' => '#000000',
  'opacity' => '100',
), $angle = 0, $maximum_width = 0, $fixed_width = 0, $align = ALIGN_LEFT) {

  // Set rotation angle.
  $q_angle = -$angle;
  while ($q_angle > 0 || $q_angle <= -90) {
    $q_angle -= $q_angle > 0 ? 90 : -90;
  }
  while ($angle < 0 || $angle >= 360) {
    $angle += $angle < 0 ? 360 : -360;
  }
  $rotation = -(floor($angle / 90) * 90);
  $rad = deg2rad($q_angle);
  $sin = -sin($rad);
  $cos = cos($rad);

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

  // Get co-ordinates of text string and boundry box.
  $coords = imagettfbbox($fontsize, 0, $font, $text);

  // Set consistant heights.
  if ($coords[3] - $coords[5] < $fontsize * 2) {
    $characters = drupal_map_assoc(range(33, 122), 'chr');
    $characters = join($characters);
    $testcoords = imagettfbbox($fontsize, 0, $font, $characters);
    if ($coords[3] - $coords[5] < $testcoords[3] - $testcoords[5]) {
      $coords[1] = $testcoords[1];
      $coords[3] = $testcoords[3];
      $coords[5] = $testcoords[5];
      $coords[7] = $testcoords[7];
    }
  }

  // Fix boundary box.
  $bbox = array();
  for ($i = 0; $i < 7; $i += 2) {
    $bbox[$i] = round($coords[$i] * $cos + $coords[$i + 1] * -$sin);
    $bbox[$i + 1] = round($coords[$i + 1] * $cos - $coords[$i] * -$sin);
  }

  // Calculate dimensions of text box.
  $text_width = sqrt(pow(abs($bbox[0] - $bbox[2]), 2) + pow(abs($bbox[1] - $bbox[3]), 2));
  $text_height = sqrt(pow(abs($bbox[0] - $bbox[6]), 2) + pow(abs($bbox[1] - $bbox[7]), 2));

  // Not used.
  // Calculate dimensions of box from text box.
  $box_width = max($bbox[0], $bbox[2], $bbox[4], $bbox[6]) - min($bbox[0], $bbox[2], $bbox[4], $bbox[6]);
  $box_height = max($bbox[1], $bbox[3], $bbox[5], $bbox[7]) - min($bbox[1], $bbox[3], $bbox[5], $bbox[7]);

  // Calculate dimensions of image.
  $image_width = $fixed_width && $maximum_width > 0 ? ($maximum_width - 1) * $cos + $text_height * $sin : $box_width;
  $image_height = $fixed_width && $maximum_width > 0 ? ($maximum_width - 1) * $sin + $text_height * $cos : $box_height;

  // Create Image.
  $image = imagecreatetruecolor($image_width + 1, $image_height + 1);
  $back = imagecolorallocatealpha($image, 0, 0, 0, 127);
  imagefill($image, 0, 0, $back);

  // Set text alignment left
  $x = -$bbox[0];
  $y = $box_height - $bbox[3];
  if ($fixed_width && $maximum_width > 0 && $align !== ALIGN_LEFT) {
    switch ($align) {

      // Set text alignment center
      case ALIGN_CENTER:
        $x += ($image_width - $box_width) / 2;
        $y += ($image_height - $box_height) / 2;
        break;

      // Set text alignment right
      case ALIGN_RIGHT:
        $x += $image_width - $box_width;
        $y += $image_height - $box_height;
        break;
    }
  }

  // Create the textimage.
  list($r, $g, $b) = _textimage_hex2rgb($color['hex']);
  $alpha = -($color['opacity'] - 100) / 100 * 127;
  $fore = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
  imagettftext($image, $fontsize, $q_angle, $x, $y, $fore, $font, $text);
  if ($rotation != 0) {
    $image = imagerotate($image, $rotation, 0);
  }
  imagealphablending($image, TRUE);
  imagesavealpha($image, TRUE);
  return $image;
}