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条)

报告相同问题?