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 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入