center text string using imagettftext

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

center text string using imagettftext

Post by SidewinderX »

Im using gd to generate an image and print text onto that image that varies depending on what is being passed through the get variable.

Code: Select all

<?php
// set the content-type
header('Content-type: image/png');

// image background in png format
$im = imagecreatefrompng ("background.png");

// some color in RGB
$color = imagecolorallocate($im, 255, 255, 255);

// the text to be printed
$text = $_GET['name'];

// set the font
$font = 'arial.ttf';

// set the font size
$size = 8;

// add the text to the image
imagettftext($im, $size, 0, 55, 14, $color, $font, $text);

imagepng($im);
imagedestroy($im);
?>
Now when i draw the text onto the image, it places it at 55,14 and depending on the length of the string determines weather it looks centered or not. My question is, with out creating a seperate function to determine the width of the image, the length of the string, and then performe a basic calculation to determine where the string should start...is there anyway i can just say like

Code: Select all

imagettftext($im, $size, 0, CENTER!!!!11!11111!!!!, 14, $color, $font, $text);
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: center text string using imagettftext

Post by Zoxive »

SidewinderX wrote:is there anyway i can just say like

Code: Select all

imagettftext($im, $size, 0, CENTER!!!!11!11111!!!!, 14, $color, $font, $text);
Yes.

You just going to need to name the function to something different.

Code: Select all

imagesx($img);
strlen($str);
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

Well i know obviousally
Center!!!111!1111!
is an invalid function name. I knew I could create a function to find out where to start writing the text, but I was hoping there was a native function that would do all the work for me, but there probabley isnt.

I was playing with the two functions you posted and I realized creating a function to find where to start writing the text wasnt as simple as

Code: Select all

$center = ((imagesx($im)/2)-(strlen($text)/2));
because strlen just returns the number of characters the string is, rather than the pixel length. Any ideas how i could find the pixel length of the string or another means of accomplishing my task?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

SidewinderX wrote:Well i know obviousally
Center!!!111!1111!
is an invalid function name. I knew I could create a function to find out where to start writing the text, but I was hoping there was a native function that would do all the work for me, but there probabley isnt.

I was playing with the two functions you posted and I realized creating a function to find where to start writing the text wasnt as simple as

Code: Select all

$center = ((imagesx($im)/2)-(strlen($text)/2));
because strlen just returns the number of characters the string is, rather than the pixel length. Any ideas how i could find the pixel length of the string or another means of accomplishing my task?
You wont be able to find exactly how many pixels, but you can use the font size to estimate.

And i was talking about

Code: Select all

imagettftext($im, $size, 0, CENTER!!!!11!11111!!!!, 14, $color, $font, $text);
being the same name.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

Actually after a little searching around php.net i found a handy dandy function that calculates the font width

Code: Select all

//calculate where to start the string
$fontwidth = imagefontwidth($font);
$center = (imagesx($im)/2) - ($fontwidth*(strlen($text)/2));
// Adds the text to the image
imagettftext($im, $size, 0, $center, 14, $color, $font, $text);
the finished code is like so

Code: Select all

<?php
// set the content-type
header('Content-type: image/png');

// the text to be printed
$text = $_GET['name'];

//sets background image
if($text == "background2"){
	$im = imagecreatefrompng ("background2.png");
} elseif($text == "background3"){
	$im = imagecreatefrompng ("background3.png");
} else {
	$im = imagecreatefrompng ("background.png");
}

// Sets the text color in RGB
$color = imagecolorallocate($im, 255, 255, 255);

// Sets the font. Must be a tff font
$font = 'arial.ttf';

// Sets the font size
$size = 8;

//calculate where to start the string
$fontwidth = imagefontwidth($font);
$center = (imagesx($im)/2) - ($fontwidth*(strlen($text)/2));
// Adds the text to the image
imagettftext($im, $size, 0, $center, 14, $color, $font, $text);

imagepng($im);
imagedestroy($im);
?>

thanks for the help :lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

imagettfbbox() may be a better function to use for calculations.
Post Reply