简体   繁体   English

使用PHP GD在图像上居中显示文本

[英]Center text on image using PHP GD

So I am creating a banner generator. 所以我正在创建一个横幅生成器。

I will be adding text in the middle, but would like it to be exactly in the center. 我将在中间添加文本,但希望它完全位于中心。 I know that imagettftext can be used to write onto the banner, but that won't center it. 我知道imagettftext可以用来写入横幅,但这不会使它居中。

A likely solution could be to find the width of the text and then use half of it taken away from half of the banner width, but I've got no idea about how to do this. 一个可能的解决方案可能是找到文本的宽度,然后使用一半从横幅宽度的一半取出,但我不知道如何做到这一点。

I am using PHP-GD and do not want to use anything else I will have to install. 我使用的是PHP-GD,不想使用其他任何我必须安装的东西。

imagettftext($img, 14, 0, (468 - ((strlen($_GET['description']) * imagefontwidth(imageloadfont('minecraft.ttf'))) / 1)), 85, imagecolorallocate($img, 0, 0, 0), 'minecraft.ttf', $_GET['description']);

The code above is making the result above. 上面的代码是上面的结果。 It is fine with small strings but there must be something wrong since as soon as they become long, it fails. 小字符串很好,但一定有问题,因为一旦它们变长,它就会失败。

Check out imagettfbbox : http://www.php.net/manual/en/function.imagettfbbox.php . 查看imagettfbboxhttpimagettfbbox It will give you the extents of the text you wish to render. 它将为您提供要渲染的文本范围。 Then it's simple arithmetic to center that on your image. 然后,这是一个简单的算术,以在您的图像上居中。

You can center the text by getting the width from the outer boundaries from imageftbbox then divide this by two to get an offset that will center the text in the image. 您可以通过从imageftbbox获取外边界的宽度然后将其除以2来获得文本居中,以获得将图像中的文本居中的偏移。

// Get image dimensions
  $width = imagesx($image);
  $height = imagesy($image);
// Get center coordinates of image
  $centerX = $width / 2;
  $centerY = $height / 2;
// Get size of text
  list($left, $bottom, $right, , , $top) = imageftbbox($font_size, $angle, $font, $text);
// Determine offset of text
  $left_offset = ($right - $left) / 2;
  $top_offset = ($bottom - $top) / 2;
// Generate coordinates
  $x = $centerX - $left_offset;
  $y = $centerY - top_offset;
// Add text to image
  imagettftext($image, $font_size, $angle, $x, $y, $color, $font, $text);

imageftbbox documentation imageftbbox文档

It took me a long time but I figured out how to center text on an image exactly. 我花了很长时间,但我想出了如何准确地将图像放在图像上。

list($left,, $right) = imageftbbox(18, 0, 'minecraft.ttf', $_GET['description']);
$dwidth = $right - $left;
$pos = (HALF_OF_IMAGE_WIDTH - $nwidth / 2);

You can center a text in an image by getting the half of the image height and width and also the half of the text height and width 您可以通过获取图像高度和宽度的一半以及文本高度和宽度的一半来使图像中的文本居中

You can get the image width and height using imagesx and imagesy respectively. 您可以分别使用imagesximagesy获取图像的宽度和高度。
You can get the text width and height using imagettfbbox method in PHP GD. 您可以使用PHP GD中的imagettfbbox方法获取文本宽度和高度。

After, you get the bound, you can get the text width and text height 之后,您获得了绑定,您可以获得文本宽度和文本高度
text width = right bound on x - left bound on x axis text width = x上的右边界 - x轴上的左边界
text height = lower bound on y axis - upper bound on y axis text height = y轴上的下限 - y轴的上限

Then use the image width and height to the get the start offset that will allow your image to be centered 然后使用图像宽度和高度来获得允许图像居中的起始偏移
start_x_offset = (imagewidth - textwidth) / 2; start_x_offset =(imagewidth - textwidth)/ 2;
start_y_offset = (imageheight - textheight) / 2; start_y_offset =(imageheight - textheight)/ 2;

// Get image dimensions
$image_width = imagesx($image);
$image_height = imagesy($image);

$text_bound = imageftbbox($font_size, $angle, $font, $text);

//Get the text upper, lower, left and right corner bounds
$lower_left_x =  $text_bound[0]; 
$lower_left_y =  $text_bound[1];
$lower_right_x = $text_bound[2];
$lower_right_y = $text_bound[3];
$upper_right_x = $text_bound[4];
$upper_right_y = $text_bound[5];
$upper_left_x =  $text_bound[6];
$upper_left_y =  $text_bound[7];


//Get Text Width and text height
$text_width =  $lower_right_x - $lower_left_x; //or  $upper_right_x - $upper_left_x
$text_height = $lower_right_y - $upper_right_y; //or  $lower_left_y - $upper_left_y

//Get the starting position for centering
$start_x_offset = ($image_width - $text_width) / 2;
$start_y_offset = ($image_height - $text_height) / 2;

// Add text to image
imagettftext($image, $font_size, $angle, $start_x_offset, $start_y_offset, $color, $font, $text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM