Generating image with regional language - php

Hi i am trying to generate image with regional language.for normal english i do this
// 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 = 'Testing...';
// Replace path by your own font path
$font = 'arial.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 for regional language it won't working...how can i do that

I wasn't able to reproduce the problem with this piece of code on my machine.
If the strings are coming from the database, is probable that there is a charset problem on it, don't forget to use:
$dbh = new PDO("mysql:$connstr", $user, $password);
$dbh->exec("set names utf8");
when connecting to the database.
It should solve your problem.

Related

PHP text to image using stylistc set in OTF font

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

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?

Make imagettftext accept the whole pwgen -ny input, not just the first password

So I have this:
https://indigodaddy.koding.com/
(Basically straight out of http://us2.php.net/manual/en/function.imagettftext.php)
<?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 = `../../../bin/pwgen -ny 12`;
// Replace path by your own font path
$font = 'font/DejaVuSans-Bold.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);
?>
How would I make the txt to image conversion accept the entire input of pwgen -ny -12? This outputs 6 columns of 12-char passwords, eg, like this: http://pwgen.net/. I want that entire output converted to the png, not just the first password, as it is doing now.
Any ideas?
** Edit, as requested, sample output of pwgen -ny 12:
Uo/thengohy5 phie=Tahw8ko Cohh1seiz]ua ooY#ee4chei) noo}Mioyei5u au$x5Isaigho
aht6vahGia{j aeph8yoh#kuY EeNgees#ae4k boh5yie1Ic\e aiSh5bahp]ai kiex%ahL0Oob
bei|G{ohm8ae Ieth|ee|wee9 koo#R2iZeot" Nai5Phain(ie Oam*ub0thoth quu6Eyimohd-
geeNgool}ee1 Oow5Pie_sija ab#ahcoo8eeJ ahcoe_r6Vae3 pi%wiedeit4A biegh}u#vu6K
euwei.gei9Of ahr?ai2UquaZ ha3eiXee"koo eiLohf*u1sao ave&aqui8Hie oc-a#z'oh1Le
iv$eeX0eo0Wu rel7aGh6gee& baig3ooN%uz, eoj9eGo.haih Aoka4Uizae:r aiG6aiP.iera
IaY?oo0Aibai pekaeth5Aeh% taav*eiK5Soo zohL5haeng?o teT^u3yash3S xudoh%Dai4ph
tho`R\ahd7ae ohf#e9Ceeph7 Uce7ou{woe8o eeb9jo!Y#aep la7Voo.chae7 te;y5Choo4ie
neeZ5out'oo) ze{iw4Aethok fah3Shoh^Re9 Quoo8au%koot xo2chu>Lae0e Shee#g1eibak
que5Og3aiN&e aw(eetooJ5Ei aeV8ging(eib ohGh9Chah[ch ieYiv5gagh-a kahm2yohR%uo
Eiraivi!o}W0 eo0Oot{ai5sh saT[ae2ahj5O Aefae,l5ieth jo0iJ,ai5eih deer9Aigol'i
aet1Ce,za+fi OFi3exoh)ng3 tae7ooGhaim/ nei$V8Ohh!ae eiy~ia4Itoh; Baisish+ai5d
iib+ooHaoj4t Ayac3aiCh>ee ka/eChoosh2c vanu1Zeex#uy phieB(ai8iev Ou|Gh3zuwo,f
ool3AxiNo~ch zai3Eew&ie*w Zee7eey4eJ*o kooree9Va>B5 FaiB-ah8yahK oha4uP3Oth<o
Aegah2aesh.u eiQuuduu>v1M ae5re8yaXuw% gei5rua1Lu/i eeNg3uyaf"ei Ootee|h8Shai
sa6ed0nieF+e aed8rah;Kai4 boghahH]a7ei eiba$Roh5uu' Ook6oonae`th Vai9taer#oo?
aich7ohMoi/d Ohh1ew-aa"xi Eig%ae]jo2ne urooX1iagh|u iiY8reew,i8d eGhee4fahw(a
ieS%ai1ieh4e pun9Ich0jee; Waer_ai1ieph re)Che`xud0A Aek2wioh\a2u deedae0Ai[be
ohr!a4Ohtae* keNai8aes%oh ookiM3uiRei~ fei-Boo2tu3i coo=c4Yie?w8 ThaiN7fe$o"t
ahw[uChae3va Ej5Evaa&Giix seat|eeZ1yae ee5wiengo]Ba Oo2aThu^p!ai ez{i4too8ohD

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