title: Draw text with GD with transparent background
body: <p>I have this code to write some text on a new created image:</p>
  
  <pre><code>class ImageCreator
  {
      public function Draw($string)
      {
          $font = "lg.ttf";
  
          $txtsize = imagettfbbox(20, 0, $font, $string);
          $image = imagecreatetruecolor($txtsize[2], $txtsize[4]);
          imagealphablending($image, false);
          $c = imagecolorallocatealpha($image,0,0,0,127);
          imagefilledrectangle($image,0,0,$txtsize[2],$txtsize[4], $c);
          imagealphablending($image, true);
  
          $c = ImageCreator::hexcol2dec("#FFFFFF");
          $white = imagecolorallocate($image, $c[0], $c[1], $c[2]);
          $c = ImageCreator::hexcol2dec("#000000");
          $black = imagecolorallocate($image, $c[0], $c[1], $c[2]);
  
          $c = ImageCreator::hexcol2dec("#044D8F");
          $tcolor = imagecolorallocate($image, $c[0], $c[1], $c[2]);
  
          imagettftext($image, 20, 0, 0, 0, $black, $font, $string);
          imagettftext($image, 20, 0, 1, 0, $tcolor, $font, $string);
          imagettftext($image, 20, 0, 2, 0, $white, $font, $string);
  
          imagealphablending($image,false);
          imagesavealpha($image,true);
          ob_start();
          imagepng($image);
          $imgData = ob_get_contents();
          ob_end_clean();
          return $imgData;
      }
  
      public function hexcol2dec($hexColor)
      {
          $R=0;
          $G=0;
          $B=0;
  
          for($i = 0; $i &lt; strlen($hexColor);$i++)
          {
              if($hexColor[$i] == "#")
              {
  
              }
              else if($hexColor[$i] == 'A' || $hexColor[$i] == 'a')
              {
                  $dec[$i] = 10;
              }
              else if($hexColor[$i] == 'B' || $hexColor[$i] == 'b')
              {
                  $dec[$i] = 11;
              }
              else if($hexColor[$i] == 'C' || $hexColor[$i] == 'c')
              {
                  $dec[$i] = 12;
              }
              else if($hexColor[$i] == 'D' || $hexColor[$i] == 'd')
              {
                  $dec[$i] = 13;
              }
              else if($hexColor[$i] == 'E' || $hexColor[$i] == 'e')
              {
                  $dec[$i] = 14;
              }
              else if($hexColor[$i] == 'F' || $hexColor[$i] == 'f')
              {
                  $dec[$i] = 15;
              }
              else
              {
                  $dec[$i] = $hexColor[$i];
              }
          }
          if($hexColor[0] == "#")
          {
              $R = 16*$dec[1]+$dec[2];
              $G = 16*$dec[3]+$dec[4];
              $B = 16*$dec[5]+$dec[6];
          }
          else
          {
              $R = 16*$dec[0]+$dec[1];
              $G = 16*$dec[2]+$dec[3];
              $B = 16*$dec[4]+$dec[5];
          }
          return array ($R, $G, $B);
      }
  
  
  }
  </code></pre>
  
  <p>and i get just the transparent background none of the text is showed. I am new at PHP GD and i can't figure out why isn't writing the text. Please help me figure out</p>
  
  <p>Thanks.</p>