我在使用 ImageMagick 时遇到问题,缺少 SVG 中使用的字体。我进行了快速搜索,但找不到可用于我的案例的答案。
我的 SVG 包含这样的内容:
<defs>
<style type="text/css">@import url("https://fonts.googleapis.com/css?family=Faustina");</style>
<style type="text/css">@import url("https://fonts.googleapis.com/css?family=Ubuntu");</style>
</defs>
<text x="61.81135214943191" y="111.73005750523703" font-size="55" fill="#9E3E3E" transform="rotate(15)" font-family="Faustina" style="font-family:'Faustina' !important">Test 1</text>
<text x="61.81135214943191" y="171.73005750523703" font-size="45" fill="#9E3E3E" transform="rotate(-15)" font-family="Ubuntu" style="font-family:'Ubuntu' !important">Test 2</text>
用户可以向 svg 添加更多字体和更多文本以及背景图像/图像,但问题是如何在将 svg 转换为 png 的同时向 ImageMagick 添加字体?
我可以下载这些字体并替换代码以使用 font-face 而不是导入,但方法 IMagick::setFont()
只接受字符串。
任何其他 php 转换方法都可以,因为 shell_exec
和类似的 shell 函数被禁用。如果可以的话,我将在我们的测试服务器上更改该值,但在生产服务器上(将运行此代码)我无法更改任何内容。
现在,我使用这个简单的代码:
$image = new IMagick();
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->readImageBlob($svg_html);
$image->setImageFormat("png32");
最佳答案
找到了我的问题的解决方案,需要在 defs 中包含作为 base64 的字体。
所以看起来像
<style>
@font-face {
font-family: 'ABeeZee';
font-style: normal;
font-weight: 400;
src: local('ABeeZee Regular'), local('ABeeZee-Regular'), url(data:font/woff2;base64,...)
}
</style>
关于php - Imagick 使用谷歌字体将 svg 转换为 png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53915042/