各位前輩大家好,想請教一下,我用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);
?>