0

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 png file and to save that file in a folder for later access.

I can convert text into png, but it is saving the text completely messed up.

<form method="post" accept-charset="utf-8">
<input type="text" name="test">
</form>
<?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' .
                      'ء|آ|أ|ؤ|إ|ئ|ا|ب|ة|ت|ث|ج|ح|خ|د|ذ|ر|ز|س|ش|ص|ض|ط|ظ|ع|غ|ف|ق|ك|ل|م|ن|ه|و|ى|ي|٫|ٮ|ٯ|پ' .
                      '!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
        $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;


}

?> 

I am writing " ترکی " and it is coming like below image.

enter image description here

please help me. thanks

Abulurd
  • 1,018
  • 4
  • 15
  • 31
usman610
  • 479
  • 1
  • 5
  • 22

0 Answers0