Skip to content

Instantly share code, notes, and snippets.

@shea-sollars
Forked from mindplay-dk/README.md
Last active December 24, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shea-sollars/6773552 to your computer and use it in GitHub Desktop.
Save shea-sollars/6773552 to your computer and use it in GitHub Desktop.
<? 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);
<?php
define('SAMPLE', @$_SERVER['QUERY_STRING'] ? $_SERVER['QUERY_STRING'] : 'abc');
define('SAMPLE_TTF', 'georgiai.ttf');
define('SAMPLE_SIZE', 100);
define('SAMPLE_PAD', 50);
$textDimensionsHack = imagettfbbox(SAMPLE_SIZE, 0, SAMPLE_TTF, '2'); // Strings starting with 2 are always correct
$widthHack = $textDimensionsHack[2] - $textDimensionsHack[0]; // Remember width of 2
$m = imagettfbbox(SAMPLE_SIZE, 0, SAMPLE_TTF, '2'.SAMPLE); // Prepend 2 to the string to be measured
$width = ($m[4]-$m[6])-$widthHack + 2*SAMPLE_PAD; // Subtract the width of 2 for the real width of the string
$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-$widthHack, $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>
<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 a work around to the problem:</p>
<p><img src="metricsHack.php"/></p>
<h3>Source Code</h3>
<p>Below is the source code of "metricsHack.php", the output of which is embedded above.</p>
<div style="padding:8px; border:dotted 1px #888;">
<? highlight_string(file_get_contents('metricsHack.php')); ?>
</div>s
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment