各位前辈大家好,想请教一下,我用GD的imagettfbbox()产出字转图
(图尺寸需跟字一样大)
只要宽一点的字体,文字的右边都会被裁到,是X轴没抓准还是width没抓准?
请问有其他解决的办法吗?
PS.因主机不够好,不能使用imagick处理,因为需好几秒才会产出图
为顾及速度,请问有能用GD解决的方式,或是有其他模组件可使用吗?
恳请解惑,附上程式码及裁到右边的字图
https://www.flickr.com/photos/57332716@N03/15371483427/in/photostream/player/
谢谢
<?php
//计算字的画框大小
function textBox($size ,$file ,$string){
$rect= imagettfbbox( $size, 0, $file, $string );
$minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
$maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
$minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
$maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));
return array(
"left" => abs($minX),
"top" => abs($minY),
"width" => $maxX - $minX,
"height" => $maxY - $minY,
"box" => $rect
);
}
$size = 52;
$file = 'font/arial.ttf';
$string = "M";
$box = textBox($size ,$file ,$string);
$im= imagecreatetruecolor( $box['width'], $box['height'] );
$black = ImageColorAllocate ($im,255, 255, 255);
$fill= imagefill( $im, 0, 0, $black );
$fcolor= imagecolorallocate( $im, 0, 0, 0);
imagettftext( $im, $size, 0, 0, $box['top'], $fcolor, $file, $string );
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>