468,257 Members | 1,429 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

Centering text in a dynamically created image (GD or ImageMagick).

Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 6 '07 #1
6 4433
On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
1. Determine the width of the image.
2. Determine the width of the text, (I'm assuming you'll be outputting
text using TTF?), using imagettfbbox()
3. The left-hand position of the text should be : (image's width / 2)
- (text width / 2)

Jun 6 '07 #2
gbbulldog wrote:
On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
>Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.
1. Determine the width of the image.
2. Determine the width of the text, (I'm assuming you'll be outputting
text using TTF?), using imagettfbbox()
3. The left-hand position of the text should be : (image's width / 2)
- (text width / 2)
Thanks!

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 7 '07 #3
-Lost wrote:
gbbulldog wrote:
>On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
>>Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.
1. Determine the width of the image.
2. Determine the width of the text, (I'm assuming you'll be outputting
text using TTF?), using imagettfbbox()
3. The left-hand position of the text should be : (image's width / 2)
- (text width / 2)

Thanks!
Ooops! I have no clue why I said thanks.

I specifically said "without the Freetype library."

That function depends on its functionality to work as expected.

Thanks though.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 7 '07 #4
Rik
On Thu, 07 Jun 2007 19:24:39 +0200, -Lost <ma****************@techie.com>
wrote:
-Lost wrote:
>gbbulldog wrote:
>>On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
Anyone know offhand how to center a text within the bounding box of an
image (width-wise at least) without the Freetype library?

GD- or ImageMagick-specifc code would do nicely.

1. Determine the width of the image.
2. Determine the width of the text, (I'm assuming you'll be outputting
text using TTF?), using imagettfbbox()
3. The left-hand position of the text should be : (image's width / 2)
- (text width / 2)
Thanks!

Ooops! I have no clue why I said thanks.

I specifically said "without the Freetype library."

That function depends on its functionality to work as expected.
Write your own variant of the freetypre library if it isn't available,
that's the only solution. There's no other way you can know the width of
dynamic text with an arbitrary font. Yes, it's a pain, so if the library
isn't available seriously consider changing hosts and/or change the server.

--
Rik Wasmus
Jun 7 '07 #5
On Jun 7, 7:49 pm, Rik <luiheidsgoe...@hotmail.comwrote:
On Thu, 07 Jun 2007 19:24:39 +0200, -Lost <maventheextrawo...@techie.com>
wrote:
-Lost wrote:
gbbulldog wrote:
On 6 Jun, 05:34, -Lost <maventheextrawo...@techie.comwrote:
Anyone know offhand how to center atextwithin the bounding box of an
image(width-wise at least) without the Freetype library?
>>>GD- orImageMagick-specifc code would do nicely.
>1. Determine the width of theimage.
2. Determine the width of thetext, (I'm assuming you'll be outputting
textusing TTF?), using imagettfbbox()
3. The left-hand position of thetextshould be : (image'swidth / 2)
- (textwidth / 2)
Thanks!
Ooops! I have no clue why I said thanks.
I specifically said "without the Freetype library."
That function depends on its functionality to work as expected.

Write your own variant of the freetypre library if it isn't available,
that's the only solution. There's no other way you can know the width of
dynamictextwith an arbitrary font. Yes, it's a pain, so if the library
isn't available seriously consider changing hosts and/or change the server.

--
Rik Wasmus
function getTextPosition($text,$center)
{
$im_tmp = imagecreatetruecolor(500,500);
$point_array = imagettftext($im_tmp,
20,0,0,50,imagecolorallocate($im0,0,0),'C:\Windows \Fonts\Verdana.ttf',
$text);
imagedestroy($im_tmp);
$left_x = $point_array[0];
$right_x = $point_array[2];
$length = $right_x - $left_x;
$text_position = $center - ($length/2);
return $text_position;
}
Sorry about the poor coding but this ought to take care of it. The
function will return the x coordinate that your text should be placed
at to be centered on $center. Just adjust the imagettftext function
to use your own font and size. All it does within the function is
place that text on a dummy image and read the points off of it. Let
me know if this works.

Eric

Jul 20 '07 #6
Rik
Hey, blast from the past:

On Fri, 20 Jul 2007 20:31:22 +0200, movement_eric <er**********@gmail.com
wrote:
On Jun 7, 7:49 pm, Rik <luiheidsgoe...@hotmail.comwrote:
>>>Anyone know offhand how to center atextwithin the bounding box of
an
>>>>image(width-wise at least) without the Freetype library?

Write your own variant of the freetypre library if it isn't available,
that's the only solution. There's no other way you can know the widthof
dynamictextwith an arbitrary font. Yes, it's a pain, so if the library
isn't available seriously consider changing hosts and/or change the
server.
function getTextPosition($text,$center)
{
....
$point_array = imagettftext($im_tmp,
http://www.php.net/imagettftext
"Notes
Note: This function requires both the GD library and the FreeType library."

So: no banana. Then again the FreeType library ought to be normally
available IMO.
--
Rik Wasmus
Jul 20 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by chotiwallah | last post: by
reply views Thread by laredotornado | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.