Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join Urdu alphabets while converting text into image in PHP

Tags:

php

urdu

I use TIC to convert text into images.

I have searched a lot on this but it seems like Unicode problem (unicodes of initial medial and final letters) or may be content type as image is in PNG.

If I echo without image conversion with content type text/html and charset=UTF-8 I get the desired output with join Urdu letters.

require_once 'lib/tic.php';
$text="زہرہ نور ";

TIC::factory('C:\Windows\Fonts\Nastalique.ttf')
->setText($text)
->setPadding(10)
->setBgColor('ff0000')
->setFontColor(0xff, 0xff, 0x00)
->setFontSize(24)->create(true);

Getting out put as

ز ہ ر ہ  ن و ر 
like image 377
Zehra Noor Avatar asked Jun 06 '15 06:06

Zehra Noor


1 Answers

You may do it like this:

$text = "زہرہ نور";

// Make it RTL
preg_match_all('/([^\d]|\d+)/us', $text, $ar);
$text = join('',array_reverse($ar[0]));

// Set font
$font = 'C:\Windows\Fonts\Nastalique.ttf';

// Create the image
$im = imagecreatetruecolor(160, 160);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// Create some colors
imagefilledrectangle($im, 0, 0, 159, 159, $white);

// Set the headers
header('Content-type: image/gif');

// Add the text
imagettftext($im, 12, 0, 20, 20, $black, $font, $text);
imagegif($im);
imagedestroy($im);

If did not work for you, you have the option to use php-gd-farsi.

How to use

Just copy the library to your PHP directory. The usage is simple:

include('php-gd-farsi-master/FarsiGD.php');
$gd = new FarsiGD();

....
// then convert your text:
$tx = $gd->persianText($str, 'fa', 'normal');

Complete code

include('php-gd-farsi-master/FarsiGD.php');

$gd = new FarsiGD();

// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);

// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $red);

// Path to our ttf font file
$font_file = './Nastalique.ttf';

// Draw the text 'PHP Manual' using font size 13
$text = imagecreatetruecolor(200, 60);
imagefilledrectangle($text, 0, 0, 200, 60, $red);
$str = 'زہرہ نور';
$tx = $gd->persianText($str, 'fa', 'normal');
imagefttext($text, 24, 10, 10, 50, $black, $font_file,$tx );
$im = $text;

// Output image to the browser
header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
like image 79
Pmpr.ir Avatar answered Oct 15 '22 21:10

Pmpr.ir