Naberd Zendi
Naberd Zendi

Reputation: 51

Imagick convert svg to png with google fonts

I came to a problem when using ImageMagick, I'm missing fonts which is used in SVG. I've done a quick search, but couldn't find an answer that can be used in my case.

My SVG contains something like this:

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

The user can add more fonts and more text as well as background image / images to svg, but the problem is how to add fonts to ImageMagick while converting the svg to png?

I can download those fonts and replace code to use font-face instead of import but method IMagick::setFont() accepts only string.

Any other php conversion method will do to since shell_exec and similar shell functions are disabled. If I can, on our test server I will change that value, but on the production server -- which will run this code -- I can't change anything.

Right now, I use this simple code:

$image = new IMagick();
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->readImageBlob($svg_html);
$image->setImageFormat("png32");

Upvotes: 2

Views: 1516

Answers (1)

Naberd Zendi
Naberd Zendi

Reputation: 51

Found solution to my problem, needed to include font as base64 in defs.

so it looks like

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

Upvotes: 2

Related Questions