title: PHP GD - Align text center-horizontally and decrease font size to keep it inside the image
body: <p>Hope you are doing great.</p>
  
  <p>I’m still a newbie with php so after making some reading and while checking some posts here I was able to put some text over an image with the imagecreatefrompng()  function using the PHP GD, users will come to a form and they will be able to enter their name and the name will be written over the image , unfortunately I have been unable to align the text center horizontally, I tried all ways possible (my ways obviously and must be wrong) with imagettfbbox but I failed in all my attempts, could you please guys help me out a little bit to align the string center horizontally? Also since I’m using a kind of alternative big font I need that the size decrease if the name entered is kind of long so this way it will not surpass the image limits and will stay at the center.  I’m getting the value of the text from a form as you may check at the beginning of my code:</p>
  
  <pre><code>&lt;?php
   $nombre=$_POST['nombre'];
    //Set the Content Type
    header('Content-type: image/jpeg');
  
    // Create Image From Existing File
    $jpg_image = imagecreatefromjpeg('fabian.jpg');
  
    // Allocate A Color For The Text
    $white = imagecolorallocate($jpg_image, 255, 255, 255);
  
    // Set Path to Font File
    $font_path = 'fabian.TTF';
  
    // Set Text to Be Printed On Image , I set it to uppercase
    $text =strtoupper($nombre);
  
    // Print Text On Image
    imagettftext($jpg_image, 75, 0, 50, 400, $white, $font_path, $text);
  
  
  
    // Send Image to Browser
    imagepng($jpg_image);
  
    // Clear Memory
    imagedestroy($jpg_image);
  
    ?&gt;
  </code></pre>
  
  <p>Your help will be highly appreciated, later on I will break my head trying to save the image by clicking a submit button since I do not want the users to save the image by right clicking on it.</p>
  
  <p>Thanks pals!</p>