Jump to content

PHP GD and true type fonts


kickstart

Recommended Posts

Hi

 

Not that often I play with these (and normally just a single font I use).

 

However I am trying to generate an image with a ttf bit of text on it. No problem using Tuffy.ttf (the normal one I use), but I want to use some arrow characters which it doesn't support.

 

I have tried using Wingding and Webdings but neither work with GD.

 

Am I missing something (hopefully something obvious)?

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

No error message, just getting small square (90%) or a blank (other 10%) for the characters.

 

My test code is as follows (loops through from 0 to 255 and creates a small image with that character in the middle of it):-

 

<?php

$width = 1;

$font = 0;
$ButtonUnit = 50;
$widthDisplay = $width * $ButtonUnit;
$font = 'webdings.ttf';

for($xCnt=0;$xCnt<=255;$xCnt++) 
{
$char = $xCnt;
$ImageSaved = DrawButton($char,array(255,255,255),array(255,165,0),$font,$widthDisplay,$ButtonUnit,15,5,array(0,0,0));
}


function DrawButton($ButtonText,$ForeGroundColour=array(255,255,255),$ButtonColour=array(18,52,86),$font = '', $Width = 0, $Height = 0, $fsize=24, $CornerRadius = 50, $FadeToColour=array(255,255,255))
{
global $Language;

$CacheLocation = "Images/Names/";
$fontarray = explode('.',$font);
$FullFilePath = $CacheLocation.$fontarray[0].'_'.$ButtonText.".gif";

if (file_exists($FullFilePath))
//if (false)
{
	// Whoopee Doo
}
else
{

	$UsedText = html_entity_decode(chr($ButtonText));
	if ($UsedText == '') $UsedText = '?';
echo "$ButtonText $font<br />";
	$font = (($font == '') ? 'Tuffy.ttf' : $font);
echo "$ButtonText $font<br />";
	$tb=calculateTextBox($fsize, 0, $font, $UsedText); //gets an array of numbers...some can be used to define image size
	$Width = (($Width==0) ? ceil($tb["width"]*1.4) : $Width);//create a space for lowercase letters that go below base
	$Height = (($Height==0) ? ceil($tb["height"]*1.4) : $Height);

	$LeftPos = ceil(($Width - $tb["width"]) / 2);
	$TopPos = $tb["top"] + ceil(($Height - $tb["top"]) / 2);

	$Image =  imagecreate ($Width , $Height );

	$background_color = imagecolorallocate($Image, 0, 0, 0);
	$button_color = imagecolorallocate($Image, $ButtonColour[0], $ButtonColour[1], $ButtonColour[2]);
	imagecolortransparent($Image, $background_color);

	$color0=($ButtonColour[0]-$FadeToColour[0])/($CornerRadius*2); 
	$color1=($ButtonColour[1]-$FadeToColour[2])/($CornerRadius*2); 
	$color2=($ButtonColour[2]-$FadeToColour[2])/($CornerRadius*2); 

	$SpotCentreX = array($CornerRadius, $Width - $CornerRadius - 1, $Width - $CornerRadius - 1, $CornerRadius);
	$SpotCentreY = array($CornerRadius, $CornerRadius, $Height - $CornerRadius - 1, $Height - $CornerRadius - 1);

	for ($i=($CornerRadius*2);$i>0;$i=$i-1) 
	{ 
		$red=$ButtonColour[0]-floor($i*$color0); 
		$green=$ButtonColour[1]-floor($i*$color1); 
		$blue=$ButtonColour[2]-floor($i*$color2); 
		$col= imagecolorallocate($Image, $red, $green, $blue); 
		imagefilledellipse($Image , $SpotCentreX[0], $SpotCentreY[0], $i, $i, $col);
		imagefilledellipse($Image , $SpotCentreX[1], $SpotCentreY[1], $i, $i, $col);
		imagefilledellipse($Image , $SpotCentreX[2], $SpotCentreY[2], $i, $i, $col);
		imagefilledellipse($Image , $SpotCentreX[3], $SpotCentreY[3], $i, $i, $col);
		imageline($Image, $SpotCentreX[0], $SpotCentreY[0]-intval($i/2), $SpotCentreX[1], $SpotCentreY[1]-intval($i/2), $col); 
		imageline($Image, $SpotCentreX[1]+intval($i/2), $SpotCentreY[1], $SpotCentreX[2]+intval($i/2), $SpotCentreY[2], $col); 
		imageline($Image, $SpotCentreX[3], $SpotCentreY[3]+intval($i/2), $SpotCentreX[2], $SpotCentreY[2]+intval($i/2), $col); 
		imageline($Image, $SpotCentreX[0]-intval($i/2), $SpotCentreY[0], $SpotCentreX[3]-intval($i/2), $SpotCentreY[3], $col); 
	} 		
	imagefilledrectangle($Image , $SpotCentreX[0] , $SpotCentreY[0] , $SpotCentreX[2] , $SpotCentreY[2] , $button_color );		

	//imagefilledellipse($Image , floor($Height/2), floor($Height/2), floor($Height/2)*2, floor($Height/2)*2, $button_color);
	//imagefilledellipse($Image , ($Width - floor($Height/2) - 1), floor($Height/2), floor($Height/2)*2, floor($Height/2)*2, $button_color);
	//imagefilledrectangle($Image , floor($Height/2) , 0 ,($Width - floor($Height/2) - 1) , floor($Height/2)*2 , $button_color );		

	$foreground_color = imagecolorallocate($Image, $ForeGroundColour[0], $ForeGroundColour[1], $ForeGroundColour[2]);
	imagettftext($Image, $fsize, 0, $LeftPos , $TopPos,  $foreground_color, $font, $UsedText);

$FullFilePath = $CacheLocation.$font.'_'.$ButtonText.".gif";
	$imageSuccess = imagegif($Image,$FullFilePath);

}
return($FullFilePath);
}

