2011-11-16 120 views

回答

1

我认为这将解决您的问题,使您的网址作为SRC的img标记。

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,/, ""); 
} 

此功能是在Get image data in JavaScript?

发现使用它像

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

或使用您自己的img标签从你的HTML

+0

感谢您unsver,但它看起来像我不能使用函数toDataUrl如果我从远程服务器加载img到画布。 和我想我有几个变种解决这个问题: - 也许使用我自己的代理图像 - 也许使用雅虎管道莫名其妙 –