note 75439 added to function.imagettfbbox
| From: | NateSweet at osu1 dot php dot net | Date: | Wed, 30 May 2007 08:36:03 +0000 |
| Subject: | note 75439 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127013@lists.php.net to get a copy of this message | ||
This script shows you side by side the difference between a font rendered at a certain size and the
same font rendered at some multiple of that size and then scaled down by the same multiple. It seems
to help small sizes and affects large ones less. This script lets you see if it is worth
implementing for your situation. Included is the great "imagettfbboxextended" function by
mike at mikeleigh dot com below.
$size = 30;
$factor = 16;
$smallSize = imagettfbboxextended($size, 0, "fonts/MPlantin.ttf", "The quick brown
fox jumps over the lazy dog.");
$smallWidth = $smallSize['width'];
$smallHeight = $smallSize['height'];
$canvas = imagecreatetruecolor($smallWidth + 20, $smallHeight * 2 + 20);
imagefill($canvas, 0, 0, imagecolorallocate($canvas, 255, 255, 255));
imagettftext($canvas, $size, 0, 10 + $smallSize['x'], 10 + $smallSize['y'],
imagecolorallocate($canvas, 0, 0, 0), "fonts/MPlantin.ttf", "The quick brown fox
jumps over the lazy dog.");
$largeSize = imagettfbboxextended($size * $factor, 0, "fonts/MPlantin.ttf", "The
quick brown fox jumps over the lazy dog.");
$largeWidth = $largeSize['width'];
$largeHeight = $largeSize['height'];
$temp = imagecreatetruecolor($largeWidth, $largeHeight);
imagefill($temp, 0, 0, imagecolorallocate($canvas, 255, 255, 255));
imagettftext($temp, $size * $factor, 0, $largeSize['x'], $largeSize['y'],
imagecolorallocate($temp, 0, 0, 0), "fonts/MPlantin.ttf", "The quick brown fox jumps
over the lazy dog.");
imagecopyresampled($canvas, $temp, 10 + $smallSize['x'], 10 + $smallSize['y'] +
10, 0, 0, $smallWidth, $smallHeight, $largeWidth, $largeHeight);
imagepng($temp, "temp.png");
imagepng($canvas, "test.png");
function imagettfbboxextended($size, $angle, $fontfile, $text) {
/*this function extends imagettfbbox and includes within the returned array
the actual text width and height as well as the x and y coordinates the
text should be drawn from to render correctly. This currently only works
for an angle of zero and corrects the issue of hanging letters e.g. jpqg*/
$bbox = imagettfbbox($size, $angle, $fontfile, $text);
//calculate x baseline
if($bbox[0] >= -1) {
$bbox['x'] = abs($bbox[0] + 1) * -1;
} else {
//$bbox['x'] = 0;
$bbox['x'] = abs($bbox[0] + 2);
}
//calculate actual text width
$bbox['width'] = abs($bbox[2] - $bbox[0]);
if($bbox[0] < -1) {
$bbox['width'] = abs($bbox[2]) + abs($bbox[0]) - 1;
}
//calculate y baseline
$bbox['y'] = abs($bbox[5] + 1);
//calculate actual text height
$bbox['height'] = abs($bbox[7]) - abs($bbox[1]);
if($bbox[3] > 0) {
$bbox['height'] = abs($bbox[7] - $bbox[1]) - 1;
}
return $bbox;
}
----
Server IP: 64.71.164.2
Probable Submitter: 67.161.110.125
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- https://master.php.net/note/edit/75439
Del: integrated -- https://master.php.net/note/delete/75439/integrated
Del: useless -- https://master.php.net/note/delete/75439/useless
Del: bad code -- https://master.php.net/note/delete/75439/bad+code
Del: spam -- https://master.php.net/note/delete/75439/spam
Del: non-english -- https://master.php.net/note/delete/75439/non-english
Del: in docs -- https://master.php.net/note/delete/75439/in+docs
Del: other reasons-- https://master.php.net/note/delete/75439
Reject -- https://master.php.net/note/reject/75439
Search -- https://master.php.net/manage/user-notes.php