error while converting text to image using php - php

// 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 = '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);
?>
The output text is not same as the input text

Yes, you can use <canvas> for that, see: https://developer.mozilla.org/en/Drawing_text_using_a_canvas

Not directly; javascript isn't supposed to be allowed to poke around in the guts of binary elements like images. You could render the text using a canvas element, or alternatively apply WebGL, but this is probably not what you want. If it absolutely has to be an image, the most portable way to do that is to make a web app that renders the text to an image and echos it back to a client. Using this, you can easily take a blob of text, create an image element that pulls the object back and then display that in the browser.

Related

PHP - imagettftext not working

I am trying to generate images containing data from my database, and I want to use a custom font for it, so I looked up the imagettftext. I am following its syntax, but it's not printing anything. What am I doing wrong?
When I use imagestring it works fine. But that does not use any custom font.
I have placed my font.tff file in the same folder as my script. Here is my code:
header('Content-Type: image/png');
$image = #imagecreate(400, 110);
$backgrund = imagecolorallocate($image, 70, 130, 180);
$textcolor = imagecolorallocate($image, 255, 255, 255);
$font = 'font.ttf';
// TTF
imagettftext($image, 10, 0, 15, 15, $textcolor, $font, 'Test TTF');
// Normal text
imagestring($image, 2, 15, 30, 'Test Normal Font', $textcolor);
imagepng($image, 'images/test.png');
imagedestroy($image);
Am I missing something? The first line is completely blank. I also tried adjusting the font size (it maybe is different than regular imagestring?) but still nothing. The font is a .ttf font file.

Get PHP's imagefttext() to show Chinese characters

I am trying to get imagefttext() to display Chinese characters, but it just displays square boxes
Here is some simplified code to show the problem
<?php
// 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 = 'C:\\Windows\\Fonts\\arial.ttf';
// Draw the text '牧師' using font size 13
imagefttext($im, 13, 0, 105, 55, $black, $font_file, '牧師');
// Output image to the browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
This is what it produces:
Is it to do with the font, or some other character set setting in PHP?
I've tried various fonts, but I just can't seem to get the
牧師
characters to display
I have managed to get them to show up by using the SimSun font
Replacing
$font_file = 'C:\\Windows\\Fonts\\arial.ttf';
With
$font_file = 'C:\\Windows\\Fonts\\simsun.ttc';
Makes it work
[Please download font at your font folder and try because i have already done this text at my local server]
http://www.fontpalace.com/font-download/SimSun/

Display Chinese characters over an image

I am trying to use PHP to display some Chinese characters over an image. The code is provided below.
<?php
$im = imagecreatetruecolor(60, 20);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 60, 20, $white);
$text = '你好';
$font = 'simsun.ttc';
imagettftext($im, 10, 0, 5, 15, $black, $font, $text);
imagepng($im, 'pic.jpg');
imagedestroy($im);
?>
I can display those two Chinese characters on my PC correctly but I cannot display them on my server. When I typed in the following command line, and the warning message showed up.
$ php font.php
PHP Warning: imagettftext(): Could not read font in /usr/share/nginx/html/model/font.php on line 9
I did some search on the internet and found that some said imagettftext may not be able to handle ASCII codes whose values are greater than 127. The point is I am able to display them on my PC. I wonder how I can fix this problem.
I found the problem. Two things: first, you need to use the right font file; second, you need to use the full path of the font file. Namely, I should use
$font = '/usr/share/nginx/html/model/fireflysung.ttf';
NOT
$font = 'fireflysung.ttf';

PHP GD text is not smooth

