php104.tmp,PHP imagettfbbox 用法 手册 | 示例代码

该脚本展示了同一字体在不同尺寸下渲染的差异,以及缩放后的效果。它通过比较字体在原始大小和放大后缩小的效果,帮助判断是否适合在网站上实施这种缩放策略。涉及到的技术包括图像处理、字体渲染和PHP的imagettfbbox函数的扩展应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值