Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This test-script demonstrates a bug in TrueType rendering in PHP.
<? phpinfo(INFO_GENERAL | INFO_MODULES); ?>
<?php
define('SAMPLE', @$_SERVER['QUERY_STRING'] ? $_SERVER['QUERY_STRING'] : 'abc');
define('SAMPLE_TTF', 'georgiai.ttf');
define('SAMPLE_SIZE', 100);
define('SAMPLE_PAD', 50);
$m = imagettfbbox(SAMPLE_SIZE, 0, SAMPLE_TTF, SAMPLE);
$width = $m[4]-$m[6] + 2*SAMPLE_PAD;
$height = $m[1]-$m[7] + 2*SAMPLE_PAD;
$baseline = abs($m[7]);
$img = imagecreatetruecolor($width,$height);
$red = imagecolorallocate($img,255,0,0);
$white = imagecolorallocate($img,255,255,255);
imagettftext(
$img,
SAMPLE_SIZE, # size
0, # angle
SAMPLE_PAD, # x
SAMPLE_PAD+$baseline, # y
$white,
SAMPLE_TTF,
SAMPLE
);
imagerectangle(
$img,
$m[6]+SAMPLE_PAD, $m[7]+SAMPLE_PAD+$baseline, # upper left x,y
$m[2]+SAMPLE_PAD, $m[3]+SAMPLE_PAD+$baseline, # lower right x,y
$red
);
header('Content-type: image/png');
imagepng($img);
<html>
<head>
<title>Font Metrics Test</title>
</head>
<body style="max-width:760px; padding:20px; font-family:Verdana,Arial,sans-serif; line-height:110%;">
<h1>Font Metrics Test</h1>
<p>This is <a href="metrics.info.php">PHP version <?=phpversion()?></a>.</p>
<p>The font metrics output by <a href="http://php.net/imagettfbbox">imagettfbbox</a> may be incorrect on some PHP installations.</p>
<p>The following sample image demonstrates the problem:</p>
<p><img src="metrics.php"/></p>
<p>The image above should display a red outline, precisely lined up with the bounds of the rendered glyphs - if the red line does not accurately line up with the boundaries of the type, the bug is present in your PHP installation.</p>
<h3>Source Code</h3>
<p>Below is the source code of "metrics.php", the output of which is embedded above.</p>
<div style="padding:8px; border:dotted 1px #888;">
<? highlight_string(file_get_contents('metrics.php')); ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.