Screenshot of problem:
I'm trying to get the same font quality such as Font Squirrel's sample fonts widget, but the font keeps coming out rough. It's smooth in Photoshop. Note: the "The lazy dog" part isn't being bolded by me, its doing it by itself.
Here's the PHP:
<?php
putenv('GDFONTPATH=' . realpath('.'));
$font = $_GET['font'] . '.ttf';
$text = 'The Quick Brown Fox Jumps over the Lazy Dog';
// Create the image
function imageCreateTransparent($x, $y) {
$imageOut = imagecreate($x, $y);
$colourBlack = imagecolorallocate($imageOut, 0, 0, 0);
imagecolortransparent($imageOut, $colourBlack);
return $imageOut;
}
$image = imageCreateTransparent(600, 800);
// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$grey = imagecolorallocate($image, 128, 128, 128);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 399, 29, $white);
// Add the text
imagettftext($image, 20, 0, 10, 20, $black, $font, $text);
imagepng($image);
imagealphablending($image, true);
imagedestroy($image);
?>
HTML: <img src="fontgen.php?font=Aller_Rg" alt="" />
How can I get that high quality result for the font?
You've only set part of the background to be white, the rest of it is transparent.
When the font is drawn over a white background, the black text is anti-aliased so that it looks smooth, which results in the pixels around the font being drawn as a blend between the two colours, which also makes the font look smaller.
On the right hand side there is no background colour so the anti-aliasing is not working properly. Instead of blending between the font colour and the background colour, the drawing algorithm is using the original font colour for any pixel that is even partly covered by a letter.
This makes the letters look 'bold' as the edge pixels are now black, instead of shades of grey.
The way to fix this properly is to use an image that has a proper background colour, even if that background colour is transparent. This makes the image library use a proper alpha-channel (which is the only sensible way of doing alpha blending) rather than using an indexed based alpha, where only one 'colour' is transparent and all the others are fully opaque.
$font = '../../fonts/Aller_Rg.ttf';
$text = 'The Quick Brown Fox Jumps over the Lazy Dog';
// Create the image
function imageCreateTransparent($x, $y) {
$imageOut = imagecreatetruecolor($x, $y);
$backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127);
imagefill($imageOut, 0, 0, $backgroundColor);
return $imageOut;
}
$image = imageCreateTransparent(600, 800);
// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$grey = imagecolorallocate($image, 128, 128, 128);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 399, 29, $white);
//// Add the text
imagettftext($image, 20, 0, 10, 20, $black, $font, $text);
//imagealphablending($image, true); //not needed as we created the image with alpha
imagesavealpha($image, true);
//imagepng($image, '../../var/log/wtf5.png');
imagepng($image);
imagedestroy($image);
This will make the font size be right as the anti-aliasing will work correctly* and the image will be transparent where appropriate e.g. the image created with the code above, shown over a red background.
The bits of the image that have a white background are white, the bits of the image that are transparent let the red colour come through, and the text is anti-aliased correctly to both.
*assuming that you want to anti-alias to what the background colour was set to, which is not always the case but probably is here.
I suspect it's because you're making the background explicitly white only in the left 400 px. To the right of that it's probably still transparent and has some side-effects. The r is the first letter that starts beyond the white background you created earlier.

Adding inner shadow to text

I'm looking for the way to add inner shadow to text using PHP. I'm not looking for a solution, which includes HTML and/or CSS. Image must be generated using only PHP.
For examle: http://i.imgur.com/jYGvM.png
From the text above ('Original') I want to create modify text, so it will look like text at the bottom ('Shadow').
Effect must be achieved using only GD library or Imagemagick, because I can't install new libraries to the server.
One way is to draw your text twice with different colors and a small offset.
Below is the sample code, mainly taken from the php manual, with some modifications to let the shadow appear. The resulting image is here.
Code:
<?php
// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($im, 0x55, 0x55, 0x55);
$gray2 = imagecolorallocate($im, 0xDD, 0xDD, 0xDD);
// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $gray2);
// Path to our ttf font file
$font_file = './ariblk.ttf';
// the text without shadow
imagefttext($im, 40, 0, 10, 45, $white, $font_file, 'Original');
// the shadow for "Shadow"
imagefttext($im, 40, 0, 10, 89, $gray, $font_file, 'Shadow');
// and the word itself
imagefttext($im, 40, 0, 10, 90, $white, $font_file, 'Shadow');
// Output image to the browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>

Categories