2

In my application i have added the code to convert the image into base 64 with a resized images.When i try the following code the image part has been cropped.

function getBase64FromImageUrl(URL)
{
var img = new Image();
img.style.width = this.width,
img.style.height = this.height,
img.src = URL;
img.onload = function ()
  {
    var canvas = document.createElement("canvas");
    canvas.width =150
    canvas.height =150;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(this, 10, 10);
    var dataURL = canvas.toDataURL("image/jpg");
    c=dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
    return c;
  }
}

How to get the full image properly?What change requires in the code

4

1 に答える 1

2
function encodeImage(src, callback) {
    var canvas = document.createElement('canvas'),
        ctx = canvas.getContext('2d'),
        img = new Image();

    img.onload = function() {
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage(img, 0, 0, img.width, img.height);
        callback(canvas.toDataURL());
    }
    img.src = src;
}

このフィドルを参照してください:

http://jsfiddle.net/NwaPT/3/

于 2013-10-19T05:32:02.023 に答える