如何确定在网页上呈现的字符串的长度(以像素为单位)? [英] How to determine the length (in pixels) of a string being rendered on a web page?

查看:97
本文介绍了如何确定在网页上呈现的字符串的长度(以像素为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

谢谢

推荐答案

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

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

另一个你可以 使用 javascript操作SVG图像,以防万一想在客户端处理它。这样,甚至无论客户是否具有指定的字体或不... ...

On the other hand you can use also javascript to manipulate SVG images in 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...

这篇关于如何确定在网页上呈现的字符串的长度(以像素为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