在php图像中居中文字

冰淇淋

我不确定如何使用imagettftext在php创建的图像中居中放置文本。这是我的代码:

header('Content-Type: image/png');
$im = imagecreatetruecolor(260, 180);
$background = imagecolorallocate($im, 0, 0, 255);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 60, 153, 181);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 200, $white);
$text = $_GET['tname'];
$font = 'OpenSans-Semibold.ttf';
imagettftext($im, 20, 0, $x+1, $y+1, $grey, $font, $text);
imagettftext($im, 20, 0, $x, $y, $black, $font, $text);
imagepng($im);
imagedestroy($im);
马丁先生

这是一个基于imagetttfbbox的工作示例

$im = imagecreatetruecolor(200, 75);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// text, font and size to draw
$text = 'hello world';
$font = 'OpenSans-Semibold.ttf';
$size = 30;

// determine the size of the text so we can center it
$box = imagettfbbox($size, 0, $font, $text);
$text_width = abs($box[2]) - abs($box[0]);
$text_height = abs($box[5]) - abs($box[3]);
$image_width = imagesx($im);
$image_height = imagesy($im);
$x = ($image_width - $text_width) / 2;
$y = ($image_height + $text_height) / 2;

// add text
imagettftext($im, $size, 0, $x, $y, $white, $font, $text);

// set content type
header('Content-Type: image/png');

// output and destroy image
imagepng($im);
imagedestroy($im);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在页脚中以图像居中文字

来自分类Dev

在图像上居中文字

来自分类Dev

动态创建的div中图像旁边的居中文字。jQuery的

来自分类Dev

离子列表插入中的居中文字

来自分类Dev

按钮下方的居中文字

来自分类Dev

Pygame-如何居中文字

来自分类Dev

CSS居中文字与长字

来自分类Dev

Jekyll Kramdown如何居中文字

来自分类Dev

以背景颜色正确居中文字

来自分类Dev

居中文字并图标向左对齐

来自分类Dev

按钮下方的居中文字

来自分类Dev

在sql中获取中文字母以在php中显示

来自分类Dev

<p>中的水平居中文字显示:表格单元格?

来自分类Dev

垂直居中文字仅在底部溢出

来自分类Dev

img旁边的居中文字被<br />破坏

来自分类Dev

结合绝对位置和居中文字

来自分类Dev

垂直居中文字出现问题

来自分类Dev

样式锚点内的居中文字

来自分类Dev

垂直居中文字出现问题

来自分类Dev

如何将背景图片与h1中居中文字的左侧对齐?

来自分类Dev

尝试使用垂直居中文字和下方圆圈文字创建圆圈

来自分类Dev

如何在ZipEntry中写中文字符?

来自分类Dev

模仿Web URL在python中编码中文字符

来自分类Dev

Java中的中文字符串处理?

来自分类Dev

Firefox 57.0中不显示中文字符

来自分类Dev

Latex 英文文档中的中文字体

来自分类Dev

Vuetify-用居中文字(两轴)制作方形v卡

来自分类Dev

居中文字向左浮动图片,然后删除所有浮动图片以供移动显示

来自分类Dev

更改代码片段中文字的顺序