How to get SVG image file WIDTH, HEIGHT and SIZE in Lucee

Hi,

Can any one please suggest, how to get SVG image file WIDTH, HEIGHT and SIZE in Lucee?

OS: Windows Server 2019 (10.0) 64bit
Java Version: 11.0.11 (AdoptOpenJDK) 64bit
Tomcat Version: Apache Tomcat/9.0.46
Lucee Version: Lucee 5.3.9.129-RC

SVG is just a text-based XML file. I believe that you’d need to parse the XML and test whether “svg.width” & “svg.height” exist. As for “size”, are you referring to file size? If so, you can use built-in CFML file functions to get it (or use java “java.io.File”).

NOTE: I have some SVG files (generated by ZINT barcode generator). One file is only 6.4k SVG, but has dimensions that are 2620x595. The dimensions aren’t that important as the file looks great any any resolution.

1 Like

What @Jamo said … svg isn’t really an image.

Here’s a quick example: (does not include the string or xml parsing for width/height).

<cfscript>
svg = fileRead("https://upload.wikimedia.org/wikipedia/commons/3/30/Vector-based_example.svg");
writeDump(var=svg, label="#len(svg)# Bytes");
</cfscript>
1 Like

This is an interesting SO answer for PHP, but it applies also to CFML here:

1 Like

If I want to get size of SVG image file in Bytes, so we can use len(svg)? or can I use any other method

Hi @Jamo Yes, I need both width, height, and size in bytes

Since svg files are just a text string (open one in you favorite text editor for context), my suggestion was a lazy / quick way to get the size.
Obviously, you can use common file tags or functions to get the size like you would with any other file types.

The Lucee image extension does support (as a hidden feature; will be official in Lucee 6) JDeli, that means you can simply download the jdeli trial jar, copy it to the classpath, restart Lucee that’s it.
If it support svg i’m not certain, give it a try.

1 Like

you need to use fileReadBinary, fileRead will give you a lower number when it for example use “UTF-8” as a charset, also creating a string is slower than simply load a byte array. Even better would be to use fileInfo and not load the image into memory at all.

Good information to know. Appreciate the clarification.

I wrote a UDF to parse width/height and filesize (and id, title desc) from an SVG file.

This should work with ColdFusion 2016-2021 and Lucee. I tested it against some Zint and Adobe-generated SVG files. If you encounter any issues, feel free to privately hit me up and provide a SVG file for testing.

4 Likes

@Jamo thanks for the blog post!!! Very appreciated!

Thanks for all your posts / suggestions

I will do a work on it as suggestions and post the results with code

Thanks

1 Like