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
https://forums.phpfreaks.com/topic/27635-length-of-text-for-gd/
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
https://forums.phpfreaks.com/topic/27635-length-of-text-for-gd/#findComment-126394
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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