Search code examples
amazon-web-servicesamazon-s3coldfusionuploadbinaryfiles

Why image generated from base64 binary data won't display?


I'm trying to grab an image from a remote location, resize it and save it to Amazon S3.

Problem is, I can save the image to S3 allright, but when I try to display it, the browser says image can't be displayed because it contains errors. I'm sure this is due to me doing the following:

a) Grab image from remote location:

  <cfhttp timeout="45" 
      throwonerror="no" 
      url="#variables.testFilePath#" 
      method="get" 
      useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12" 
      getasbinary="yes" 
      result="objGet">

b) Create image to validate and resize

<cfset objImage = ImageNew(objGet.FileContent);

c) After resizing, I'm converting the resized image (!) back to binary data, because the following function call to S3.cfc needs a valid file path to read the image again as binaryData. I'm doing this:

 <cfset variables.filekey = toBase64( objImage )>

instead of this:

 <cffile action="readBinary" file="#arguments.uploadDir##arguments.fileKey#" variable="binaryFileData">

because I can't get a valid arguments.uploadDir to work, so instead of re-reading the image here, I thought I just convert back to binary data and save this to S3. Which converts my image back to a base64 string, which I then save at S3.

Question:
Can someone tell me what I'm doing wrong? I guess it's the base64 getasbinary and toBase64. But I'm not sure.

Thanks for help!


Solution

  • You don't need to convert it back to binary. Just use the cfimage tag to write it to disk, and then use the path to send the image off to Amazon. Then delete the image when you're done with it.