function calculateTextBox($font_size, $font_angle, $font_file, $text) 
{
$box   = imagettfbbox($font_size, $font_angle, $font_file, $text); 
if( !$box ) return false; 
$min_x = min( array($box[0], $box[2], $box[4], $box[6]) ); 
$max_x = max( array($box[0], $box[2], $box[4], $box[6]) ); 
$min_y = min( array($box[1], $box[3], $box[5], $box[7]) ); 
$max_y = max( array($box[1], $box[3], $box[5], $box[7]) ); 
$boxwidth  = ( $max_x - $min_x ); 
$boxheight = ( $max_y - $min_y ); 
$left   = abs( $min_x ) + $boxwidth; 
$top    = abs( $min_y ) + $boxheight; 
// to calculate the exact bounding box i write the text in a large image 
if ($boxwidth == 0) $boxwidth = 50;
if ($boxheight == 0) $boxheight = 50;
$img     = imagecreatetruecolor( $boxwidth << 2, $boxheight << 2 );  // << bitwise shift
$white   =  imagecolorallocate( $img, 255, 255, 255 ); 
$black   =  imagecolorallocate( $img, 0, 0, 0 ); 
imagefilledrectangle($img, 0, 0, imagesx($img), imagesy($img), $black); 
// for sure the text is completely in the image! 
imagettftext( $img, $font_size, 
			$font_angle, $left, $top, 
			$white, $font_file, $text); 
// start scanning (0=> black => empty) 
$rleft  = $w4 = $boxwidth<<2; 
$rright = 0; 
$rbottom   = 0; 
$rtop = $h4 = $boxheight<<2; 
for( $x = 0; $x < $w4; $x++ ) 
	for( $y = 0; $y < $h4; $y++ ) 
		if( imagecolorat( $img, $x, $y ) )
		{ 
		$rleft   = min( $rleft, $x ); 
		$rright  = max( $rright, $x ); 
		$rtop    = min( $rtop, $y ); 
		$rbottom = max( $rbottom, $y ); 
		} 
// destroy img and serve the result 
imagedestroy( $img ); 
return array( "left"   => $left - $rleft, 
			"top"    => $top  - $rtop, 
			"width"  => $rright - $rleft + 1, 
			"height" => $rbottom - $rtop + 1 ); 
}


?>

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.