title: Convert text file to png and store it in folder with PHP
body: <p>What I want to do is, I have a text area where users enter Arabic text, when user submits the text, the text is to be converted to <code>png</code> file and to save that file in a folder for later access.</p>
  
  <p>I can convert text into <code>png</code>, but it is saving the text completely messed up.</p>
  
  <pre><code>&lt;form method="post" accept-charset="utf-8"&gt;
  &lt;input type="text" name="test"&gt;
  &lt;/form&gt;
  &lt;?php
  $test   =   $_POST['test'];
  $text = ''.$test;
  $font = 'alvinastaleeq';
  $font_color = '000';
  $background_color = 'fff';
  $font_size = '41';
  $filename = 'image.png';
  
  if(text_to_PNG_file($text, $font, $font_color, $background_color, $font_size, $filename)){
      print 'The text was saved to '.$filename.'.';
  }else{
      print 'There was an error saving the text';
  }
  
  function text_to_PNG_file($text, $font, $font_color, $background_color, $font_size, $filename)
  {
  
      function get_dip($font,$size)
      {
          $test_chars = 'abcdefghijklmnopqrstuvwxyz' .
                        'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
                        '1234567890' .
                        'ء|آ|أ|ؤ|إ|ئ|ا|ب|ة|ت|ث|ج|ح|خ|د|ذ|ر|ز|س|ش|ص|ض|ط|ظ|ع|غ|ف|ق|ك|ل|م|ن|ه|و|ى|ي|٫|ٮ|ٯ|پ' .
                        '!@#$%^&amp;*()\'"\\/;.,`~&lt;&gt;[]{}-+_-=' ;
          $box = @ImageTTFBBox($size,0,$font,$test_chars) ;
          return $box[3] ;
      }
  
  
      function hex_to_rgb($hex)
      {
          // remove '#'
          if(substr($hex,0,1) == '#')
              $hex = substr($hex,1) ;
  
          // expand short form ('fff') color
          if(strlen($hex) == 3)
          {
              $hex = substr($hex,0,1) . substr($hex,0,1) .
                     substr($hex,1,1) . substr($hex,1,1) .
                     substr($hex,2,1) . substr($hex,2,1) ;
          }
  
          if(strlen($hex) != 6) return FALSE;
  
          // convert
          $rgb['red'] = hexdec(substr($hex,0,2)) ;
          $rgb['green'] = hexdec(substr($hex,2,2)) ;
          $rgb['blue'] = hexdec(substr($hex,4,2)) ;
  
          return $rgb ;
      }
      $font_directory = '';
      $font_file  = $font_directory . $font . '.ttf' ;
      $transparent_background  = true ;
      $mime_type = 'image/png' ;
      $send_buffer_size = 4096 ;
  
      // check for GD support
      if(!function_exists('ImageCreate')) return FALSE;
  
  
      // clean up text
      if(empty($text)) return FALSE;
  
  
      // check font availability
      if(!$font_found = is_readable($font_file)) return FALSE; 
  
  
      // create image
      $background_rgb = hex_to_rgb($background_color) ;
      $font_rgb = hex_to_rgb($font_color);
      $dip = get_dip($font_file,$font_size) ;
      $box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
      $image = @ImageCreate(abs($box[2]-$box[0]),abs($box[5]-$dip)) ;
      if(!$image || !$box) return FALSE;
  
  
      // allocate colors and draw text
      $background_color = @ImageColorAllocate($image,$background_rgb['red'],
          $background_rgb['green'],$background_rgb['blue']) ;
      $font_color = ImageColorAllocate($image,$font_rgb['red'],
          $font_rgb['green'],$font_rgb['blue']) ;   
      ImageTTFText($image,$font_size,0,-$box[0],abs($box[5]-$box[3])-$box[1],
          $font_color,$font_file,$text) ;
  
      // set transparency
      if($transparent_background)
          ImageColorTransparent($image,$background_color) ;
  
      ob_start();
      ImagePNG($image);
      if($fp = fopen($filename, "w") and fwrite($fp, ob_get_clean()) and fclose($fp)){
          return TRUE;
      }
      return FALSE;
  
  
  }
  
  ?&gt; 
  </code></pre>
  
  <p>I am writing "  ترکی  " and it is coming like below image.</p>
  
  <p><img src="https://i.stack.imgur.com/Bn4kb.png" alt="enter image description here"></p>
  
  <p>please help me. thanks</p>