0

I'm trying to create a dynamic QR code to be sent in a triggered mail, but since QR codes are not supported (only bar codes are) am I using Google API to create one. I then try to base64 encode the PNG from Google to put in an img tag (as normally done at websites sometimes). Everything seem to work great except that the base64 encoding returns an invalid string.

<script language=javascript runat=server>
  Platform.Load("Core", "1")

  var imgFetch = HTTP.Get('http://www.google.com/chart?chs=200x200&chld=M&cht=qr&chl=otpauth:%2F%2Ftotp%2FTest%3Fsecret%3DGVFXGN3WIYZEMYKW');
  var imgData = imgFetch.Content;
  var imgDataEncoded = Base64Encode(imgData);

  Write('<img src="data:image/png;base64,' + imgDataEncoded + '" />');
</script>

I took the raw image data from the Google call and ran it through another base64 encoder and it returns a different (valid) string then the Base64Encode function above. I also tried the same using AMPScript, with the same result.

Am i missing something here?

1
  • Base64Encode is the function used to encode strings in AMPScript. To encode strings with SSJS you should use Platform.Function.Base64Encode to do this job. Dec 17, 2019 at 18:59

2 Answers 2

1

Your sample URL returns an image, so why not just hotlink it?

<script language="javascript" runat="server">

  Platform.Load("Core", "1.1.1")

  var imgURL = "http://www.google.com/chart?chs=200x200&chld=M&cht=qr&chl=otpauth:%2F%2Ftotp%2FTest%3Fsecret%3DGVFXGN3WIYZEMYKW";

  Write("<img src='" + imgURL + "'/>");

</script>

In AMPScript (faster)

%%[

var @imgURL
set @imgURL = "http://www.google.com/chart?chs=200x200&chld=M&cht=qr&chl=otpauth:%2F%2Ftotp%2FTest%3Fsecret%3DGVFXGN3WIYZEMYKW"

]%%
<img src="%%=v(@imgURL)=%%"/>
5
  • The fallback solution is to hotlink it but I'm thinking that the user might not have a internet connection when wanting to show the image and it would be pretty easy to change the data since it's in the url.
    – AxAn
    Jun 30, 2015 at 10:24
  • No internet connection seems like an edge case. It's going to get cached on the device once it's been viewed. Maybe I'm not understanding your scenario. Jun 30, 2015 at 14:49
  • 1
    It's not much of a complicated scenario: I want to put a QR code in a mail. The data in the QR code might contain things that the user should not be able to (easily) see/change. If I hotlink the QR code then it's easy to see/change the data, which is bad. The main question here is why the Base64Encode function does not work in my example.
    – AxAn
    Jul 1, 2015 at 7:28
  • I've tried several things to make this work (setting header values, alternate encoding) -- with no success. Seems like it should, but I think there may be something with the HTTP.Get() that's retrieving the image with an unintended encoding. Jul 1, 2015 at 14:09
  • I was printing out the raw data of the image and it -looked- exactly like the data in the working image file I got when saving it to my computer. That is why I thought it might be a problem in the encoding function, and not with HTTP.Get() function.
    – AxAn
    Jul 1, 2015 at 15:08
0

Unfortunately this is not possible with SSJS as it cannot properly interpret binary file data and therefore the input string to the Base64Encode() function will never be valid.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .