1

我在绘制“杂项符号和象形文字”unicode 块下的表情符号时遇到了问题。

这是一个示例代码:

<?php
    header('Content-Type: image/png');

    $im = imagecreatetruecolor(290, 60);

    $grey = imagecolorallocate($im, 200, 200, 200);
    $black = imagecolorallocate($im, 0, 0, 0);
    imagefilledrectangle($im, 0, 0, 289, 59, $grey);

    $font = 'seguisym.ttf';
    $font = realpath($font);

    //&#127744; is the unicode html entity for a cyclone. It is under 'Miscellaneous Symbols And Pictographs'.
    $text = "&#127744; is a cyclone!";
    imagettftext($im, 20, 0, 5, 22, $black, $font, $text);

    //&#9924; is the unicode html entity for a snowman. It is under 'Miscellaneous Symbols'.
    $text = "&#9924; is a snowman!";
    imagettftext($im, 20, 0, 5, 52, $black, $font, $text);

    imagepng($im);
    imagedestroy($im);
?>

这是输出:

旋风和雪人

这是它的样子:

旋风与雪人第二部分

如您所见,这些表情符号都位于不同的 Unicode 块下。旋风位于“杂项符号和象形文字”下方,绘制的是 HTML 实体而不是实际字符,但雪人位于“杂项符号”下方且绘制正确。我已经仔细检查以确保我使用的字体包含两个字符。

需要明确的是,我希望绘制实际字符而不是 HTML 实体。

呈现为 UTF8 的相同字符:

旋风和雪人,第三部分

4

1 回答 1

0

我在使用 Abigail TTF 时遇到了同样的问题。经过一些研究,我发现了这个链接

https://bugs.php.net/bug.php?id=17955

其中有这个示例脚本:

<?php
$str = "this is a test";
$str = iconv("UCS-2", "UTF-8", preg_replace("/(.)/","\xf0\$1", $str));

$fsize = 32;
$ffile= "C:/wamp/www/gm/fonts/Aztec/101_Aztec SymbolZ.ttf";

$size = ImageTTFBBox($fsize, 0, $ffile, $str);

$txt_width = ($size[2] - $size[0]);
$txt_height = ($size[1] - $size[7]);

$im_width = $txt_width * 1.5;
$im_height = $txt_height * 1.5;

$im = ImageCreateTrueColor($im_width, $im_height);
$black = ImageColorAllocate($im, 0, 0, 0);
$white = ImageColorAllocate($im, 255, 255, 255);

$txt_x = ($im_width - $txt_width) / 2;
$txt_y = ($im_height - $txt_height) / 2 + $txt_height;

ImageFilledRectangle($im, 0, 0, $im_width, $im_height, $white);
imagecolortransparent( $im, $white );
ImageTTFText($im, $fsize, 0, $txt_x, $txt_y, $black, $ffile, $str);

Imagegif($im, "./test.gif");
ImageDestroy($im);
?>

他们的回答是:您必须正确地将字符串转换为 Unicdoe。这使 Abigail 字体正确显示。不幸的是,我仍在研究如何让非标准 TTF 文件正确显示。但我认为这只是找到他们放置实际字体的位置(比如你的旋风图像)的问题。

另一个令人沮丧的因素是GD有时会放入正方形,有时会将字符留空。我真的希望始终给出空白字符,因为它会以 WIDTH=0 和 HEIGHT=0 的形式返回,而正方形可以以许多不同的大小返回。如果 GD 标准化总是返回一个没有大小的空白字符,那么您所要做的就是寻找它。否则-您必须跳过箍才能确定返回的正方形。

我希望这有帮助!:-)

于 2015-12-07T05:12:29.377 回答