【问题标题】:php imagettftext and specific emojiphp imagettftext 和特定的表情符号
【发布时间】:2014-05-17 04:44:10
【问题描述】:

我在绘制 'Miscellaneous Symbols And Pictographs' 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 的相同字符:

【问题讨论】:

  • 你能说清楚点吗?你希望输出是什么样的?
  • 我认为我说得很清楚,但我会尝试改写它。我想要的是绘制实际字符而不是 html 实体。
  • 显然imagettftext 的奇怪且毫无意义的 HTML 字符引用解析器不支持基本多语言平面之外的字符。如果您直接写入字符("???? is a cyclone!" 并将文件另存为 UTF-8)或 UTF-8 字节转义 "\xF0\x9F\x8C\x80 is a cyclone!",会发生什么情况? (可能仍然无法工作,但应该更接近......)
  • 当我执行其中任何一个建议时,输出如下:i.imgur.com/NjJfOnA.png 我假设绘制的 4 个字符与字符的字节有某种关联。
  • 我认为它不会起作用。 GD 目前无法识别 UCS-4 CMAP。它将回退到使用 UCS-2 CMAP。无法访问 BMP 之外的字符。

标签: php unicode gd imagettftext


【解决方案1】:

我在使用 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 标准化总是返回一个没有大小的空白字符,那么您所要做的就是寻找它。否则 - 你必须跳过箍才能找出返回的正方形。

我希望这会有所帮助! :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    相关资源
    最近更新 更多