Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
Locked
0

Base64 to Image

Guest
Apr 03, 2008 Apr 03, 2008
Hello,

I was hoping someone could tell me how to decode a base64 string into an actual image. I have not used this before so actually have no idea how it performs... I've tried ImageReadBase64() but it says variable ImageReadBase64 is not defined. Any help in this would be great.

JC
TOPICS
Getting started
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 03, 2008 Apr 03, 2008
Reign Supreme wrote:
> it says variable ImageReadBase64
> is not defined. Any help in this would be great.
>
> JC
>

Install ColdFusion 8. That is a new function in CF8. The error message
"is not defined" on a function indicates that the function does not
exist in your version of ColdFusion. So my first advice is to upgrade
or look for another solution.

Sorry can't help beyond this.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 03, 2008 Apr 03, 2008
Is there a work around in CF 7??? I can't afford to upgrade to 8 right now.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 03, 2008 Apr 03, 2008
Reign Supreme wrote:
> Is there a work around in CF 7??? I can't afford to upgrade to 8 right now.


I theorize that if you wrote out the image binary data to a file then
used that file in an <img...> tag, you could somewhat simulate this.
But I have no experience with actually doing this or even understand all
the details well enough to be confidant that this works at all or is
scalable and robust if it does work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 03, 2008 Apr 03, 2008
I'm new at this posting in a forum thing.. Should I repost asking for Base64 to image file in CF7???
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 03, 2008 Apr 03, 2008
> Base64 to image file in CF7???

I would guess one of the open source CFC's like ImageCFC already does this. However, you can convert the bas64 string to an image file using a little java. I believe the image formats you can read/write are determined by the JVM. More specifically, by the readers/writers registered with the jvm. I think MX7 installs jvm 1.4.2 by default, which is capable of reading gif images, but not writing them to disk. The CF8's JVM (1.6), on the other hand, is capable of writing gif's to disk.

This code will display the mime types supported for read and write operations. Plus the informal names you can use when writing out an image. An informal names is similar to a file extension like "jpeg" or "png".

<cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
<cfset readerNames = ImageIO.getReaderFormatNames()>
<cfset readerMimeTypes = ImageIO.getReaderMimeTypes()>
<cfset writerNames = ImageIO.getWriterFormatNames()>
<cfset writerMimeTypes = ImageIO.getWriterMIMETypes()>
<cfdump var="#writerMimeTypes#" label="writerMimeTypes">
<cfdump var="#readerMimeTypes#" label="readerMimeTypes">
<cfdump var="#readerNames#" label="readerNames">
<cfdump var="#writerNames#" label="writerNames">

Here is an example of how you could decode and save a jpeg image to disk.

<cfscript>
// convert the base 64 string to binary
binaryData = binaryDecode(base64Image, "base64");
inStream = createObject("java", "java.io.ByteArrayInputStream").init( binaryData );
ImageIO = createObject("java", "javax.imageio.ImageIO");
// convert the bytes to a BufferedImage
buffImage = ImageIO.read( inStream );
// indicate where to save the image file
outputPath = ExpandPath("savedFile.jpg");
saveToFile = createObject("java", "java.io.File").init( outputPath );
// write the file to disk
wasWritten = ImageIO.write( buffImage, "jpeg", saveToFile);

WriteOutput("was written to disk="& wasWritten);
</cfscript>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 03, 2008 Apr 03, 2008
On second thought, did you actually try writing the file to disk as Ian Skinner suggested? Assuming you actually want to save the image to a file, and it it is properly encoded already, that should work. To stream it to the browser, you could use cfcontent with the variable attribute.

<cfset binaryData = binaryDecode(yourBase64Image, "base64")>
<cfcontent type="image/jpeg" variable="#binaryData#">
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 04, 2008 Apr 04, 2008
LATEST
You hit the nail on the head... I got creative with naming but all in all works like a charm!! Thank you!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources