我已成功将文字写入我的img
并正确定位,但我遇到的问题是增加字体大小会使文字偏离图像。
所以我知道只需简单地增加文本写入屏幕的行高就可以实现我想要的外观。我似乎无法找到关于是否/如何做到的任何文档。
我的PHP代码
<?php
$font = 'font-type.ttf';
$rImg = ImageCreateFromJPEG( "test.jpg" );
$color = imagecolorallocate($rImg, 255, 255, 255);
//create bounding box
$bbox = imagettfbbox ( 38 , 0 , $font , urldecode($_GET['promo']) );
//setting the cordinates
$x = $bbox[0] + (imagesx($rImg) / 2) - ($bbox[4] / 2) + 200;
$y = $bbox[1] + (imagesy($rImg) / 2) - ($bbox[5] / 2) + 25;
//write it
imagettftext($rImg, 38, 0, $x, $y, $color, $font, urldecode($_GET['promo']));
header('Content-type: image/jpeg');
imagejpeg($rImg, NULL,100);
imagedestroy($rImg);
?>
答案 0 :(得分:0)
我已经使用此功能实现了我想要的外观
function ImageTTFTextWithTracking($im, $size, $angle, $t, $x, $y, $color, $font, $text) {
$numchar = strlen($text);
for($i = 0; $i < $numchar; $i++) {
# Assign character
$char[$i] = substr($text, $i, 1);
# Write character
imagettftext($im, $size, $angle, ($x + $w + ($i * $t)), $y, $color, $font, $char[$i]);
# Get width of character
$width = imagettfbbox($size, $angle, $font, $char[$i]);
$w = $w + $width[2];
}
}
ImageTTFTextWithTracking($rImg, 48, 0, 10, $x, $y, $colorWhite, $font, urldecode($_GET['code']));