Jump to content

Length of text for GD


SharkBait

Recommended Posts

Hi,

  I am trying to center text on an image I am creating with the GD.

I am currently using [code=php:0]imagestring()[/code] but with that I am not specifing a font like I would with imagettftext()

So if I could do something like this:
[code]
<?php

$fontWidth = imagefontwidth($font);
$stringLength = strlen("My String") * $fontWidth;

?>
[/code]
But because I am using [code=php:0]imagestring()[/code] I am unsure as how to figure out the default font's width because I dont set a font.

Should I use a font and just use imagettftext instead of imagestring?
Link to comment
Share on other sites

Ok my centering isnt working.

My image is 50 in width.

This is what I have:

[code]
<?php

$imgWidth = 50;

$text = "HELP";
$fontWidth = 5;
$strLength = strlen($text) * $fontWidth;
$start = ($imgWidth - $strLength) / 2;

imagestring($img, $fontWidth, $start, 30, $text, $black);
?>
[/code]

But it doesn't look centered.

I also do [code=php:0]list($imgWidth, $imgHeight, $imgType, $imgAttr) = getimagesize('graphics/speed.gif');[/code] and that makes imgWidth = 50

Is my logic for centering incorrect?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.