Yesterday I blogged about a base64 image creator I built. Today I was working to do more image conversion to base64 for my project. I quickly ran into an issue on my ColdFusion 9 server.
Source image
I was using this code to get the base64 of the image:
2 myImg = imageRead('grayButton.png');
3 writeDump(toBase64(myImg));
4</cfscript>
But it threw this error:
2
3Null Pointers are another name for undefined values.
I was using this method because "imageWriteBase64()" only writes out to a file. I have no use / need / or want for a file. I want the result. So, using ColdFusion 9 I was able to avoid writing a file to the disk but still get the result. The solution utilizes the in-memory file system.
2 myImg = imageRead('grayButton.png');
3 ImageWriteBase64(myImg,"ram:///Base64.txt","png","yes", true);
4 a = fileRead("ram:///Base64.txt");
5 fileDelete("ram:///Base64.txt");
6 writeDump(a);
7</cfscript>
This solution seems totally wrong to me. First, why does toBase64() throw a null error. The source is obviously good as imageWriteBase64() can process it. Also, why is the only possible result from imageWriteBase64() is to write a file. Granted the function name contains imageWrite but it still seems wrong.
Till next time...
--Dave
#1 by Azadi Saryev on 4/28/10 - 11:15 AM
you can convert it to a proper binary object using imageGetBlob() function:
myImg = imageRead('grayButton.png');
writeDump(toBase64(imageGetBlob(myImg)));
alternatively, you can just use fileREadBinary() function:
myImg = fileReadBinary('grayButton.png');
writeDump(toBase64(myImg));
Azadi
#2 by Andreas Schuldhaus on 4/28/10 - 11:24 AM
#3 by Andreas Schuldhaus on 4/28/10 - 11:30 AM
#4 by Dave Ferguson on 4/28/10 - 4:22 PM
writeDump(toBase64(fileReadBinary(ExpandPath('.') & '/grayButton.png')));
@Abndreas Yea.. I found this when I tried to process this image. This forced me to find an alternative to what I was doing.