Skip to content

Instantly share code, notes, and snippets.

@leotsem
Created May 7, 2011 14:33
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leotsem/960542 to your computer and use it in GitHub Desktop.
Save leotsem/960542 to your computer and use it in GitHub Desktop.
Get binary image data from remote image
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Get binary image data from remote image</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script type="text/javascript">
$().ready(function(){
$('#form').submit(function(){
if ($('#image').val()) {
// create a new image object to hold the remote image
var img = new Image();
img.src = $('#image').val();
// force load image
$(img).load(function(){
// create a canvas object
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
// copy image contents to canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// get data from canvas object
var data = canvas.toDataURL("image/png");
// set results
$('#result_data').text(data);
$('#result_image').text('').append(canvas);
})
} else {
alert('Enter a valid image path');
}
return false;
})
});
</script>
</head>
<body>
<h1>Get binary image data from remote image</h1>
<form id="form">
<input type="text" id="image" size="100" style="padding:5px" value="http://dl.dropbox.com/u/36473/frogy.jpg"/>
<input type="submit" value="GET!"/>
</form>
<h2>Data</h2>
<textarea id="result_data" style="width:500px;height:200px"></textarea>
<h2>Image</h2>
<div id="result_image"></div>
</body>
</html>
@Andrew8xx8
Copy link

How are you resolve problem with security restrictions of HTML Canvas?

I tested your solution and it will not worked correctly.

@vrunoa
Copy link

vrunoa commented Jun 17, 2013

Yeah, I have the same problem. This only works with images on the same server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment