note 74949 added to function.imagettftext
| From: | ultraniblet at gmail dot com | Date: | Sat, 05 May 2007 20:08:32 +0000 |
| Subject: | note 74949 added to function.imagettftext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126225@lists.php.net to get a copy of this message | ||
I have found the kerning (spacing between letters) pretty poor with GD - it's not up to your
average designer's standards. Here are some ways to improve it:
- Place the letters one by one using their bounding box instead of using one string
- adjust with a $kerning value
- For small text, sample it down from a larger size to adjust in increments less than 1px
Eg:
<?PHP
$STRING = "NOTRE PHILOSOPHIE";
// ---- PRESETS
$FONT = "CantoriaMTStd-SemiBold.otf";
$FONT_SIZE = 10.5;
$WIDTH = 200;
$HEIGHT = 16;
$KERNING = 0;
$BASELINE = 12;
$BG_COLOR = array(
"R"=>5,
"G"=>45,
"B"=>53
);
$TXT_COLOR = array(
"R"=>188,
"G"=>189,
"B"=>0
);
// ---- CREATE CANVAS + PALETTE
$canvas = imageCreateTrueColor($WIDTH*4,$HEIGHT*4);
$bg_color = imageColorAllocate($canvas, $BG_COLOR["R"], $BG_COLOR["G"],
$BG_COLOR["B"]);
$txt_color = imageColorAllocate($canvas, $TXT_COLOR["R"], $TXT_COLOR["G"],
$TXT_COLOR["B"]);
imagefill ( $canvas, 0, 0, $bg_color );
// ---- DRAW
$array = str_split($STRING);
$hpos = 0;
for($i=0; $i<count($array); $i++)
{
$bbox = imagettftext( $canvas, $FONT_SIZE*4, 0, $hpos, $BASELINE*4, $txt_color, $FONT, $array[$i]
);
$hpos = $bbox[2]+$KERNING;
}
// ---- SAMPLE DOWN & OUTPUT
$final = imageCreateTrueColor($WIDTH,$HEIGHT);
imageCopyResampled( $final, $canvas, 0,0,0,0, $WIDTH, $HEIGHT, $WIDTH*4, $HEIGHT*4 );
header('Content-type: image/png');
imagePNG($final);
imageDestroy($canvas);
imageDestroy($final);
?>
----
Server IP: 194.145.210.3
Probable Submitter: 84.9.35.165
----
Manual Page -- http://www.php.net/manual/en/function.imagettftext.php
Edit -- https://master.php.net/note/edit/74949
Del: integrated -- https://master.php.net/note/delete/74949/integrated
Del: useless -- https://master.php.net/note/delete/74949/useless
Del: bad code -- https://master.php.net/note/delete/74949/bad+code
Del: spam -- https://master.php.net/note/delete/74949/spam
Del: non-english -- https://master.php.net/note/delete/74949/non-english
Del: in docs -- https://master.php.net/note/delete/74949/in+docs
Del: other reasons-- https://master.php.net/note/delete/74949
Reject -- https://master.php.net/note/reject/74949
Search -- https://master.php.net/manage/user-notes.php