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
https://forums.phpfreaks.com/topic/183771-help-with-image-text/
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
https://forums.phpfreaks.com/topic/183771-help-with-image-text/#findComment-969996
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
https://forums.phpfreaks.com/topic/183771-help-with-image-text/#findComment-970006
Share on other sites

Archived

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

×
×
  • 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.