tags:

views:

429

answers:

4

Hi Guys,

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 that the said font would be used. 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 box size if the string is big.

Thanks

+2  A: 

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).

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 can do is determine that width using javascript after the 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.

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

ChssPly76
Please see my edit. +1 for the link but it does not solve my purpose :(
Crimson
@ChssPly76 - I disagree. Crimson is using PHP to generate an image. That is done on the server side, so the font will definitely be available.
Abinadi
@Abinadi - Crimson is asking how many pixels will that string take when rendered **in browser**, not in the image he's rendering. Or, at least, that's the way I understood it.
ChssPly76
@Crimson - if you're truly **absolutely** certain that said string will be rendered using that font / size (which I suppose is possible if you have complete control over the end environment - specific browser / OS / fonts) you can actually use GD to find that out as suggested by `phalacee`
ChssPly76
Actually, it is a little bit of both as I am using the SVG definition for an image - the image is not a bitmap. Thus, it is the browser's problem to find the correct font but let's say that the font is definitely available. How do we find the width now? How can I retrieve the pixel width of each glyph in the font and sum?
Crimson
@ChssPly76 - I stand corrected. +1
Abinadi
@Crimson - take a look at http://www.php.net/manual/en/function.imagettfbbox.php for example. Assuming you have the font file available on your server, this should do what you want.
ChssPly76
+2  A: 

You could use PHP GD to render out the string as a image, and then get the size of the of the resulting image.

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

$width = $right - $left;

it should work ...

phalacee
It seems that Crimson is rendering the string as an image, but the question is what size to make the image so that the string will fit correctly. Can that be done with the GD library? I didn't think there was a word wrap option with GD.
Abinadi
+1 GD appears helpful
Crimson
phalacee, I am not able to find a way in GD to determine the string width. It seems that I must call imagecreate() with image width and height params before a string can be written to it thus making the excercise a futile round trip :(
Crimson
@ Crimson, even using imageftbbox() ??? if you use:list($left,, $right) = imageftbbox( 12, 0, arial.ttf, "Hello World");$width = $right - $left;it should work ...
phalacee
+2  A: 

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

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...

kkyy
that was the function i was thinking of when I gave my answer ... my memory isn't as good as it used to be ...
phalacee
+1  A: 

Do you have jQuery?

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

WebManWalking