Unicode characters is shuffled in php imagettftext - php

I am trying to print unicode characters in image using GD library
I have following code
$text = "கைவிடப்பட்ட";
$font = "arial.ttf";
$im = imagecreatetruecolor(2500, 500);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 2500, 500, $white);
imagettftext($im, 16, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
Original text here is கைவிடப்பட்ட , but it prints as following , you can see that first letter is replaced.
Edit : I have tried 5 different fonts All results in same(online font preview is showing characters correctly) . whereas when I echo the string via php(from database) to browser, output is exactly same(கைவிடப்பட்ட), but why imagettftext is switching characters..

I am not sure about GD library and environment but I guess the issue is due to rendering. கை = combination of க + ை and the environment doesn't support the rendering for unicode while displaying. so either check any rendering solution or use non unicode fonts or do below workaround to tackle it in php.
$text = "கைவிடப்பட்ட";
$imgtext = tamilrender($text);
$font = "arial.ttf";
$im = imagecreatetruecolor(2500, 500);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 2500, 500, $white);
imagettftext($im, 16, 0, 10, 20, $black, $font, $imgtext);
imagepng($im);
imagedestroy($im);
function tamilrender($input)
{$input= preg_replace("/([க-ஹ])([ைேெ])/u", "$2$1", $input);
$input= preg_replace("/([க-ஹ])ொ/u", "ெ$1ா", $input);
$input= preg_replace("/([க-ஹ])ோ/u", "ே$1ா", $input);
return $input;}

Limitations of Unicode
The Unicode standard policy is to encode only characters, not glyphs. கை, includes a combination character onto க. of Tamil is treated a complex script that requires complex processing, and many software components don't properly handle it.
The best approach would be to find, or create, a php extension library which was written specifically for Tamil.

Related

Text to Image function just outputs garbage

I'm attempting to convert text to an image with a custom font using the textttftext() function. I've scoured the internet for various solutions and nothing seems to work. I've ensured FreeType is enabled, I've attempted the putenv variant as mentioned in the docs, and I've tried different font files. No matter what I try, when I run the script it just outputs garbage to my console (like trying to cat a binary file). Other text-to-image functions work, just not textttftext(). Script is below, almost literally just ripped from the docs (also, I'm using Ubuntu in an EC2 but was having the same issues using WSL):
<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$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);
$text = 'Testing...';
//imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
putenv('GDFONTPATH=' . realpath('.'));
$font = 'arial';
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>

Weird image glitch when using imagepng()

I am trying to make a gamecard for a RSPS. This is my first time using PHP to generate text on images and when trying to apply this weird glitch happens:
But, when the original photo looks like this:
As you can see the corners are not curved like they should be. But, in the original it comes out fine. I am not talking about the location of the numbers.
Here is the code I am using:
<?php
header('Content-Type: image/png');
// Image Creation
$image_file = "SoulSplitCard.png";
$im = imagecreatefrompng($image_file);
//Colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$rscol = imagecolorallocate($im, 23, 113, 183);
// Levels (for now)
$text = '99';
$text2 = '87';
// Font
$font = 'arial.ttf';
//Text Applications
imagettftext($im, 15, 0, 150, 35, $rscol, $font, $text);
imagettftext($im, 15, 0, 150, 81, $rscol, $font, $text2);
// Using imagepng() instead of imagejpeg()
imagepng($im);
imagedestroy($im);
?>
What is causing this to happen?

How to convert Unicode Indian language (Malayalam) to Image using PHP

Here is my php code for creating an image with text inside,
I used Malayalam language text but PHP generating an image like this "?????"
<?php mb_language('uni');
mb_internal_encoding('UTF-8');
header('Content-type: image/gif');
$text = 'മലയാളം ';
$font = 'mlkarthika.ttf';
$im = imagecreatetruecolor(160, 160);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 159, 159, $white);
imagettftext($im, 12, 0, 20, 20, $black, $font, $text);
imagegif($im);
imagedestroy($im);
?>
the font ML_ttkarthika is avalable in unicode font website, please help me
There are issues with ImageTTFText and Unicode.
You may want to look at this: PHP function imagettftext() and unicode

How can I write arabic text in image in PHP

I tried to write an Arabic text in an image with PHP.
This is my code:
<?php
header("Content-Type: image/png; charset=utf8");
$im = imagecreatetruecolor(150, 30);
// Create some colors
$white = imagecolorallocate($im, 100, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$font = 'tahoma.ttf';
$t = strrev('إن الدين عند الله الإسلام');
$text=utf8_encode($t);
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>
Thank you.
What’s the output?
The strrev function is not unicode compatible
http://de2.php.net/manual/en/function.strrev.php
See the comments there for examples of unicode compatible ones.
Also see the imagettftext page,
I think the following comment is exactly what you need:
http://de2.php.net/manual/en/function.imagettftext.php#97767
This will work for you:
include("fagd.php");
use imagettftext($im, 20, 0, 10, 20, $black, $font, fagd($text));
fagd.php codes are here:
http://cdn.shirazitco.ir/fagd.txt

Fitting Text into an Image

I have a function which takes in a font (ttf or otf file) and generates an image of text in different fonts.
The problem I have is trying to work out how I can make the text fit in the image regardless of the font-size, type of font, and amount of text.
I have tried to make the image rectangle variable so that it contains the text of different fonts without cutting a bit of the text since the image is not long or wide enough.
Here is the function that I currently have, I have tried using the number of characters to determine the width of the image, but in some cases for some fonts and sizes, it still gets cut off.
function generate_image($save_path, $text, $font_path){
$length = strlen($text) * 15;
// Create the image
$im = imagecreatetruecolor($length, 40);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $length, 40, $white);
$font = $font_path;
imagettftext($im, 30, 0, 0, 25, $black, $font, $text);
if(imagepng($im, $save_path)){
$status = true;
}else{
$status = false;
}
imagedestroy($im);
return $status;
}
Thank you all for any help
imagettfbbox()

Categories