3

I have HTML canvas and it work fine to display image.

And I have this jquery code to download the image :

$(".img-download").click(function(){
    var data = canvas.toDataURL();
    download.href = data; -- I tried this to download image, but PNG file generated cannot be opened in photoshop or any other image manipulation apps
    alert (data); -- then, I tried to see what's actually the data content
});

And here's my download button :

<a href="" class="img-download btn btn-lg btn-default" download="image.png">Download</a>

Here's what the data content :

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjAAAAH0CAYAAAAjYBdQAAAgAElEQVR4XrS9d7Cm6Vnm93Sf0DlMTp0naZSFTCokYNf2er2mbBZvFcG4WGxClQtThVibUpk/EFCLjQoEJmgUkECkrRImLMqiKEmIIIE0EsrSaKIm9/R0DqfP6fb1u677ft9vpJGE7Nqv58w53/e97xPu5w7XHZ7n3fSrv/rblzdt2jzys2lsbKyPy5suD16X9WvT2DyWNm8em/WT1yW+GZs36a/Ll/QXf2/yvZcv5T7uujT9vVnt6WL9LC/pzkvr4+LauXHu7Olx/vxpt7Okpi9d2lB/G2lf/V9W5xuXLo3N+vKy2qNNfjbWL42lpSX/MED6vKzrGIcG6b8Z3yY1TBvctbx5eawzL413WR/Q58rW1bFtxw5dR/uZg+9byn28mPP6+rr/XmIcG/pedPLXvp5xp2/aps/QSWPf2DDdGLPpwk2Mx9MwYUMzN3bZ7fC+vpjm6zXQ55sZtNYia+JWTR/ebFZ7lzZ0v77XSrh9+qflzZtFJ415aWnZffV85vWk+9Dssm7lms36Y/OmpbFrx65x1dXXjOUtq15r+twsuq+vb4wTJ06MCxcujJ07d+hn59gmerImyysr7tN9aU039MPvZfXP2OAvj19jUqPVt9ZoZXXs3rt3bN22bZw4eXI8dfwpX3uJ8XC9aE9/58+fdz9bt24ND7it8FfIDIHCrxsaM/0v+f2SyJI1oM26JRRjLPpv06ZL4r5L4pWN0JzPWWOuh59MUfGxaAOx6MNcU/wCDTOerNNl+vXa5Tro7DH4ojG2bN0ydu/eM5aXV8fpM2fGGf2wjh4jvK8+t4r220STVdOXdjMG5Gvt4vo4d+68a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ytBeBgAAAABJRU5ErkJggg==

why it generate PNG file successfully, but why it can't be opened? why the PNG file is corrupt?

UPDATE : here's my complete jquery script

    $(".pepe-thumbnail").click(function(){
        var pepe_src = $(this).attr("src");
        $("#canvasimg").attr("src", pepe_src);
    });

    var canvas = document.getElementById("canvas");
    ctx = canvas.getContext("2d");

    var deviceWidth = window.innerWidth;;

    canvasWidth = deviceWidth - 40;
    canvasHeight = deviceWidth - 40;

    canvas.width = canvasWidth;
    canvas.height = canvasHeight;

    if (canvas.width > 500 || canvas.height > 500){
        canvas.width = 560;
        canvas.height = 500;    
    }

    var img = document.getElementById("canvasimg");
    //imgx = canvas.width/2 - img.width/2;
    //imgy = canvas.height/2 - img.height/2;

    imgx = 0;
    imgy = 0;

    function runLoop(){             
        ctx.lineWidth  = 8;
        ctx.font = "26pt Lato";
        ctx.strokeStyle = "black";
        ctx.fillStyle = "white";
        ctx.textAlign = "center";
        ctx.lineJoin = "round";

        var text1 = document.getElementById("canvastext-top").value;
        //text1 = text1.toUpperCase();

        var text2 = document.getElementById("canvastext-bottom").value;
        //text2 = text2.toUpperCase();

        x = canvas.width/2;
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, imgx, imgy, canvas.width, canvas.height);

        ctx.strokeText(text1, x, 50);
        ctx.fillText(text1, x, 50);

        ctx.strokeText(text2, x, (canvas.height - 40));
        ctx.fillText(text2, x, (canvas.height - 40));

        window.setTimeout(runLoop, 14);
    };
    runLoop();

    $(".img-download").click(function(){
        var data = canvas.toDataURL();
        alert (data);
    });
Saint Robson
  • 5,475
  • 18
  • 71
  • 118
  • Can you share a demo ? – Rayon Jun 17 '16 at 09:43
  • I have attempted to convert the base64 manually but, apparently, it is not a valid base64 string. Are you sure what you are getting is correct? Try and set the `` src to that base64 string, if the correct image appears then it rules that out. Just an idea to isolate where the problem may lie – wmash Jun 17 '16 at 09:55

2 Answers2

2

Try this :

$(".img-download").click(function(){
    var data = canvas.toDataURL();
    $(this).attr("href",data)
  $(this).attr("download","imgName.png");
}); 

Ref:Download canvas image using JS Jquery

Community
  • 1
  • 1
Shweta Matkar
  • 301
  • 1
  • 11
0
var data = canvas.toDataURL();
dataURL = data.replace(/^data:image\/[^;]*/, 'data:application/octet-stream');
dataURL = dataURL.replace(/^data:application\/octet-stream/, 'data:application/octet-stream;headers=Content-Disposition%3A%20attachment%3B%20filename=Canvas.png');

var aTag = document.createElement('a');
aTag.download = 'download.png';
aTag.href = dataURL;
aTag.click();
ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
Soke91
  • 1
  • 1
  • Please add some explanations of your "solution". – ADreNaLiNe-DJ Jun 17 '16 at 10:00
  • While this code snippet may solve the question, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Box Box Box Box Jun 17 '16 at 12:05