I have a script which allows me to choose an image, upload as a Blob, then attempts to upload it to a folder on the server
<script src="assets/js/jquery-1.11.3.min.js"></script><script type="text/javascript" src="assets/js/html2canvas.js"></script>
<script type="text/javascript" src="assets/js/jquery.plugin.html2canvas.js"></script>
<a onclick="capture();">Take shot</a>
<div id="target" style="position: relative; width: 300px; height: 300px;">
<input type="file" capture="camera" accept="image/*" id="takePictureField">
<img id="yourimage" style="position: relative; width: 300px; height: 300px;">
</div>
<script type="text/javascript">
var desiredWidth;
$(document).ready(function() {
$("#takePictureField").on("change",gotPic);
desiredWidth = window.innerWidth;
if(!("url" in window) && ("webkitURL" in window)) {
window.URL = window.URL;
}
});
function gotPic(event) {
if(event.target.files.length == 1 &&
event.target.files[0].type.indexOf("image/") == 0) {
$("#yourimage").attr("src",URL.createObjectURL(event.target.files[0]));
}
}
function capture() {
$('#target').html2canvas({
onrendered: function (canvas) {
var img = canvas.toDataURL("image/png");
var output = encodeURIComponent(img);
var cur_path = 'uploads';
var Parameters = "image=" + output + "&filedir=" + cur_path;
$.ajax({
type: "POST",
url: "savePNG.php",
data: Parameters,
success : function(data)
{
console.log("screenshot done");
}
}).done(function() {
//$('body').html(data);
});
}
});
}
</script>
php
<?php
$image = $_POST['image'];
$filedir = $_POST['filedir'];
$name = time();
$image = str_replace('data:image/png;base64,', '', $image);
$decoded = base64_decode($image);
file_put_contents("uploads/" . $name . ".png", $decoded, LOCK_EX);
?>
I have tested and am able to snapshot the screen if the image is already there. But when trying to snapshot the screen after the image has been chosen and is on screen, the image file appears in the folder, but the png file is blank.
The image created is a blob, and comes up with 'blob:http' in the img src. Is this an issue with trying to upload the image in the page loaded state, ie: it doesnt see the image url with 'blob:http'?
Does anyone know if FileReader.js will do the job?
EDIT: This is what the image looks like after image upload:
<img id="yourimage" style="position: relative; width: 300px; height: 300px;" src="blob:http%3A//website.com/def20622-14dd-4db8-8749-bb4a85d4f62d">