php 如何确定在网页上呈现的字符串的长度(以像素为单位)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1641756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:21:30  来源:igfitidea点击:

How to determine the length (in pixels) of a string being rendered on a web page?

phphtmlsvg

提问by Kshitij Saxena -KJ-

If I know the font size (12) and the font family (calibri), is there a way to determine the length (in pixels) a given string will take after it will be rendered? I am absolutely certain about the font and size. Are there any lookup tables for this purpose to determine the length in PHP code itself? I am writing a PHP script to dynamically generate an SVG image which contains various boxes filled with data pulled out of a DB. The problem is that I need to manually wrap the text around and increase the size of the box if the string is big.

如果我知道字体大小 (12) 和字体系列 (calibri),有没有办法确定给定字符串在呈现后的长度(以像素为单位)?我绝对确定字体和大小。是否有任何用于此目的的查找表来确定 PHP 代码本身的长度?我正在编写一个 PHP 脚本来动态生成一个 SVG 图像,其中包含充满从数据库中提取的数据的各种框。问题是,如果字符串很大,我需要手动环绕文本并增加框的大小。

Thanks

谢谢

采纳答案by kkyy

Use the PHP function imagettfbboxto get the size of the bounding box for the given string.

使用 PHP 函数imagettfbbox获取给定字符串的边界框的大小。

On the other hand you canuse also javascript to manipulate SVG imagesin case you would like to handle it on the client side. That way it wouldn't even matter if the client had the specified font or not...

另一方面,如果您想在客户端处理它,您也可以使用javascript 来操作 SVG 图像。这样,客户端是否具有指定的字体甚至都无关紧要......

回答by jsnfwlr

You could use PHP GDto render out the string as a image and get the size of the resulting image.

您可以使用PHP GD将字符串渲染为图像并获取结果图像的大小。

If you use this, it should work:

如果您使用它,它应该可以工作:

   list($left,, $right) = imageftbbox( 12, 0, "arial.ttf", "Hello World");

   $width = $right - $left;

回答by ChssPly76

Your title and actual post seem to say slightly different things. I'm guessing what you're really asking is whether it's possible to determine the width in pixels of text rendered on a web page in your PHP code(prior to web page being rendered).

你的标题和实际帖子似乎说的有点不同。我猜您真正要问的是是否可以确定在您的 PHP 代码中呈现在网页上的文本的宽度(在呈现网页之前)。

If so, the answer is "NO". Said font may not be available on user's computer; s/he may have custom stylesheet applied; use large font sizes; etc...

如果是这样,答案是“否”。该字体可能在用户的计算机上不可用;他/她可能应用了自定义样式表;使用大字体;等等...

What you cando is determine that width using javascript afterthe page is rendered (obviously, you can do that in some other page as well and hope nothing changes till then) and submit that back to PHP. With some clever AJAX-ing you can even do this on the same page.

可以做的是在页面呈现使用 javascript 确定宽度(显然,您也可以在其他页面中执行此操作,并希望在此之前没有任何更改)并将其提交回 PHP。通过一些巧妙的 AJAX 处理,您甚至可以在同一页面上执行此操作。

Take a look at this questionfor details on how to do this in javascript.

有关如何在 javascript 中执行此操作的详细信息,请查看此问题

回答by WebManWalking

Use jQuery,

使用jQuery,

$("text").each(function(){alert(this.innerHTML+" is "+$(this).width()+" pixels.");});

回答by TimoRieth

If you want to determine the string length of text, using PHP's Build-In-Fonts (e.g. if you are using imagestring()), you can use:

如果你想确定文本的字符串长度,使用 PHP 的内置字体(例如,如果你使用 imagestring()),你可以使用:

imagefontwidth()


$text = "Your text here"
$fontSize = 5;

$textLength = imagefontwidth($fontSize) * strlen($text);

If you do not use the Build-In-Fonts, use imagettfbbox(), like mentioned above.

如果您不使用内置字体,请使用 imagettfbbox(),如上所述。