×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Using Two Truetype Fonts to Create Image

Using Two Truetype Fonts to Create Image

Using Two Truetype Fonts to Create Image

(OP)
Some time ago I created a function for building font-based images on one of my sites and it is working fine but now I need to repurpose it for another site that sometimes needs to use two different fonts to create the image.

Using the GD library (Imagick is not available), can someone help me to add a second Truetype font to it so that a single image is created using both? Here is the existing single-font function which also calls other custom functions to provide drop-shadows etc.

CODE --> PHP

function textimage($String,$FontName1,$FontName2,$FontSize,$Angle,$EnableShadow,$CenterAlign) {
	global $Height;
	$FontPath = $_SERVER['DOCUMENT_ROOT'] . "/functions/truetype/";
	$Font1 = $FontPath . $FontName1;
	$Font2 = $FontPath . $FontName2;
	$String = html_entity_decode($String);

	$TextDimensions = imagettfbbox($FontSize,$Angle,$Font1,$String);
	$Width = abs($TextDimensions[4] - $TextDimensions[0] + 5);
	$Height = abs($TextDimensions[5] - $TextDimensions[1]) + 10;
	$y_offset = abs($TextDimensions[5]);

	$Image = imageCreateTransparent($Width,$Height);
	imagesavealpha($Image, TRUE);
	$TextColor = imagecolorallocate($Image,255,204,153);
	$ShadowColor = imagecolorallocate($Image,119,119,119);

	if ($EnableShadow === TRUE):
		if ($CenterAlign === TRUE):			
			$x_offset = CenterText($FontSize,$Font1,$String,$Width);
		else:
			$x_offset = ($Width / 2) - ((min($TextDimensions[2],$TextDimensions[4]) - max($TextDimensions[0],$TextDimensions[6])) / 2);
		endif;
		// Creates drop-shadow
		imagettftextblur($Image,$FontSize,$Angle,$x_offset+5,$y_offset+5,$ShadowColor,$Font1,$String,10);
		// Creates text over drop-shadow
		imagettftextblur($Image,$FontSize,$Angle,$x_offset,$y_offset,$TextColor,$Font1,$String);
	else:
		if ($CenterAlign === TRUE):			
			$x_offset = CenterText($FontSize,$Font1,$String,$Width);
		else:
			$x_offset = ($Width / 2) - ((min($TextDimensions[2],$TextDimensions[4]) - max($TextDimensions[0],$TextDimensions[6])) / 2);
		endif;
		imagettftext($Image,$FontSize,$Angle,$x_offset,$y_offset,$TextColor,$Font1,$String);
	endif;
	
	return imagepng($Image);
	imagedestroy($Image);
} 

I am calling the function with the code below and note that the "/" is part of the text string but it is also the separation between the fonts and will probably be presenting using the second font:

CODE --> PHP

$String = "firsttext / second text";
	$Font1 = "racew17.ttf";
	$Font2 = "timesbd.ttf";
	$Size = 20;
	$Angle = 0;
	$Shadow = TRUE;
	$CenterAlign = TRUE;

echo textimage($String,$Font1,$Font2,$Size,$Angle,$Shadow,$CenterAlign); 

RE: Using Two Truetype Fonts to Create Image

(OP)
Perhaps this is a bit clearer without the calls to custom functions. It is properly creating any number of strings/fonts pairs using a foreach loop but each successive loop overwrites the previous image so the resulting image contains only the last string. What needs to be done to create the entire image?

CODE --> PHP

function TextImage($Strings,$Fonts,$FontSize,$Angle,$EnableShadow,$CenterAlign) {
	$FontPath = $_SERVER['DOCUMENT_ROOT'] . "/functions/truetype/";

	if (is_array($Strings) === TRUE && is_array($Fonts) === TRUE) :
		foreach (array_combine($Strings, $Fonts) AS $Strings => $Fonts) :
			$Fonts = $FontPath.$Fonts.".ttf";
			$TextDimensions = imagettfbbox($FontSize,$Angle,$Fonts,$Strings);
			$Width = abs($TextDimensions[4] - $TextDimensions[0] + 5);
			$Height = abs($TextDimensions[5] - $TextDimensions[1]) + 10;
			$Image = imageCreateTransparent($Width,$Height);
			$TextColor = imagecolorallocate($Image,255,204,153);
			$ShadowColor = imagecolorallocate($Image,119,119,119);
			$y_offset = abs($TextDimensions[5]);
			$x_offset = ($Width / 2) - ((min($TextDimensions[2],$TextDimensions[4]) - max($TextDimensions[0],$TextDimensions[6])) / 2);
			imagettftext($Image,$FontSize,$Angle,$x_offset,$y_offset,$TextColor,$Fonts,$Strings);
		endforeach;
	endif;

	return imagepng($Image);
	imagedestroy($Image);
} 

It is being called with:

CODE --> PHP

header('Content-Type: image/png');

require("logo_functions.php");

	$Strings = array("Firsttext ","/ Secondtext");
	$Fonts = array("racew17","timesbd");
	$Size = 25;
	$Angle = 0;

echo TextImage($Strings,$Fonts,$Size,$Angle); 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close