title: Using PHP GD to create image form text with different fonts
body: <p>I have been using this simple script to generate images from text:</p>
  
  <pre><code>&lt;?php
  header('Content-type: image/png');
  
  $color = RgbfromHex($_GET['color']);
  
  $text = urldecode($_GET['text']);
  
  $font = 'arial.ttf';
  
  $im = imagecreatetruecolor(400, 30);
  
  $bg_color = imagecolorallocate($im, 255, 255, 255);
  
  $font_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);
  
  imagefilledrectangle($im, 0, 0, 399, 29, $bg_color);
  
  imagettftext($im, 20, 0, 10, 20, $font_color, $font, $text);
  
  imagepng($im);
  
  imagedestroy($im);
  
  function RgbfromHex($hexValue) {
      if(strlen(trim($hexValue))==6) {
          return array(
                       hexdec(substr($hexValue,0,2)), // R
                       hexdec(substr($hexValue,2,2)), // G
                       hexdec(substr($hexValue,4,2))  // B
                      );
      }
      else return array(0, 0, 0);
  }
  
  ?&gt;
  </code></pre>
  
  <p>I call the script with file.php?text=testing script&amp;color=000000</p>
  
  <p>Now I'd like to know how could I generate text with normal and bold fonts mixed in the same image, something like <code>file.php?text=testing &lt;b&gt;script&lt;/b&gt;&amp;color=000000</code></p>
  
  <hr>
  
  <p>Thanks to dqhendricks for helping me figure this out.</p>
  
  <p>Here's a quick script I wrote, still needs lot of improvements but for the basic functionality it seems to be working fine:</p>
  
  <pre><code>&lt;?php
  header('Content-type: image/png');
  
  $color = RgbfromHex($_GET['color']);
  
  $im = imagecreatetruecolor(400, 30);
  
  $white = imagecolorallocate($im, 255, 255, 255);
  
  imagefilledrectangle($im, 0, 0, 399, 29, $white);
  
  $tmp = $_GET['text'];
  
  $words = explode(" ", $tmp);
  
  $x = array(0,0,10); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD
  
  $addSpace = 0;
  
  foreach($words as $word)
  {
      if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE
  
      if(stristr($word, "&lt;b&gt;"))
      {
          $font = 'arialbd.ttf'; // BOLD FONT
          $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, str_replace(array("&lt;b&gt;","&lt;/b&gt;"), "", $word));
      }
      else
      {
          $font = 'arial.ttf'; // NORMAL FONT
          $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, $word);
      }
  
      $addSpace = 1;
  }
  
  imagepng($im);
  
  imagedestroy($im);
  
  function RgbfromHex($hexValue) {
      if(strlen(trim($hexValue))==6) {
          return array(
                       hexdec(substr($hexValue,0,2)), // R
                       hexdec(substr($hexValue,2,2)), // G
                       hexdec(substr($hexValue,4,2))  // B
                      );
      }
      else return array(0, 0, 0);
  }
  
  ?&gt;
  </code></pre>
  
  <p>Note: This will only work for "bolding" single words separated by spaces and not for bolding part of a word.</p>
  
  <p>Call the script with <code>file.php?text=testing+&lt;b&gt;script&lt;/b&gt;&amp;color=000000</code></p>