Question
· Apr 19, 2023

Is there a way to convert svg to jpg or png

Hi everyone,

Is there a way to convert svg to jpg or png? thanks.

Product version: IRIS 2021.2
Discussion (4)2
Log in or sign up to continue

In the past have used Apache Batik https://xmlgraphics.apache.org/batik/

The scenario was that a message contained a diagnostic background image with layered SVG annotations but the receiving system needed to receive a flattened PNG image instead. Can also generate TIFF or JPEG instead.

Batik is a Java based integration, so depends if IRIS integrating with Java is an option for your deployment.

Interesting if someone suggests a reliable Python alternative.

Other directions might be phantom NodeJS / image magic.

ImageMagick is likely available for your platform and can be called using $ZF(-100). It has a LOT of image conversion options.

A sample command line for svg to png conversion:

$ convert -background none -density 1000 -resize 1000x myvector.svg myraster.png

Example using $ZF(-100):

Class User.Util.Image [ Abstract ]
{

ClassMethod Convert(pSourceFile As %String, pDestFile As %String, pDensity As %Integer = 1000, pResize As %String = "1000x", pBackground As %String = "none") As %Status
{
    Set OsCmd = "/usr/bin/convert"
    Set OsArgs(1) = "-background"
    Set OsArgs(2) = pBackground  // "none" for transparent and black for formats w/o alpha channel
    Set OsArgs(3) = "-density"
    Set OsArgs(4) = pDensity // set the vector width before resizing for best image quality
    Set OsArgs(5) = "-resize"
    Set OsArgs(6) = pResize // image output width/height (default is width 1000 keeping aspect ratio)
    Set OsArgs(7) = pSourceFile
    Set OsArgs(8) = pDestFile // file type controlled by extension; .png, .jpg, .gif etc.
    Set OsArgs = 8
    Set tRC = $ZF(-100,"",OsCmd,.OsArgs)
    // On Linux, a return code of 0 indicates success
    If '(tRC = 0)
    {
        Return $$$ERROR(5001,"OsCmd "_OsCmd_" Returned Error Code ["_tRC_"]")
    }
    Return $$$OK
}

}

Called like this:

Set tSC = ##class(User.Util.Image).Convert("/path/to/filename.svg", "/path/to/filename.png")