Hi, I have trouble with toBase64() and an image ob...
# cfml-general
d
Hi, I have trouble with toBase64() and an image object. I create an image with ImageNew() or imageCreateCaptcha(). I want to convert them into a base64 string. In my local ACF2018/Java11 dev env: toBase64(img) returns an empty string. Now I tried that on trycf.com - ACF2018: null pointer exception - Lucee: expected string https://trycf.com/gist/3db1910021fb61459fb255556893acdc/acf2018?theme=monokai Any ideas?
w
I don’t know about acf but Lucee has a very annoying bug in the ImageNew function. I discovered this when trying to test the totp module from forgebox|https://luceeserver.atlassian.net/browse/LDEV-3964
w
try
binaryEncode( imageObj, "base64" )
d
result: Parameter 1 of the BinaryEncode function, which is now coldfusion.image.Image@2af94e0, must be a valid binary object.
if I try `toBinary(img)`: The parameter 1 of function ToBinary, which is now coldfusion.image.Image@7263f3f must be a base-64 encoded string.
w
are these results on Lucee or acf Dirk?
d
Sorry, I forgot: ACF2018/Java11
w
add fileReadBinary() as appropriate
d
my image is created in realtime, not saved.
w
or i guess
binaryEncode( toBinary(imageObj), "base64" )
d
see above:
toBinary()
results in an exception
w
try writing to ram:// file and then reading it as binary
the first example is what i use to save pdfs as base64 strings all the time
d
I have just thought of such a workaround as well: save to ramdisk and then read as binary. But strange that this not work like expected 😞
@Mark Takata (Adobe) Any explanation?
That's my workaround:
Copy code
myCfImage = ImageNew(...);
imageFilePath = 'ram://' & createUUID() & '.png';
myCfImage.write( imageFilePath );
myCfImage = fileReadBinary( imageFilePath );
fileDelete( imageFilePath );
myCfImageBase64 = toBase64( myCfImage );
m
This is... odd, for sure. @priyank_adobe just off the top of your head have you seen anything like this?
d
I think the problem with
ImageNew()
is that it doesn't set a file type when you create an image from scratch. There is a Java method,
getImageBytes()
, that appears to remedy this by allowing you to specify the file type:
Copy code
myImage = ImageNew("",500,500,"argb","white").getImageBytes("png");
However, that turns your image object into a binary, so you'll need to send it back to ImageNew to turn it back into an image object:
Copy code
myImage = ImageNew(ImageNew("",500,500,"argb","white").getImageBytes("png"));
Now you should be able to base64 encode it, or do anything else you could do if you had started with a source file. That said, if base64 encoding is all you need, there is a shortcut:
Copy code
ImageNew("",500,500,"argb","white").getBase64String("png")
p
Was it working before and stopped working recently? Also, have you tried with JDK8 and CF2018
d
@David Buck thnx for your explaination. the missing filetype makes sense.
@priyank_adobe I never used it before. It's a new development. CF2018 + JDK8: In this company I do not have a custom local environment to test something like that