How can I write arabic text in image in PHP - 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

Related

Imagettftext not producing correct results if I use the tamil lanuage ttf?

I am trying to write Tamil text on an image. I tried with different font files like latha.ttf and
Uni Ila.Sundaram-01.ttf files. The problem is that the vowels are getting misplaced. Please see the image for reference. Can anyone please suggest to me how to overcome this issue?
I use the code below:
<?php
header('Content-type: image/png; charset=UTF-8');
$text ='சுற்றிப்பார்க்க ';
$font = './fonts/UniIlla.ttf';
$im = imagecreatetruecolor(600, 300);
$bg_color = imagecolorallocate($im, 255, 255, 255);
$font_color = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 599, 299, $bg_color);
imagettftext($im, 20, 0, 10, 50, $font_color, $font, $font);
imagettftext($im, 20, 0, 10, 120, $font_color, $font, $text);
imagepng($im);
imagedestroy($im);
?>

php how add transparent background color for php image?

background is comming in white i want to remove white and put transparent.please help
this is an image: http://funbusy.com/fbtest/user_image.php
?php
// Create the image
$im = imagecreatetruecolor(400, 25);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 22, 125, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = ucwords('tanuja sree');
// Replace path by your own font path
$font = 'OpenSans-Italic.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()
ob_start();
imagepng($im);
$image_data = ob_get_clean();
$image_code = '<img id="image_code" src="data:image/png;base64,'. base64_encode($image_data) . '">';
imagedestroy($im);
?>
Try adding this piece of code after imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagecolortransparent($im, $white);
imagesavealpha($im,true);
imagealphablending($im, true);

Hindi text to Image - words not appearing properly PHP

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);
?>

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 add a background image while creating a text image using php?

I'm currently using the following script--
<?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, 115, 150, 195);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'My Name';
// Replace path by your own font path
$font = 'AGENCYB.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);
?>
But I want to add a background image too. Please help, I'm new to this function especially.
Would something like the following work for you? You want to open the image you want to use as the background, and then write your text over the top.
<?php
// Set the content-type
header('Content-type: image/png');
/* Attempt to open */
$im = #imagecreatefrompng('backgroundimage.png');
/* See if it failed */
if(!$im)
{
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 115, 150, 195);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'My Name';
// Replace path by your own font path
$font = 'AGENCYB.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);
}else
{
//you want to do something here if your image didn't open like maybe fpassthru an alternative image
}
?>

Categories