I'm hoping someone can give me some guidance on the issue that I'm having. I am working on some code for a photo badge card. After the photo is taken I click a print preview button that opens the badge in a PDF of the badge and I print it from there. However, I get the following error:
The Base64 data is not in proper format. Data should be in the format understood by the <img> tag in HTML, which is "data:image/jpg;base64,[base64 data]"
<cfset myImage = ImageReadBase64("#form.xBase64PhotoString#")> <cfimage source="#myImage#" destination="c:/workgroups/webdata/badgecards/#BadgPhotoName#" action="write" overwrite="Yes">
This is my first experience with photos so I would appreciate any help that I can get. Please let me know if you need anymore information.
While someone else may offer the exact solution needed, if I were in your shoes, I would be doing a cfdump of the form.xBase64PhotoString variable. According to that error, what you should see should be not a string (like an image file name or something) but literally the bytes of an image, such that if you on that same page did
<img src="#form.xBase64PhotoString#"> <cfabort>
you should see the image displayed. (If you see nothing in doing that, even though there is seemingly valid data in the variable, beware that you may have other tags preventing display of output at this point in your program.)
And can you confirm that the error is indeed on the imagereadbase64? You don't say, but it seems implied by the error.
If instead it's on the cfimage, I would then dump the myimage variable, to see what's in there.
To debug, use Charlie's suggestions.
It could also help for you to do the following besides:
1) Create a folder, say, imageTest, in your ColdFusion workspace;
2) Copy into the folder a test JPEG which you have renamed to testImage.jpg;
3) Save the code provided below as the file testPage.cfm in the folder imageTest;
The code includes explanatory comments.
4) Launch the page in the browser.
<!--- Create a new ColdFusion image. ---> <cfset myImage=ImageNew("testImage.jpg")> <!--- Convert the image to Base64 format and write it to a text file within the current folder.---> <cfset ImageWriteBase64(myImage,"testImageAsBase64.txt","jpg","yes")> <!---Read the text file as a string variable---> <cffile action="read" file="#expandPath('testImageAsBase64.txt')#" variable="testImageAsBase64" > <!--- Check whether the text file containing the Base64 representation of the image exists and whether the form has not yet been submitted ---> <cfif fileExists(expandPath('testImageAsBase64.txt')) and not isDefined("form.ImageAsBase64String")> <cfoutput> <!--- Form will post text content back to the same file ---> <form action="#cgi.script_name#" method="post"> <!--- Populate the input field with the Base 64 string already ---> <input type="text" name="ImageAsBase64String" value="#trim(testImageAsBase64)#"> <input type="submit" name="submit" value="Submit Image"> </form> </cfoutput> </cfif> <!--- Check if the form has been submitted. ---> <!--- If so, write the image file, newImageWrittenFromBase64.jpg, to the current folder. ---> <cfif isDefined("form.ImageAsBase64String")> <cfset myImage = ImageReadBase64("#form.ImageAsBase64String#")> <cfimage source="#myImage#" destination="#expandPath('newImageWrittenFromBase64.jpg')#" action="write" overwrite="Yes"> Now verify that the file <b>newImageWrittenFromBase64.jpg</b> has been written to the current folder. </cfif> <!--- Optional: delete the text file afterwards ---> <!---<cffile action="delete" file="#expandPath('testImageAsBase64.txt')#" >--->