如何使用PHP GD将文本写入不同大小的图像
我正在创建不同大小的图像。如何在这些图像上书写文本,使文本始终与图像相匹配如何使用PHP GD将文本写入不同大小的图像,php,gd,image-manipulation,Php,Gd,Image Manipulation,我正在创建不同大小的图像。如何在这些图像上书写文本,使文本始终与图像相匹配 $text=“一些文本作为示例”; $font=“arialbd.ttf” $fontsize=26; 偏移量x=0; 偏移量y=0; $image01=imagecreate(1120900); $image02=imagecreate(400300); $image03=imagecreate(1120900); 我知道有一个imagefttext功能可以将文本应用到图像中,但使用该功能,我只能更改字体大小以使文本
$text=“一些文本作为示例”;
$font=“arialbd.ttf”
$fontsize=26;
偏移量x=0;
偏移量y=0;
$image01=imagecreate(1120900);
$image02=imagecreate(400300);
$image03=imagecreate(1120900);
我知道有一个
imagefttext
功能可以将文本应用到图像中,但使用该功能,我只能更改字体大小以使文本变大。如何确定它是否适合我的图像?使用imageftbbox函数获取文本边框的大小。然后,您可以调整文本大小以完全适合图像的大小
如果您希望缩放字体大小以使文本字符串填充图像宽度,请尝试此操作。 使用imagettfbbox确定当前文本框的宽度:
$bbox = imagettfbbox($fontsize,0,$fontpath,$string);
$tbwidth = $bbox[2];
然后将图像宽度除以$tbwidth得到一个系数
$factor = round(($imgwidth / $tbwidth), 0, PHP_ROUND_HALF_DOWN); //ie. 800/240=3
将$factor乘以$fontsize可获得近似的增长
$newfontsize = $fontsize * $factor; //ie. 12(pt) * 3 = 36
请记住,如果使用GD2.0,fontsize是以点为单位的,而不是以像素为单位的。您的算法将计算默认字体大小文本框(表示为文本框宽度)和图像宽度之间的差异 我最近遇到了同样的情况,背景是透明的,但当前的示例不是解决方案而是线索,或者是不起作用的解决方案,因此,如果有人需要,这里是一个组合的、有效的解决方案
function imagecreater($width = 600, $height = 600) {
//Create an empty transparent image
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagesavealpha($img, true);
$transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $transparent);
//Text information
$text = "some text as example";
$font = "arialbd.ttf"
$fontsize = 26; //default font to be altered later
//simulate a complete text box and get the width
$bbox = imageftbbox($fontsize, 0, $font, $text);
$tbwidth = $bbox[2];
//Calculate different between our transparent image and text box
//I've added a little padding (20px) since the text sometimes crossing the edge..
$factor = (($width - 20) / $tbwidth);
//Alter font size with difference to fit fully image
$newfontsize = $fontsize * $factor;
//Find the horisontal center
$bbox = imageftbbox($newfontsize, 0, $font, $text);
$newheight = ($height / 2) + (($bbox[3] - $bbox[7]) / 2);
//Set Color of text
$color = imagecolorallocate($img, 200, 0, 0);
//Produce our image
imagettftext($img, $newfontsize, 0, 0, $newheight, $color, $font, $text);
//Copy image to file and free the cached image
$target = "testimage.png";
imagepng($img, $target);
imagedestroy($img);
}
正如rwhite35在这里提到的,请记住GD 2.0 write字体大小是以点为单位的,而不是以像素为单位的。请看这个。它仍然不在完整的图像上,只是显示在顶部。我希望该文本完全覆盖您创建的所有图像
$im=imagecreatetruecolor(800600)代码>请记住,返回的坐标数组可能具有负值。更好的宽度计算是abs($bbox[2]-$bbox[0])
,如中所述。解决方案不错,但将0
作为round()的精度传递会得到不准确的结果。如果在500px宽的图像上有一个400px宽的文本,它将向下取整并返回1.0
,因此不会调整字体大小。我建议更高的精度,或者更好的是,放弃舍入。只需使用$imgwidth/$tbwidth
。
function imagecreater($width = 600, $height = 600) {
//Create an empty transparent image
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, false);
imagesavealpha($img, true);
$transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $transparent);
//Text information
$text = "some text as example";
$font = "arialbd.ttf"
$fontsize = 26; //default font to be altered later
//simulate a complete text box and get the width
$bbox = imageftbbox($fontsize, 0, $font, $text);
$tbwidth = $bbox[2];
//Calculate different between our transparent image and text box
//I've added a little padding (20px) since the text sometimes crossing the edge..
$factor = (($width - 20) / $tbwidth);
//Alter font size with difference to fit fully image
$newfontsize = $fontsize * $factor;
//Find the horisontal center
$bbox = imageftbbox($newfontsize, 0, $font, $text);
$newheight = ($height / 2) + (($bbox[3] - $bbox[7]) / 2);
//Set Color of text
$color = imagecolorallocate($img, 200, 0, 0);
//Produce our image
imagettftext($img, $newfontsize, 0, 0, $newheight, $color, $font, $text);
//Copy image to file and free the cached image
$target = "testimage.png";
imagepng($img, $target);
imagedestroy($img);
}