Converting SVG to Image ignores text

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Locked
Spiritmonger
Posts: 6
Joined: 2020-03-25T14:16:12-07:00
Authentication code: 1152

Converting SVG to Image ignores text

Post by Spiritmonger »

Hello folks,

I want to convert a SVG to PNG (or any other Bitmap-Image), but every text-element is ignored.

SVG-File:
https://plugin.myvoiceprint.de/voice-pr ... 3c807b.svg

PNG-File
https://plugin.myvoiceprint.de/voice-pr ... 3c807b.png

CONFIG:
https://plugin.myvoiceprint.de/wp-conte ... config.php

CODE:

Code: Select all

 
$file_name = UPLOAD_DIR . time();   
    
//Store SVG-Content
file_put_contents($file_name.'.svg', $svg_content); 
        
 //Generate PNG-Image
 $image = new IMagick();  
 $image->setBackgroundColor(new ImagickPixel('transparent'));  
 $image->readImageBlob($svg_content);  
 $image->setImageFormat("png32");
  file_put_contents($file_name.".png", $image);
Any ideas?

Thank you very much and best regards :)
Kilian

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting SVG to Image ignores text

Post by fmw42 »

What is your OS and what is your version of ImageMagick and what renderer are you using and its version. For me using ImageMagick 6.9.10.2 Q16 on Mac OSX with Inkscape fails with error. But with the internal MSVG rendered it works fine as

Code: Select all

convert MSVG:test2.svg result.png

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting SVG to Image ignores text

Post by snibgo »

This converts fine for me (but falls back to default fonts as I don't have the fonts specified), using IM v7.0.8-64 and Inkscape 0.91, or just IM with "msvg:" prefix.
snibgo's IM pages: im.snibgo.com

Spiritmonger
Posts: 6
Joined: 2020-03-25T14:16:12-07:00
Authentication code: 1152

Re: Converting SVG to Image ignores text

Post by Spiritmonger »

Hello fmw41 and snibgo,

thank you for your fast responses!

All details about the configuration are here: https://plugin.myvoiceprint.de/wp-conte ... config.php (Its a managed server)

And if I convert the SVG on another Webtool, its fine (so the SVG is correct). But not with imagemagick on my server.

Best Regards
Kilian

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting SVG to Image ignores text

Post by fmw42 »

It runs fine for me with IM 7.0.8.66 Q16 Mac OSX. Not sure if the fonts match perfectly, but I do see the text.

Code: Select all

magick -verbose MSVG:test2.svg result.png

Run my command in PHP exec() with -verbose in your command and let us know what error message you get.

Spiritmonger
Posts: 6
Joined: 2020-03-25T14:16:12-07:00
Authentication code: 1152

Re: Converting SVG to Image ignores text

Post by Spiritmonger »

I have following error:

Code: Select all

Array
(
    [0] => mvg:/var/www/clients/client1/web11/tmp/magick-28070ejzeQ3opi3sL=>/var/www/clients/client1/web11/tmp/magick-28070ejzeQ3opi3sL MVG 969x671 969x671+0+0 16-bit sRGB 237809B 0.230u 0:00.235
    [1] => MSVG:1585170611-5e7bc8b3c807b.svg=>1585170611-5e7bc8b3c807b.svg MVG 969x671 969x671+0+0 16-bit sRGB 237809B 0.000u 0:00.000
    [2] => magick: unable to read font `helvetica' @ error/annotate.c/RenderFreetype/1383.
    [3] => magick: non-conforming drawing primitive definition `text' @ error/draw.c/RenderMVGContent/4406.
)
1
It seams, that there is a problem with the installed font, but I have no helvetica in my code.

Best regards
Kilian

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting SVG to Image ignores text

Post by snibgo »

I suppose helvetica is a fall-back font if requested fonts are not available. I suppose you have no fonts available.

I don't know IMagick and PHP so can't advise. I notice that your config.php has: "--with-fontpath=" Perhaps that fontpath should be defined.
snibgo's IM pages: im.snibgo.com

Spiritmonger
Posts: 6
Joined: 2020-03-25T14:16:12-07:00
Authentication code: 1152

Re: Converting SVG to Image ignores text

Post by Spiritmonger »

Hello Snibgo,

thank you for your support. The Solution was to set a font in the exec:

Code: Select all

exec('magick -verbose -font Lato-Regular MSVG:'.$file_name.'.svg '.$file_name.'.png 2>&1', $output, $error);
But now I have two problems:
  • I can only set one font, how is it possible to use several one (f.e. in a order), the Font-family in the svg is ignored.
Thank you and best regards
Kilian

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting SVG to Image ignores text

Post by snibgo »

Spiritmonger wrote:I can only set one font, how is it possible to use several one (f.e. in a order), the Font-family in the svg is ignored.
The proper solution is to change some configuration so your PHP process can access your installed fonts. Using "-font" in IM will allow use of only one font.
Spiritmonger wrote:The quality is really bad
Where did those colours come from? Inkscape doesn't show colours in the SVG you posted.
snibgo's IM pages: im.snibgo.com

Spiritmonger
Posts: 6
Joined: 2020-03-25T14:16:12-07:00
Authentication code: 1152

Re: Converting SVG to Image ignores text

Post by Spiritmonger »

Hello Snibgo,

the colors come from the mask with lineargradient in <defs>

The interesting thing is, thats only with lighter backgrounds:
Image
Image

Image

With green, it mixes white and green to yellow:
Image

Is there any problem with transparancy?

Best regards
Kilian

Spiritmonger
Posts: 6
Joined: 2020-03-25T14:16:12-07:00
Authentication code: 1152

Re: Converting SVG to Image ignores text

Post by Spiritmonger »

I found the solution: +antialiasing was missing.

Thank you for your great support, lot of kudos for you!

Locked