Search code examples
phpimagesvgimagick

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


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 :

glorious-cat.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 ?


Solution

  • 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.