Skip to main content
April 16, 2012
Answered

Base64 string to JPG

  • April 16, 2012
  • 1 reply
  • 4113 views

I am hoping someone can help with this. I am trying to take a Base64 string that is passed from a web form and, using ColdFusion (Version 6) conver that to a JPG image that can be posted to the server and a reference stored in a database.

I have the string being passed and it seems to be converting (at least it's creating a jpg file) but when I go to view the file, it's empty. If I view it in Preview on a Mac it says: "It may be damaged or use a file format that Preview doesn’t recognize."

Here is what I am using:

<head>

<cfif isdefined("form.imageName")>

<cfset newJPG = form.imageName>

<cffile action="write" file="#expandPath("images/newimage.jpg")#" variable="newJPG" output="#newJPG#" addnewline="no">

</cfif>

</head>

<body>

<cfoutput>

The string as an output <br /><br />

#newJPG#<br /><br />

The image as written back to a file<br /><br /><img src="#expandPath("images/newimage.jpg")#" /></cfoutput>

</body>

What am I doing wrong?

    This topic has been closed for replies.
    Correct answer BKBK

    There are some subjects to touch upon: tobase64(), toBinary(), and so on. An example will illustrate it best.

    My current directory contains the picture penguins.jpg. I have also created a new directory, image, within the current directory. My suggestion follows:

    <cfif isDefined("form.image_in_base64")>

       

        <cfset base64JPG = form.image_in_base64>

        <cfset binaryJPG = toBinary(base64JPG)>

        <cffile action="write" file="#expandPath("images/newimage.jpg")#" variable="newJPG" output="#binaryJPG#" addnewline="no">

       

        <cfoutput>

        The string as an output <br /><br />

        #base64JPG#<br /><br />

        The image as written back to a file<br /><br /><img src="#expandPath("images/newimage.jpg")#" />

        </cfoutput>

    <cfelse>

        <cffile action="readbinary" file="#expandPath('penguins.jpg')#" variable="binaryImg">

        <cfset base64Img = tobase64(binaryImg)>

        <cfform>

            <cfinput name="image_in_base64" type="text" value="#base64Img#">

            <cfinput name="sbmt" type="submit" value="Send">

        </cfform>

    </cfif>

    1 reply

    BKBK
    BKBKCorrect answer
    Adobe Expert
    April 16, 2012

    There are some subjects to touch upon: tobase64(), toBinary(), and so on. An example will illustrate it best.

    My current directory contains the picture penguins.jpg. I have also created a new directory, image, within the current directory. My suggestion follows:

    <cfif isDefined("form.image_in_base64")>

       

        <cfset base64JPG = form.image_in_base64>

        <cfset binaryJPG = toBinary(base64JPG)>

        <cffile action="write" file="#expandPath("images/newimage.jpg")#" variable="newJPG" output="#binaryJPG#" addnewline="no">

       

        <cfoutput>

        The string as an output <br /><br />

        #base64JPG#<br /><br />

        The image as written back to a file<br /><br /><img src="#expandPath("images/newimage.jpg")#" />

        </cfoutput>

    <cfelse>

        <cffile action="readbinary" file="#expandPath('penguins.jpg')#" variable="binaryImg">

        <cfset base64Img = tobase64(binaryImg)>

        <cfform>

            <cfinput name="image_in_base64" type="text" value="#base64Img#">

            <cfinput name="sbmt" type="submit" value="Send">

        </cfform>

    </cfif>

    April 17, 2012

    Thank you! That is what I was missing. The image is still not displaying in the  output:

       <cfoutput>

        The string as an output <br /><br />

        #base64JPG#<br /><br />

        The image as written back to a file<br /><br /><img src="#expandPath("images/newimage.jpg")#" />

        </cfoutput>

    But it is writing the file.

    BKBK
    Adobe Expert
    April 18, 2012

    Gregory Nelson wrote:

    But it is writing the file.

    And this: <img src="images/newimage.jpg">?