Jump to content


Photo

[SOLVED] GD Help


  • Please log in to reply
3 replies to this topic

#1 tobeyt23

tobeyt23
  • Members
  • PipPipPip
  • Advanced Member
  • 259 posts
  • LocationSalisbury

Posted 06 March 2006 - 10:16 PM

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.

#2 tobeyt23

tobeyt23
  • Members
  • PipPipPip
  • Advanced Member
  • 259 posts
  • LocationSalisbury

Posted 08 March 2006 - 07:36 PM

Still working on centering text and ideas?

#3 tobeyt23

tobeyt23
  • Members
  • PipPipPip
  • Advanced Member
  • 259 posts
  • LocationSalisbury

Posted 08 March 2006 - 08:30 PM

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?????

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 08 March 2006 - 10:48 PM

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

<?php

$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");
imagepng($im);
imagedestroy($im);
?>

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'>
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users