0

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.

Kofi Black
  • 111
  • 1
  • 7
  • Possible duplicate of [how to use imagick annotateImage for chinese text?](http://stackoverflow.com/questions/11101544/how-to-use-imagick-annotateimage-for-chinese-text) – node_modules May 19 '17 at 07:23
  • I just wonder whether there exists an easy fix for my problem. I have read the post mentioned above, and have tried to use other ttf such as wt024.ttf, but still cannot fix the problem. – Kofi Black May 19 '17 at 07:39

1 Answers1

0

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

Kofi Black
  • 111
  • 1
  • 7