2

how to convert image url to base64 encoded data url without using html5 canvas object.

canvas.todataURL()

I am gettting base64 data using canvas.todataURL, but it fails to read remote url's, so I want to convert my image url to dataurl with out using canvas.todataURL() and use it in my application.

HOw can I to do this.

2
  • You can actually use canvas.toDataUrl() with remote images if you use Cross-origin resource sharing. The browser support is still pretty poor though. Commented Sep 19, 2013 at 13:43
  • 1
    @Strille: Update--Good news! Actually, most modern browsers do support cross-browser XMLHttpRequests Good on: ie10,FF,webkit,ios,blackberry, Awkward implementation on: ie9, no support on: opera-mini. And of course ie<9 has no canvas support. Enabling cross-domain sharing on the server hosting your images can be tricky, but once set, you're good. Some "cloud" image servers like dropbox.com offer cross-domain by default. Commented Sep 19, 2013 at 17:14

2 Answers 2

2

You can use this code-

HTML-

    <img id="preview" src="http://www.gravatar.com/avatar/0e39d18b89822d1d9871e0d1bc839d06?s=128&d=identicon&r=PG">
    <canvas id="myCanvas" />

javascript-

    <script>
     var c = document.getElementById("myCanvas");
     var ctx = c.getContext("2d");
     var img = document.getElementById("preview");
     ctx.drawImage(img, 10, 10);
     alert(c.toDataURL());
    </script>

found it here- How to get base64 encoded data from html image

Sign up to request clarification or add additional context in comments.

Comments

0

You can't do it purely in javascript without using canvas.toDataUrl(). You would have to do something server-side. Basically create a simple service which takes an url to an image as parameter and then returns that image as a base64 string.

1 Comment

hmm... yes i did creating some service to send image url and return base64 string but in my application there is no chance to use any server side script, It is purely on html and javascript.. that is why I'm trying to do it on pure javascript..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.