简体   繁体   中英

PHP Image Create with UTF-8 Character

When I create an image with PHP, it does not display UTF-8 characters properly.

$imgPath = 'uplatnica1.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 000, 000, 000);

$string = "šđčćž";
$x = 70;
$y = 200;

$fontSize = 3;

imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image, 'qwe.jpg');

How can I solve this problem?

It's always good to read documentation prior asking

font

Can be 1, 2, 3 , 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts) or any of your own font identifiers registered with imageloadfont().

and UTF8 is not subset of latin2

You could check out imagettftext .

Your code will become:

$imgPath = 'uplatnica1.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 000, 000, 000);

$string = "šđčćž";
$x = 70;
$y = 200;

$fontSize = 3;
$fontFile = 'verdana.ttf'; // This file must reside on your system

imagettftext($image, $fontSize, 0, $x, $y, $color, $fontFile, $string);
imagejpeg($image, 'qwe.jpg');

Don't forget that your $fontFile must exist on the server and be accessible by the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM