如何使用带有变音符号的imagettftext?

问题描述 投票:0回答:1

以下 php 代码在 Windows 上产生正确的结果,但在 Linux 上则不然。 我不知道如何解决这个问题,有人可以给我提示吗?该字体是来自 google 的标准 Roboto 字体,其中包含所有必需的字符,正如我在 Windows 下看到的那样。

<?php
header('Content-Type: image/jpeg');
$total_width   = 500; // Breite Gesamtbild
$total_height  = 120; // Höhe Gesamtbild
$dst_img = imagecreatetruecolor($total_width, $total_height);  // leeres Bild anlegen
$bg_color = imagecolorallocate($dst_img, 255, 255, 240);    // Hintergrundfarbe festlegen
$rand_color = imagecolorallocate($dst_img, 224, 123, 21);    // Randfarbe festlegen
$text_color = imagecolorallocate($dst_img, 1, 1, 1);        // Textfarbe festlegen
imagefill($dst_img, 0, 0, $bg_color);                       // Hintergrundfarbe malen
imagerectangle ($dst_img, 0, 0, $total_width-1, $total_height-1, $rand_color);  // Rand malen
imagerectangle ($dst_img, 1, 1, $total_width-2, $total_height-2, $rand_color);  // Rand malen (zweites Pixel)
$rp = realpath('');
$txtfont = "$rp/Roboto/Roboto-Regular.ttf";
    
$txt1 = "Viele Gr&uuml;&szlig;e";
$txt2 = html_entity_decode($txt1, ENT_QUOTES, 'UTF-8');
$txt3 = "Viele Grüße";
imagettftext($dst_img, 12, 0, 15, 28, $text_color, $txtfont, $txt1);
imagettftext($dst_img, 12, 0, 15, 58, $text_color, $txtfont, $txt2);
imagettftext($dst_img, 12, 0, 15, 88, $text_color, $txtfont, $txt3);

imagejpeg($dst_img);  // Bild liefern
imagedestroy($dst_img); // Speicher freigeben
?>

Windows 上的结果: Windows:

Linux 上的结果: Linux:

Expexted 是为了在 Linux 上看到与 Windows 上相同的结果

php gd
1个回答
0
投票

经过一些更多的实验,我知道问题不是字体。 在 Windows 上一切都按预期工作。 在我的提供商的 Linux 上,问题是 imagettftext 无法与 html_entity_decode 创建的变音一起使用。但它适用于数字字符引用。

所以我编写了以下辅助函数来替换 html_entity_decode:

函数 html2ttf($s): 字符串
{
    return str_replace(["ü", "ß", "ä", "ä", "Ö", "ö", "Ü", "©", "€"],
                       ['ü', 'ß', 'ä', 'ä', 'Ö', 'ö', 'Ü', '©', '€'], $s);
}
© www.soinside.com 2019 - 2024. All rights reserved.