1

I'm attempting to create a build process that will base64 encode the images in my CSS file. I use rhino for my build process so natural thought was to just do this after I get done building in rhino.

So far I've got the image reading done but getting stuck at the Base64 part ... Everything I try in JavaScript gets mad about the byte[] and everything in Java I try has problems with the Rhino.

Since its rhino I'm limited on the libraries I can use and can't use the apache commons.

Anyone got any ideas?

prodcss = prodcss.replace(/url\(['"]*([^"')]*)['"]*\)/g, function(match) {
    var path = "build/" + match.substring(4, match.length - 1);

    // Read the image to a byte[]
    var file = new java.io.File(path);
    var bufferedImage = javax.imageio.ImageIO.read(file);
    var raster = bufferedImage.getRaster();
    var data = raster.getDataBuffer().getData();

            var base64 = ''; // Whats the best way to accomplish this ...

    return "url(data:image/png;base64," + base64 + ")";
});

Appreciate the help.

Eliran Malka
  • 14,498
  • 5
  • 72
  • 96
amcdnl
  • 7,766
  • 12
  • 59
  • 88
  • if you want to send the png data (assuming the file is png to begin with), don't convert the data to an image, just read the File bytes and return them. you are sending "raw" data, not png encoded data. – jtahlborn Sep 13 '12 at 17:30
  • and what do you mean "gets mad about the byte[]"? and why can't you use apache commons? – jtahlborn Sep 13 '12 at 17:30
  • note, a "common" base64 encoding utility (without using apache commons) is `sun.misc.BASE64Encoder`. – jtahlborn Sep 13 '12 at 17:33
  • I only have access to the 'java' namespace ... https://developer.mozilla.org/en-US/docs/Scripting_Java E.g. when I try to do the sun encoder I get js: uncaught JavaScript runtime exception: ReferenceError: "sun" is not defined. – amcdnl Sep 13 '12 at 17:36
  • I am not familiar with rhino, but [this](http://stackoverflow.com/a/246813/3340) answer to a similar question might help. – Jacob Schoen Sep 13 '12 at 17:50
  • I've seen that but my problem is passing the bytes to that javascript function produces errors. – amcdnl Sep 13 '12 at 18:02
  • @AustinMcDaniel - _what_ errors...? – jtahlborn Sep 13 '12 at 19:25

0 Answers0