function textimage_text_to_image in Textimage 6.2
Same name and namespace in other branches
- 5.2 textimage.module \textimage_text_to_image()
- 5 textimage.module \textimage_text_to_image()
- 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 689
Code
function textimage_text_to_image($text, $fontsize, $font, $color = array(
'hex' => '#000000',
'opacity' => '100',
), $angle = 0, $maximum_width = 0, $fixed_width = 0, $align = TEXTIMAGE_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);
// Calculate rotation variables.
$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 fixed and rotated boundry box co-ordinates.
$bbox = _textimage_imagettfbbox($fontsize, $q_angle, $font, $text);
// 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 != TEXTIMAGE_ALIGN_LEFT) {
switch ($align) {
// Set text alignment center
case TEXTIMAGE_ALIGN_CENTER:
$x += ($image_width - $box_width) / 2;
$y += ($image_height - $box_height) / 2;
break;
// Set text alignment right
case TEXTIMAGE_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);
}
// Preserve transparency settings.
imagealphablending($image, TRUE);
imagesavealpha($image, TRUE);
return $image;
}