如何在PHP中将SVG转换为PNG而不丢失编写的文本。

huangapple go评论56阅读模式
英文:

How to convert an SVG into PNG in PHP without losing written text

问题

我正在尝试将SVG转换为PHP中的PNG文件。在我的SVG中,有一些文本在转换过程中丢失了。我用Imagick来实现这个目的,宁愿坚持这个解决方案,但只要是免费的,我也可以考虑替代库。

我的代码非常简单:

$i = new Imagick($svg);
$i->setImageFormat('png');
$image = $i->getImageBlob();

结果:

如何在PHP中将SVG转换为PNG而不丢失编写的文本。

我猜这可能是在Imagick过程中缺少的文本字体之类的东西,特别是因为在Imagick对象中有一个setFont()方法。不幸的是,我找不到我的字体文件在我的alpine容器中的位置。

为了正确显示我的文本,我该怎么做?

英文:

I am trying to convert an SVG to a PNG file in PHP. In my SVG, there is some text written that is lost during my convertion process. I use Imagick for that purpose and would rather stay on that solution, but as long as it is free, i am open to alternate libraries.
My code is very simple :

    $i = new Imagick($svg);
    $i->setImageFormat('png');
    $image = $i->getImageBlob();

And the result :

如何在PHP中将SVG转换为PNG而不丢失编写的文本。

I assume it's something like the text fonts that are missing from Imagick during the process, especially since there is a setFont() method in the Imagick object. Unfortunately, I can't find where my font files are located in my alpine container.

What could I do in order to get my text to be properly displayed ?

答案1

得分: 1

感谢RickN的评论,我成功修复了它。

实际上,我只是使用了他们链接中的这个命令:

RUN apk --no-cache add msttcorefonts-installer fontconfig && \
    update-ms-fonts && \
    fc-cache -f

这确实是我需要做的一切。

英文:

So, thanks to RickN's comment, I managed to fix it.

Quite litterally I just used the very commmand in their link :

RUN apk --no-cache add msttcorefonts-installer fontconfig && \
update-ms-fonts && \
fc-cache -f

It's really all I needed to do.

huangapple
  • 本文由 发表于 2023年5月10日 21:49:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定