title: Writing text to an image using php
body: <p>I have this task working great! I have successfully wrote text to an image then displayed the image using php, however the problem I'm running into is getting the font size perfect and to fit within a contained area. Im using <code>imagettftext()</code></p>
  
  <p>I'm getting this to work however when i move the text to the desired location im manually changing the font to then get it to fit. I would like to text to perfectly fill a "box" inside the image.</p>
  
  <p>Here is my code</p>
  
  <p><strong>Main.php</strong></p>
  
  <pre><code>    check out the promo code below&lt;br /&gt;
  
      &lt;img src="imagem.php?promo=DISH120" alt="" /&gt;
  </code></pre>
  
  <p><strong>imagem.php</strong>
        $font = 'font-type.ttf';</p>
  
  <pre><code>    $rImg = ImageCreateFromJPEG( "test.jpg" );
  
      $color = imagecolorallocate($rImg, 255, 255, 255);
  
      imagettftext($rImg, 20, 0, 660, 130, $color, $font, urldecode($_GET['promo']));
  
      header('Content-type: image/jpeg');
      imagejpeg($rImg, NULL,100);
  </code></pre>
  
  <p>Here is the image im writing 
  <a href="https://i.stack.imgur.com/jQK94.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jQK94.jpg" alt="image"></a></p>
  
  <p>i want the text to write to the bottom right next to code, however when i make the font big enough to match the CODE height it way to wide.</p>
  
  <p><strong>ADDITION</strong></p>
  
  <pre><code> &lt;?php 
  function makeTextBlock($text, $fontfile, $fontsize, $width) 
  {    
      $words = explode(' ', $text); 
      $lines = array($words[0]); 
      $currentLine = 0; 
      for($i = 1; $i &lt; count($words); $i++) 
      { 
          $lineSize = imagettfbbox($fontsize, 0, $fontfile, $lines[$currentLine] . ' ' . $words[$i]); 
          if($lineSize[2] - $lineSize[0] &lt; $width) 
          { 
              $lines[$currentLine] .= ' ' . $words[$i]; 
          } 
          else 
          { 
              $currentLine++; 
              $lines[$currentLine] = $words[$i]; 
          } 
      } 
  
      return implode("\n", $lines); 
  } 
  ?&gt;
  </code></pre>
  
  <p>I found this function re formats a text string into a text block of a given width, this is kinda what im trying to achieve however, im not sure how to integrate this. I want a box which width and height never change that will be filled with text that its character count will change.</p>