Skip to content

Instantly share code, notes, and snippets.

@hanafiah
Created January 9, 2013 23:26
Show Gist options
  • Save hanafiah/4497976 to your computer and use it in GitHub Desktop.
Save hanafiah/4497976 to your computer and use it in GitHub Desktop.
image to canvas
<!DOCTYPE HTML>
<html>
<body>
<?php
$img = 'http://www.saudigazette.com.sa/myfiles/Images/2012/12/18/li02.jpg';
$type = pathinfo($img, PATHINFO_EXTENSION);
$data = file_get_contents($img);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
?>
Image
<img src="<?php echo $img;?>" />
<hr>
Using Base64 Image
<img src="<?php echo $base64; ?>" />
<hr>
Using Canvas
<canvas id="myCanvas" width="578" height="500"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.onload = function() {
context.drawImage(imageObj,0,0);
};
imageObj.src = '<?php echo $base64; ?>';
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment