Helping ordinary people create extraordinary websites!
   

GD library imagettftext function not working

Wednesday, 27th January 2010
by idrees

I'm using GD library to draw curvy text on image. It works fine if i use imagestring function. but i need to apply fonts, which means i need to use imagettftext function but the problem i'm facing when i use imagettftext function i get blank image. here is my function which returns image. Please tell me where i'm wrong and how to fix it.


function DrawTextArc($str, $aStart, $aEnd, $iRadius, $bCCW, $fontFile, $colorNumber, $textsizeValue){
$nFont = $textsizeValue ;
list($colorR,$colorG,$colorB) = hex2rgb($colorNumber,false);
// create image to store each character
$xFont = imagefontwidth($nFont);
$yFont = imagefontheight($nFont);
$imgChar = imagecreatetruecolor($xFont, $yFont);

// create overall image
$iCentre = $iRadius + max($xFont, $yFont);
$img = imagecreatetruecolor(2 * $iCentre, 2 * $iCentre);
imagealphablending($img, false);
imagesavealpha($img,true);
$transparent = imagecolorallocatealpha($img, $colorR,$colorG,$colorB, 127);

//$black = imagecolorallocate($img, 0, 0, 0);



//imagecolortransparent($img, $black);

//$red = imagecolorallocate($img, 255, 0, 0);
// sort out colours
//$colBG = imagecolorallocate($img, 255, 255, 255);
imagealphablending($imgChar, false);
imagesavealpha($imgChar,true);
$transparent2 = imagecolorallocatealpha($imgChar, $colorR,$colorG,$colorB, 127);

//$colFGchar = imagecolorallocate($imgChar, 0, 0, 0);
//imagecolortransparent($imgChar, $colFGchar);
//$colRed = imagecolorallocate($imgChar, 255, 0, 0);
$textColor = imagecolorallocate($imgChar,$colorR,$colorG,$colorB );

imagefilledrectangle($img, 0, 0, 2 * $iCentre, 2 * $iCentre, $transparent);

// arrange angles depending on direction of rotation
if ($bCCW){
while ($aEnd < $aStart){
$aEnd += 360;
}
}else{
while ($aEnd > $aStart){
$aEnd -= 360;
}
}

$len = strlen($str);
$font = "fonts/".$fontFile;
// draw each character individually
for ($i = 0; $i < $len; $i++)
{
// calculate angle along arc
$a = ($aStart * ($len - 1 - $i) + $aEnd * $i) / ($len - 1);

// draw individual character
imagefilledrectangle($imgChar, 0, 0, $xFont, $yFont,$transparent2);



imagettftext($imgChar, 18, 0, 11, 21, $textColor, 'fonts/times.ttf', $str[$i]); //not working
// imagestring($imgChar, $nFont, 0, 0, $str[$i], $textColor); //working
// rotate character
$imgTemp = imagerotate($imgChar, (int)$a + 90 * ($bCCW ? 1 : -1), $transparent2);


$xTemp = imagesx($imgTemp);
$yTemp = imagesy($imgTemp);

// copy to main image
imagecopy($img, $imgTemp,
$iCentre + $iRadius * cos(deg2rad($a)) - ($xTemp / 2),
$iCentre - $iRadius * sin(deg2rad($a)) - ($yTemp / 2),
0, 0, $xTemp, $yTemp);
}

return $img;

}





More PHP Help:
» How i'll send mail using php script from localhost?
» Which script can I use for restoring my MySQL database?
» How to create a loan eligibility calculator using PHP?
» How do I make a PHP5/MySQL login system with admin features?
» What is the use of extract($_REQUEST);
» How can I split a string into several parts using PHP?
» PHP get current page's URL
» Creating new login in php