Jump to content

help with image text


the-botman

Recommended Posts

Hi ...

 

see i need the text of the name and number to start in the center and always be in the center now with the code i have the text starts in the center and goes on to the rite it does not stay in the center any ideas guys?

 

here is my code

  $white = ImageColorAllocate($im, 255, 255, 255);
  $start_xx = 80; $start_yy = 50; 
  $start_x = 70; $start_y = 90; 
  Imagettftext($im, 15, 0, $start_xx, $start_yy, $white, 'image1.ttf', $_GET['name']);
  Imagettftext($im, 35, 0, $start_x, $start_y, $white, 'arial.ttf', $_GET['number']);

 

Thanks in Advance

Botman

Link to comment
Share on other sites

You're going to need to do a little of math to make the text be centered no matter what the string is. To calculate the starting point of the text we need to do: The width of the entire image / 2 - string width / 2. To find the string width of a ttf font we can use imagettfbbox. Example:

 

$bounds = imagettfbbox(35, 0, 'arial.ttf', $_GET['number']);
$start_x = IMAGE_WIDTH / 2 - ($bounds[2] - $bounds[0]) / 2; // $bounds[2] - $bounds[0] will be the width of the string, check the documentation for more info.

 

Untested, but should work. Make sure to replace IMAGE_WIDTH with the width of the image ($im) that you're dealing with.

 

imagettfbbox

Link to comment
Share on other sites

see this is how i do it rite now

<?php	
    $number = $_GET['number'];    
   $name = $_GET['name'];

  header("Content-Type: image/png"); 
  $im = imagecreatefrompng("soccer-kit.png");
  if(!$im){ 
  die("IMAGE CREATION FAILED");
}
  $white = ImageColorAllocate($im, 255, 255, 255);
  $start_xx = 80; $start_yy = 50; 
  $start_x = 70; $start_y = 90; 
  Imagettftext($im, 15, 0, $start_xx, $start_yy, $white, 'image1.ttf', $_GET['name']);
  Imagettftext($im, 35, 0, $start_x, $start_y, $white, 'arial.ttf', $_GET['number']);

  //Creates the png image and sends it to the browser 
  //100 is the png quality percentage 
  imagepng($im, NULL, 9);
  ImageDestroy($im); 
?>

and this is how i show the image   

 echo "<img src=\"image_generator.php?name={$_POST['name']}&number={$_POST['number']}\" />";

how do i make it stay in the center and add a download link i kinda understand what u are saying but i dont know how to add it to this please can you maybe show me

 

thanks alot

Botman

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.