Page 1 of 1

Converting SVG to Image ignores text

Posted: 2020-03-25T14:24:07-07:00
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

Re: Converting SVG to Image ignores text

Posted: 2020-03-25T15:40:32-07:00
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

Re: Converting SVG to Image ignores text

Posted: 2020-03-25T16:01:53-07:00
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.

Re: Converting SVG to Image ignores text

Posted: 2020-03-25T16:12:01-07:00
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

Re: Converting SVG to Image ignores text

Posted: 2020-03-25T17:16:03-07:00
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.

Re: Converting SVG to Image ignores text

Posted: 2020-03-26T02:17:10-07:00
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

Re: Converting SVG to Image ignores text

Posted: 2020-03-26T05:43:40-07:00
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.

Re: Converting SVG to Image ignores text

Posted: 2020-03-27T13:43:32-07:00
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

Re: Converting SVG to Image ignores text

Posted: 2020-03-27T14:10:04-07:00
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.

Re: Converting SVG to Image ignores text

Posted: 2020-03-28T02:30:07-07:00
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

Re: Converting SVG to Image ignores text

Posted: 2020-03-30T05:00:40-07:00
by Spiritmonger
I found the solution: +antialiasing was missing.

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