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
ز ہ ر ہ ن و ر
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);
Related
I'm trying to output a text to image using a font that has 7 stylistic sets. I'm using the code below but can't find how I can use a style in the output. Any advice would be greatly appreciated.
<?php
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'This is a test!';
// Replace path by your own font path
$font = 'joined.otf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
I am trying to generate image with some text and I have wrote following code:
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = '준수';
// Replace path by your own font path
$font = 'fonts/Walkway Black RevOblique.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Now when I add text with English characters it works but when I used Korean characters is not working and getting this image.
Any idea how to display Korean text?
Thanks
Looks like Walkway Black RevOblique.ttf is not Korean font. Try to download and use Korean one.
I want to display hindi words on an image. Here is the code for it, it is working properly but it does not display "chote E ki matra" correctly. Example "अब हिन्दी" is displayed as : http://prntscr.com/8rj8mq
Also hindi.txt file contains these :
अब हिन्दी
मे टाइप
करना बहुत आसान है
Below php script read one line randomly and put it on image and display it. Can someone help me to correct this....
<?php
$word= file("hindi.txt");
$random = rand(0,2);
$text = $word[$random];
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
// Replace path by your own font path
$font = 'Mangal.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
I would like to generate an image with some text on it, the LTR languages seems to be working fine, but when trying it in arabic, it, simply, didn't work, see the screenshot bellow where I draw 3 text strings with the presented text code
Here my test code (with some comments):
// Create a 300*300 image
$im = imagecreate(300, 300);
// Yellow transparent background and blue text
$bg = imagecolorallocatealpha($im, 255, 255, 0, 45);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write an LTR string
imagestring($im, 5, 250, 100, 'test', $textcolor);
$font = 'DroidKufi-Regular.ttf'; // specify the fonts file
$text = "تجربة";
// Add the text
imagettftext($im, 10, 0, 10, 20, $textcolor, $font, $text);
// set a red text color
$textcolor = imagecolorallocate($im, 255, 0, 0);
// strrev doesn't seem to solve the problem
$text = strrev( $text );
// add the text
imagettftext($im, 10, 0, 30, 250, $textcolor, $font, $text);
imagefilledrectangle ($im, 0, 0, 1, 1, $bg);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
Finally I've found a solution in some other forums, which is this tiny library word2uni and is worked perfectly, the whole thing is about converting the arabic letters into unicode symboles, practically, converting this:
$text = 'العربية';
to this:
$text = 'ﺔﻴﺑﺮﻌﻟﺍ';
and using, that library, which is making this conversion, so in my previous code it would work like that (after including the library):
// Create a 300*300 image
$im = imagecreate(300, 300);
// Yellow transparent background and blue text
$bg = imagecolorallocatealpha($im, 255, 255, 0, 45);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write an LTR string
imagestring($im, 5, 250, 100, 'test', $textcolor);
$font = 'DroidKufi-Regular.ttf'; // specify the fonts file
$text = "تجربة";
$text = word2uni( $text );
// Add the text
imagettftext($im, 10, 0, 10, 20, $textcolor, $font, $text);
// set a red text color
$textcolor = imagecolorallocate($im, 255, 0, 0);
// strrev doesn't seem to solve the problem
$text = strrev( $text );
// add the text
imagettftext($im, 10, 0, 30, 250, $textcolor, $font, $text);
imagefilledrectangle ($im, 0, 0, 1, 1, $bg);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
( the main thread )
you can check this library
http://www.ar-php.org/Glyphs-example-php-arabic.html
here is an example how to use it
require('../I18N/Arabic.php');
$Arabic = new I18N_Arabic('Glyphs');
$text = 'بسم الله الرحمن الرحيم';
$text = $Arabic->utf8Glyphs($text);
What is the best way to display underlined text and output the result as image with GD or any other library?
You can try using the Unicode underline combining character U+0332.
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = "̲U̲d̲e̲r̲l̲i̲n̲e";
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
There are lots of FREE PHP CAPTCHA out there that come with a lot of customization, download one and see what exactly happens behind the scene. Also have a look at this link
HTH
I am using this...
$font = imageloadfont($font_file);
$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);
$str_width = strlen($text)*$font_width;
ImageLine($image, $left, $top+$font_height, $left+$str_width, $top+$font_height, $color);