Display Chinese characters over an image - php

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

Related

Creating image with fix background and randomly displayed text from a pre-defined list

I would like to create a script which is generating an image with one fix background image but display a randomly selected sentence on the image from a list which is previously given in the script.
So far I have a base and I've tried many variations for the random text, but none of them worked until this time.
Here is the script itself:
<?php
header('Content-type: image/png');
$text = 'The text which will be displayed';
$background_path = 'assets/bg_image.jpg';
$image = imagecreatetruecolor(1280, 720);
$background = imagecreatefromjpeg($background_path);
$color = imagecolorallocate($image, 255, 255, 255);
$title_sizes = imagettfbbox(24, 0, 'assets/roboto-medium.ttf', $text);
$title_height = $title_sizes[3];
$title_width = $title_sizes[4];
imagecopy($image, $background, 0, 0, 0, 0, 1280, 720);
imagettftext($image, 24, 0, (640 - $title_width / 2), (360 - $title_height / 2), $color, 'assets/roboto-medium.ttf', $text);
imagepng($image);
imagedestroy($image);
imagedestroy($background);
?>
So the thing is that the line "$text" allows only one sentence to be displayed. I would like to have multiple lines (various sentences) instead which will be displayed on the given background image randomly upon refresh.
Can you please help me to solve this problem?
Thank you very much in advance!
Regards,
Adam

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/

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