0

does someone know, is it possible read image to binary from remote URL. I know that File API give us possibility read as binary file from our local system. But i cant find info about reading from remote URL. Maybe even i can read this info from browser cache, after i load this info from to or else?

Volodymyr Dvornyk
  • 1,332
  • 1
  • 12
  • 15

1 Answers1

1

I think this will solve your problem, make a img tag with your url as SRC.

function getBase64Image(img) {
    // Create an empty canvas element
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;

    // Copy the image contents to the canvas
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);

    // Get the data-URL formatted image
    // Firefox supports PNG and JPEG. You could check img.src to
    // guess the original format, but be aware the using "image/jpg"
    // will re-encode the image.
    var dataURL = canvas.toDataURL("image/png");

    return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

This function was found on Get image data in JavaScript?

Use it like

var img = new Image();
img.load = function(){
    var data = getBase64Image(this);
};
img.src= "my IMAGE URL";

Or use your own img tag from your HTML

Community
  • 1
  • 1
Niels
  • 48,601
  • 4
  • 62
  • 81
  • Thank you for unsver, but its looks like i cant use function toDataUrl if i load img to canvas from remote server. and i think i have several variants to solve this: - maybe use my own proxies for images - maybe use Yahoo pipes somehow – Volodymyr Dvornyk Nov 17 '11 at 00:23