Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Centering Text Onto Image Using GD Library

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I think I may be misunderstanding the units between image size and text string length (one is probably pixels and the other characters) so am comparing apples to oranges in trying to center text onto an image:

Code:
imagestring($im, 3, (imagesx($im)-(strlen($string))/2), 9, $string, $FontColor);

The math looks right to me but the text is off to the right, nearly off the image. Any clarification?

Although not as big an issue, how can I center vertically? Is there a function for measuring the height of text? I am using one of the default fonts now ("3") but may use TrueType later so then, it will be more important to make it dynamic, if possible.

Also, if the image, is shorter than the text, can it be stretched somehow with specific padding on the ends? It is a jpg, by the way, but is stored in the MySQL database rather than being a physical image.

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
If you are positioning text on a page, you need to find out, for a given text string, font and font size, how much space that text will take up.

PHP's image functions can provide this information. Take a look at imagettfbbox(), imageftbbox() or imagepsbbox().

These functions will return an array which define, in pixels, the size of the box which your text will fit into. From those numbers you can calculate where on the page to put the text.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks. I am using the GD Library and trying to place the text properly into an image (centered), not onto a page. Where the resulting image is on the page is unrelated because by then, the text will be part of the image.

In the GD Library, the default font choices are from 1 to 5 but other than that, I do not know exactly which fonts they are or what their size is. "3" seems close to the size I need so I am using it now just to get things started.

I have been searching for some way to convert string lengths to pixels or pixels to string lengths since I think that's the problem but, so far, I have not found such a function.

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Thanks, I thought of that but I do not have TrueType functions available on the ISP's box and they will not install them for me. I have it here on my development platform but it does me no good if it isn't where the live site goes!

I suppose as a work aound, I could create an image of only the text, then measure the results, which would be in pixels. Sounds like a roundabout way to go though.

Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
OK, to clarify :
If you want to use a True Type Font on your ISP's box, then you can. just dump it in the directory and specify the correct path.

Aso included are the calc for centering text based etc ..
enjoy
// karv

Code:
<?php
// Set the static variables 
$font_file = '/var/[URL unfurl="true"]www/html/public/myfont.ttf';[/URL]
$font_size = 8;
$angle = 0;
$picfile='somepic.png';
$text='copyright';

// work out image sizes
$sizes=getimagesize($picfile);
$max_width=$sizes[0];
$max_height=$sizes[1];

// use the output from this to calculate the length and height of the text 
$line_width = imagettfbbox($font_size, 0, $font_file, $text);

// Calculate horizontal starting point for the text 
$x_start = (($max_width - $line_width[2] - $line_width[0]) / 2);
// Calculate vertical starting point for text based on font size
$y_start = (($max_height /2) - (($line_width[5] - $line_width[0]) / 2));



// Create the image resource and assign colors to variables 
header("Content-type: image/png");
$im = imagecreatefrompng($picfile);

$color = imagecolorallocate($im, 123,123,123);


// Write the text to the image 
imagettftext($im, $font_size, $angle ,$x_start, $y_start, $color, $font_file, $text);


// Create and then destroy the image 
imagepng($im);
imagedestroy($im);

?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks, I'll try it in more detail next week but it looks promising. But doesn't imagettfbbox() need the FreeType library installed?

On my site, both $Image and $mime_type are coming from a MySQL database entry but, in this case, it is a jpeg. What I have now gives a "dead" image so I must have missed something:

Code:
Header ("Content-type: image/" . $mime_type);
Header ("Pragma: no-cache");

// Create the image resource and assign colors to variables
$im = ImageCreateFromString($Image);
$FontColor = ImageColorAllocate($im, 200, 31, 31);

// Set the static variables 
$font_file = 'C:/Websites/domain/truetype/hobo.ttf';
$font_size = 8;
$angle = 0;

// work out image sizes
$sizes=getimagesize($im);
$max_width=$sizes[0];
$max_height=$sizes[1];

// use the output from this to calculate the length and height of the text 

$line_width = imagettfbbox($font_size, 0, $font_file, $string);

// Calculate horizontal starting point for the text 
$x_start = (($max_width - $line_width[2] - $line_width[0]) / 2);

// Calculate vertical starting point for text based on font size
$y_start = (($max_height /2) - (($line_width[5] - $line_width[0]) / 2));

// Write the text to the image 
imagettftext($im, $font_size, $angle, $x_start, $y_start, $FontColor, $font_file, $string);

imagejpeg($im);
imagedestroy($im);


Don
contact@pc-homepage.com
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
This should center it horizontally and vertically. i got it off the php.net site.

Code:
<?php
function imagestringcentered ($img,$font,$text,$color) {
 while (strlen($text) * imagefontwidth($font) > imagesx($img)) {
  if ($font > 1) { $font--; }
  else { break; }
 } 
 $cy = (imagesy($img)/2) - (imagefontwidth($font)/2);
 imagestring($img,$font,imagesx($img) / 2 - strlen($text) * imagefontwidth($font) / 2,$cy,$text,$color);
}
?>

David Kuhn
------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top