How to render a byte array (Blob) as image using TagLib

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:

enter image description here

enter image description here

How can the company?.logo be rendered as an image and not a byte array ?

Jon Skeet
people
quotationmark

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' />"

people

See more on this question at Stackoverflow