PHP - imagettftext not working - php

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.

Related

PHP imagettftext Is Not Showing Text

Website:
https://bimmr6696.000webhostapp.com/signs/Sign.php?Line1=&Line2=asd&Line3=&Line4=
PhpInfo:
https://bimmr6696.000webhostapp.com/signs/test.php
Currently I have the following code:
$img = LoadPNG('sign.png');
//$font = imageloadfont('minecraft.ttf');
// Add the text
imagettftext($img, 20, 0, 5,5, $black, 5, "Test Text");
imagestring($img, 5, 5, 50, 'Test Text', $text_color);
// Create image instances
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $img, 0, 0, 20, 13, 80, 40);
// Set the content type header - in this case image/png
header('Content-type: image/png');
// Output the image
imagejpeg($img);
imagejpeg($dest);
imagedestroy($dest);
imagedestroy($img);
When I use imagestring I'm able to get the text to show, but the text is too small which causes me to need imagettftext, although nothing shows when I use this. I've just about run out of ideas to try and solve this and so any help would be very appreciated.
TLDR: I need to either find out why imagettftext doesn't work or find a way to change font size with imagestring
Deleted everything then retyped it all out and for some reason that seemed to fix it... Because this isn't an actual answer I'm not going to post it below...

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';

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

error while converting text to image using 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.

Categories