Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active May 23, 2022 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamoCA/baa454a6a5381e7339548a1f627ce1c3 to your computer and use it in GitHub Desktop.
Save JamoCA/baa454a6a5381e7339548a1f627ce1c3 to your computer and use it in GitHub Desktop.
svgInfo: A ColdFusion UDF to parse and return SVG metadata (width, height, filesize, title, desc, id) #cfml
<--- svgInfo: A cross-compatible ColdFusion UDF to parse and return SVG metadata (width, height, filesize, title, desc, id)
2022-05-23 James Moberg / SunStar Media
Gist: https://gist.github.com/JamoCA/baa454a6a5381e7339548a1f627ce1c3
Blog: https://dev.to/gamesover/parsing-widthheight-from-svg-file-using-coldfusion-ljo
Based on this request: https://dev.lucee.org/t/how-to-get-svg-image-file-width-height-and-size-in-lucee/10231/7
<cfscript>
public struct function svgInfo(required string filepath) hint="I parse and return SVG metadata (width, height, filesize, title, desc, id)" {
local.result = [
"width": javacast("int", 0)
,"height": javacast("int", 0)
,"fileSize": javacast("int", 0)
,"title": ""
,"desc": ""
,"id": ""
];
if (!fileExists(arguments.filepath)){
return local.result;
}
local.javaFile = createObject("java", "java.io.File");
local.fileSize = local.javaFile.init(javacast("string", arguments.filepath));
local.result.fileSize = javacast("int", local.fileSize.length());
local.XmlText = javacast("string", fileRead(arguments.filepath, "UTF-8"));
local.start = findNoCase("<SVG", local.XmlText);
if (local.start gt 0){
local.end = findNoCase(">", local.XmlText, local.start);
local.str = mid(local.XmlText, local.start, (local.end - local.start + 1));
if (!val(local.result.width) && !val(local.result.height) && findNocase("viewBox", local.str)){
local.v = refindNoCase("viewBox\s*=\s*""([\d\.\s,]+)""", local.str, 1, true);
if (arrayLen(local.v.match) gte 2 && listLen(local.v.match[2], " ,") is 4){
local.result.width = val(listGetAt(local.v.match[2], 3, " ,"));
local.result.height = val(listGetAt(local.v.match[2], 4, " ,"));
}
}
if (!val(local.result.width) && refindNoCase("width\s*=\s*""(\d+)", local.str)){
local.w = refindNoCase("width\s*=\s*""(\d+)+", local.str, 1, true);
if (arrayLen(local.w.match) gte 2){
local.result.width = val(local.w.match[2]);
}
}
if (!val(local.result.height) && refindNoCase("height\s*=\s*""(\d+)", local.str)){
local.h = refindNoCase("height\s*=\s*""(\d+)+", local.str, 1, true);
if (arrayLen(local.h.match) gte 2){
local.result.height = val(local.h.match[2]);
}
}
if (refindNoCase("id\s*=\s*""(.+?)""", local.str)){
local.id = refindNoCase("id\s*=\s*""(.+?)""", local.str, 1, true);
if (arrayLen(local.id.match) gte 2){
local.result.id = trim(local.id.match[2]);
}
}
}
local.start = findNoCase("<title>", local.XmlText);
if (local.start gt 0){
local.end = findNoCase("</title>", local.XmlText, local.start);
local.result.desc = trim(mid(local.XmlText, local.start+7, (local.end - local.start - 7)));
}
local.start = findNoCase("<desc>", local.XmlText);
if (local.start gt 0){
local.end = findNoCase("</desc>", local.XmlText, local.start);
local.result.desc = trim(mid(local.XmlText, local.start+6, (local.end - local.start - 6)));
}
local.result.width = (!local.result.width) ? javacast("int", 300) : (fix(local.result.width) is local.result.width) ? javacast("int", local.result.width) : javacast("double", local.result.width);
local.result.height = (!local.result.height) ? javacast("int", 150) : (fix(local.result.height) is local.result.height) ? javacast("int", local.result.height) : javacast("double", local.result.height);
return local.result;
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment