Jump to content


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



Recommended Posts

Can someone explain how imageftbbox works? Also how can you center text with a text block. Another question i have can you auto resize an image? I tried and it just seemed to crop it instead of resize. Any info would be great.

Share this post

Link to post
Share on other sites
Is see on php.net for image that this will place the text centered on the image:
$px    = (imagesx($im) - 7.5 * strlen($string)) / 2;

However i need to center it on a certain area in the image not the image iself. When doing a pdf you can set the XY for the boundingbox not sure if this can be done in GD as that would do the trick. Any suggestions?????

Share this post

Link to post
Share on other sites
If you want to centre in a certain area then if X is the centre point of the area, the start of the text string ($px) will be at (X - textwidth/2)

This will taka text string, split it into lines of approx 25 charcters, and centre the lines of text in the image


$font = "C:/windows/fonts/arial.ttf";

$text = $_GET['text'];

$im = imagecreate (200, 80);

$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

$text_array = explode ("\n", wordwrap($text, 25));

$image_width = imagesx($im);
$y = 5;
foreach ($text_array as $line) {
         $box = imagettfbbox(12,0,$font,$line);
         $text_width = $box[2] - $box[0];
               // calc pos to centre the text
         $x = ($image_width - $text_width) / 2;
         $y += 20;

         imagettftext($im, 12, 0, $x, $y, $white, $font, $line);


header("content-type: image/png");

Save it as "centreText.php" and place image on page with

<IMG src='centreText.php?text='Now+is+the+time+for+all+good+men+to+come+to+the+aid+of+the+party'>

Share this post

Link to post
Share on other sites


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.