Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Two Truetype Fonts to Create Image

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
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.

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:

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);
 
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?

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:

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);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top