PHP 映像创建 使用 UTF-8 字符



当我使用 PHP 创建图像时,它无法正确显示 UTF-8 字符。

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

如何解决这个问题?

在询问之前阅读文档总是好的

字体

对于 latin2 编码的内置字体,可以是 1、2、3、4、5(其中 较大的数字对应于较大的字体(或任何您自己的字体 使用 imageloadfont(( 注册的标识符。

并且 UTF8 不是拉丁语 2 的子集

你可以看看imagettftext。

您的代码将变为:

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

不要忘记,您的$fontFile必须存在于服务器上,并且可以通过代码访问。

最新更新