Page 1 of 1
center text string using imagettftext
Posted: Sat Nov 25, 2006 9:48 pm
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);
Re: center text string using imagettftext
Posted: Sat Nov 25, 2006 10:39 pm
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.
Posted: Sat Nov 25, 2006 11:43 pm
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?
Posted: Sat Nov 25, 2006 11:48 pm
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.
Posted: Sat Nov 25, 2006 11:53 pm
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

Posted: Sun Nov 26, 2006 7:33 am
by feyd
imagettfbbox() may be a better function to use for calculations.