Decoding Base64 to an Image in ColdFusion

4.5k views Asked by At

A while back I posted a question regarding decoding a Base64 string into a image to store in my ColdFusion application. Here's the link.

I was getting the string from a Topaz signature pad that had an NAPI browser plugin that created the Base64 string. I would then take that string and use the following code to decode it:

 <cfscript>
        binaryValue = binaryDecode( form.SigImgData, "hex" );
        FileWrite("c:\Inetpub\wwwroot\signatures\#fullfilename#.bmp", "#binaryValue#");
</cfscript>

I would then store it to disk and display it in the browser using the tag.

Well now that Google Chrome has discontinued support for NAPI plugins, I am now creating the Base64 string using JavaScript supplied from the signature pad manufacturer. I can successfully create the Base64 string using the new JS but when I plug the string into my same CFScript code from above, it creates a non-usable image. Basically a corrupted image file.

Here is an example of my Base64 string:

iVBORw0KGgoAAAANSUhEUgAAAfQAAABkCAYAAABwx8J9AAAIaklEQVR4Xu3csZMMTRgH4JYJyYRkRKSykxEhQkQoQ0Z0JyJDRIZISkSGiD9BRigkk/mu96s+Y+y62b1de/O+z1YpVWe3e97nHfWb7pnbAz9//twopdQ/XgQIECBAgMBIBQ5sB/rW9rFvjvT4HTYBAgQIECCwLSDQnQYECBAgQCCAgEAP0EQlECBAgAABge4cIECAAAECAQQEeoAmKoEAAQIECAh05wABAgQIEAggINADNFEJBAgQIEBAoDsHCBAgQIBAAAGBHqCJSiBAgAABAgLdOUCAAAECBAIICPQATVQCAQIECBAQ6M6BdAKfPn0qX79+LYcOHSqnTp1aev1t/DrwquZY+kEbkACB0QsI9NG3UAHzCHz58qUcO3Zs5yPXrl0rV69eXVrw1jA/ceLEb4dU53j69Ok8h7nwe7sXEy4oFmb0QQKjFBDoo2ybg96LwMOHD8u3b9/K+/fvy7t3734L9xs3buxp1X7nzp1y//79cuHChXLy5MmdOTY2Nkodu67YVxW0W1tb5e7du3/Q1Lk3NzdL/duLAIG4AgI9bm9VtovAjx8/ypMnT/4I90VX1PUi4fDhw+X48ePlw4cPk/Cuc9SAf/To0WSe7mvReaaV1cK83kI4f/78zlu+f/9enj17NplbsPsvQSC2gECP3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IBAD9lWRREgQIBANgGBnq3j6iVAgACBkAL/AcbIWJJimEY5AAAAAElFTkSuQmCC

I found a site that allows you to past a string into a box and decode it to the original image and it seems to decode it fine.

As per the suggestions, I have tried the following modification:

<cfset image = imageReadBase64("data:image/bmp;base64,form.SigImgData")>
    <cfimage 
    source="#image#"
    destination="c:\Inetpub\wwwroot\signatures\#fullfilename#.bmp" 
    action="write">

But I receive the following error:

Can not decode string "form.SigImgData". The input string is not base64-encoded.

2

There are 2 answers

2
Abhishekh Gupta On BEST ANSWER

As your binary data doesn't have image headers(Contains Mime type e.g. data:image/png;base64 for png image) so you can simply use imageReadBase64 like this:

<cfset image = imageReadBase64(form.SigImgData)>
<cfimage 
   source="#image#"
   destination="c:\Inetpub\wwwroot\signatures\#fullfilename#.bmp" 
   action="write">

I tried it locally with same code. Is this your image? enter image description here

6
Cedrun On

With a Base64 string you can use this function to get an image:

ImageReadBase64(yourstring)

Example:

<cfset image = ImageReadBase64(form.SigImgData)>
<cfimage source="#image#" destination="C:\Inetpub\wwwroot\signatures\#fullfilename#.bmp" action="write">