dtz55359 2016-11-15 00:30
浏览 93
已采纳

在PHP中渲染复杂的字体/脚本?

Looking to render complex fonts (with diacritics, joined glyphs, right to left text) in various languages/scripts, output is an image (not web page), ideally need to use PHP. The commonly built in graphics libraries for PHP, Imagick and GD, don't support complex fonts, I believe because the version of Freetype they come with doesn't support it.

I've looked into custom building PHP with the possible support but it looks horribly complex and messy.

Any thoughts on an easier solution for this?

Thanks

  • 写回答

2条回答 默认 最新

  • dovhpmnm31216 2017-09-15 12:12
    关注

    This a problem I faced a lot too, imagettftext is the most use GD function to write text on a image but the problem with this is that it can't render complex unicode characters, so as long as your language does not have complex characters we are good.

    To render complex unicode characters you may need imagick installation plus pango installed on your server. Most of the hosting providers do not have pango installed so that means you need to have a dedicated server ready for your application.

    most of the linux distributions comes with pango pre installed so if you managed to install imagick on your local linux matchine following code should work without any problem

    /* complex unicode string */
    $text = "වෙබ් මත ඕනෑම තැනක";
    
    $im = new \Imagick();
    $background = new \ImagickPixel('none');
    $im->setBackgroundColor($background);
    $im->setPointSize(30);
    $im->setGravity(\Imagick::GRAVITY_EAST);
    $im->newPseudoImage(300, 200, "pango:" . $text );
    $im->setImageFormat("png");
    $image = imagecreatefromstring($im->getImageBlob());
    
    //just for print out to the browser
    ob_start();
    imagepng($image);
    $base64 = base64_encode(ob_get_clean());
    $url =  "data:image/png;base64,$base64";
    echo "<img src='$url' />";
    

    Let me know if you find any difficulties with the code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。