How to render an Image file (Blob) stored in the database using a custom tag, I used to render a logo but the image is being crashed and the output is the byte array.
This is what I tried:
def logo = { attrs, body ->
if(session?.companyId && session?.companyId != 'null'){
Company company = Company.findById(session?.companyId)
if (!company || !company?.logo) {
println "No response..."
out <<"<img src='/cdc/static/images/logo.png' width='150' height='70' />"
}
else{
println "Writing..."
out << "<img src='"
out << company?.logo
out << "' width='150' height='70' />"
}
}
else{
println "No response..."
//out <<"<img src='/cdc/static/images/logo.png' width='150' height='70' />"
}
}
The output is as follows:
How can the company?.logo be rendered as an image and not a byte array ?
It sounds like you want to provide a data URI, basically. For that you'll want something like:
src="data:img/png;base64,xxx"
where "xxx" is the data in base64 format.
So for example, using this public domain base64 library:
out << "<img src='data:img/png;base64,"
out << Base64.encode(company?.logo)
out << "' width='150' height='70' />"
See more on this question at Stackoverflow