title: how to center and align texts by merging two images using PHP GD
body: <p>How do I centralize and align text above my main image?</p>
  
  <p>The result of this code is this image:
  <a href="https://i.imgur.com/Dk5pYJM.jpg" rel="nofollow noreferrer">https://i.imgur.com/Dk5pYJM.jpg</a></p>
  
  <p>I wish it would look like this:
  <a href="https://i.imgur.com/mED295l.jpg" rel="nofollow noreferrer">https://i.imgur.com/mED295l.jpg</a></p>
  
  <p>but I do not understand much of moving the images and texts.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  $fontname = 'verdana.ttf';
  $i = 30;
  $quality = 85;
  function create_image($user){
      global $fontname;
      global $quality;
      $file = md5($user[1]['text'].rand(30454, 343434)).".jpg";
  
      //if (!file_exists($file)) {
          $im   = imagecreatefromjpeg("fundo.jpg");
          $logo = imagecreatefromjpeg("img.jpeg"); 
          $color['grey']  = imagecolorallocate($im, 255, 255, 255);
          $y = imagesy($im) - $height - 365;
          $font_size = 25;
          $logo_x = imagesx($logo); 
          $logo_y = imagesy($logo); 
          
          foreach ($user as $value){
              $x = center_text($value['text'], $font_size);
              imagettftext($im, $font_size, 0, $x, $y+$i, $color["grey"], $fontname, $value['text']);
              $i = $i+32;
  
          }
          imagecopymerge($im, $logo, 37, 370, 0, 0, $logo_x, $logo_y, 100);
          imagejpeg($im, $file, $quality);
      //}
      return $file;
  }
  
  function center_text($string, $font_size) {
      global $fontname;
      $image_width = 720;
      $dimensions = imagettfbbox($font_size, 0, $fontname, $string);
      return ceil(($image_width - $dimensions[4]) / 2);
  }
  
  $user = array(
      array('text'=&gt; 'Our adge lacks gravitas. That’s whyaadasdasdasdasdsadasdasdasdas')
  );
  $filename = create_image($user);
  ?&gt;
  &lt;img src="&lt;?=$filename;?&gt;" /&gt;&lt;br/&gt;&lt;br/&gt;</code></pre>
  </div>
  </div>
  </p>