I've the code which takes the screen shot of the specified div tab. But I'm encountering the problem to display it as .png file from base64 format
Here is my code.
It has two separate files
1) Download.php
<script type="text/javascript"src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="js/html2canvas.js"></script>
<script type="text/javascript" src="js/jquery.plugin.html2canvas.js"></script>
<!-- -->
<div id="target">
<img src="images/1.jpg" alt="Smiley face" width="100" height="100">
</div>
<script type="text/javascript">
function capture() {
$('#target').html2canvas({
onrendered: function (canvas) {
//Set hidden field's value to image data (base-64 string)
$('#img_val').val(canvas.toDataURL("image/png"));
//Submit the form manually
document.getElementById("myForm").submit();
}
});
}
</script>
<input type="submit" value="Take Screenshot Of Div" onclick="return capture();" />
<form method="POST" enctype="multipart/form-data" action="save.php" id="myForm">
<input type="hidden" name="img_val" id="img_val" value="" />
</form>
2)Save.php
<?php
//Show the image
$data = $_POST['img_val'];
//Get the base-64 string from data
$uri = substr($data, strpos($data, ",") + 1);
//Decode the string
$unencodedData = base64_decode($uri);
$im = str_replace($data, '+', 'img.png');
//Save the image
$v = file_put_contents($data, $im);
$result = '<img src="' . $im . '" />';
print_r($result);
?>
I need the image to be displayed as localhost/xyz/screenshot/img.png which I'm unable to do.Please see the code help me to figure out that problem.
Thanks in advance.
I cant see any element in html with id attribute of target in your code but you are using it
$('#target').html2canvas({
Similar question [here][1]
I have found some thing usefull
var html2obj = html2canvas($('body'));
var queue = html2obj.parse();
var canvas = html2obj.render(queue);
var img = canvas.toDataURL();
window.open(img);
[1]: http://stac
koverflow.com/questions/10457608/create-screenshot-of-webpage-using-html2canvas-unable-to-initialize-properly
Related
I am trying to take image fron webcam then store it in a file as jpeg format. So far this code captures image and convert that in base64 encoding. Now how can I store it in folder. This is my first time. I am working a long time with it. How can I fix it.
php
<?php
if(isset($_POST["mydata"])) {
$encoded_data = $_POST['mydata'];
$binary_data = base64_decode( $encoded_data );
// save to server (beware of permissions)
$result = file_put_contents( 'webcam.jpg', $binary_data );
// move_uploaded_file ( "test.png" , "/img" );
//if (!$result) die("Could not save image! Check file permissions.");
}
?>
html
<body>
<div id="results">Your captured image will appear here...</div>
<h1>WebcamJS Test Page</h1>
<div id="my_camera"></div>
<!-- First, include the Webcam.js JavaScript Library -->
<script type="text/javascript" src="webcam.min.js"></script>
<!-- Configure a few settings and attach camera -->
<script language="JavaScript">
Webcam.set({
width: 180,
height: 100,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach( '#my_camera' );
</script>
<!-- A button for taking snaps -->
<input type=button value="Take Snapshot" onClick="take_snapshot()">
<form id="myform" method="post" action="" enctype="multipart/form-data">
<input id="mydata" type="hidden" name="mydata" value=""/>
</form>
<!-- Code to handle taking the snapshot and displaying it locally -->
<script language="JavaScript">
function take_snapshot() {
// take snapshot and get image data
Webcam.snap( function(data_uri) {
// display results in page
document.getElementById('results').innerHTML =
'<h2>Here is your image:</h2>' +
'<img src="'+data_uri+'"/>';
var raw_image_data = data_uri.replace(/^data\:image\/\w+\;base64\,/, '');
document.getElementById('mydata').value = raw_image_data;
document.getElementById('myform').submit();
} );
}
</script>
Try below code, it will convert your base64 data to png and save it with random name.
I am assuming you are doing everything right, just doing mistake while converting base64 to image.
Code :
<?php
if(isset($_POST["mydata"])) {
define('UPLOAD_DIR', 'uploads/');
$encoded_data = $_POST['mydata'];
$img = str_replace('data:image/jpeg;base64,', '', $encoded_data );
$data = base64_decode($img);
$file_name = 'image_'.date('Y-m-d-H-i-s', time()); // You can change it to anything
$file = UPLOAD_DIR . $file_name . '.png';
$success = file_put_contents($file, $data);
}
I have rendered an image via html2canvas and jquery:
<script>
function capture() {
console.log("function is running");
jQuery('#square').html2canvas({
onrendered: function(canvas) {
var url;
url = canvas.toDataURL("image/png");
//Set hidden field's value to image data (base-64 string)
jQuery('#img_val').val(canvas.toDataURL("image/png"));
//Submit the form manually
//var myImage = canvas.toDataURL("image/png");
window.open(url);
jQuery("#myForm").submit();
console.log(jQuery('#img_val').val());
}
});
}
</script>
The code works all fine, and I see the image via the window.opne.
The issue I have is when I submit my form holding the image value is NOT passed. I know it's set because I see that via console.
My form:
<input type="submit" value="Take Screenshot Of Div" onclick="capture();" />
<form method="POST" enctype="multipart/form-data" action="/save.php" id="myForm">
<input type="hidden" name="img_val" id="img_val" value="" />
</form>
My Save.php
<?php
//save.php code
//Show the image
var_dump($_POST['img_val']);
echo 'Billede:<br />';
echo '<img src="'.$_POST['img_val'].'" />';
//Get the base-64 string from data
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Save the image
file_put_contents('img' . rand(0,10000) . '.png', $unencodedData);
?>
Why is the value of the renderend image not passed via my form - when I know I set it via jQuery ?
(beginner)
I'm having trouble uploading my file. I see the filename being posted to the database, but the file is not being posted to the images folder. It seems as though nothing is happening. Here is my following code, please advise me what I need to change:
<?php
//the $art variable gets posted to a database eventually
$art = mysql_real_escape_string(stripslashes($_FILES["art"]["name"]));
$art_ext = pathinfo($art, PATHINFO_EXTENSION);
$art = md5($art).".".$art_ext;
if($art!=""){
move_uploaded_file($art, "images/".$art );
}
?>
<script type="text/javascript">
$(function(){
image_upload = {
UpdatePreview: function(obj){
// if IE < 10 doesn't support FileReader
if(!window.FileReader){
// don't know how to proceed to assign src to image tag
} else {
var reader = new FileReader();
var target = null;
reader.onload = function(e) {
target = e.target || e.srcElement;
$("#imageupload").attr("src", target.result);
};
reader.readAsDataURL(obj.files[0]);
}
}
};
});
</script>
<form action="new.php" method="post" enctype="multipart/form-data">
<input type='file' name='art' id="file" onchange='image_upload.UpdatePreview(this)' value="Upload" accept="image/gif,image/jpeg,image/png"/>
</p>
<p>upload a image! (.gif, .jpg, .png formats allowed. 5MB max)</p>
<img id="imageupload" src="1x1.png" alt="test" />
<input type="submit" class="smallbtn4" style="cursor:pointer;" value="post"/>
</form>
Here's the format for move_uploaded_file():
$path = 'images/';
move_uploaded_file($_FILES['art']['tmp_name'], $path.basename($_FILES['art']['name']));
when using move_uploaded_files() your destination path should also include the name of the file....right now your destination path is:
images/
it should be:
images/nameOfImg.ext
Hope this helps!
EDIT:
After seeing the comment by #enhzflep, you should also hash the name of the file and create your filename string before using it in move_uploaded_file();
Check out this link.
How is the code working out? I mean the src attribute of IFRAME is set to # then how does upload.php get to know that the form was submitted?
Here is the code : index.php :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Max's AJAX File Uploader</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
<!--
function startUpload(){
document.getElementById('f1_upload_process').style.visibility = 'visible';
document.getElementById('f1_upload_form').style.visibility = 'hidden';
return true;
}
function stopUpload(success){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
}
else {
result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
}
document.getElementById('f1_upload_process').style.visibility = 'hidden';
document.getElementById('f1_upload_form').innerHTML = result + '<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
document.getElementById('f1_upload_form').style.visibility = 'visible';
return true;
}
//-->
</script>
</head>
<body>
<div id="container">
<div id="header"><div id="header_left"></div>
<div id="header_main">Max's AJAX File Uploader</div><div id="header_right"></div></div>
<div id="content">
<form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
<p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p>
<p id="f1_upload_form" align="center"><br/>
<label>File:
<input name="myfile" type="file" size="30" />
</label>
<label>
<input type="submit" name="submitBtn" class="sbtn" value="Upload" />
</label>
</p>
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
</div>
<div id="footer">Powered by AJAX F1</div>
</div>
</body>
</html>
upload.php :
<?php
// Edit upload location here
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$result = 0;
$target_path = $destination_path . basename( $_FILES['myfile']['name']);
if(#move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$result = 1;
}
sleep(1);
?>
<script language="javascript" type="text/javascript">window.top.window.stopUpload(<?php echo $result; ?>);</script>
The flow in this code is pretty simple if you follow the HTML form tag an it's related attributes. What is happening is the follwing:
Select upload
Submit form
The onSubmit() triggers a JS function which enables the loading.. visualization and disables the upload form. Because of the * return true* the form is still submitted.
The form submit is done into the iFrame because of the 'target=' attribute. With that action you make the iFrame source 'upload.php'
Upload.php is called from within the iFrame.
The Upload.php result calls the main window JS function 'stopUpload' which informs on the rest of actions to take.
This script will work although it's result might change cross browser. This is a good script for a fast AJAX upload implementation.
You have form action set to upload.php <form action="upload.php" hence when you submit upload.php knows form is submitted (if i understand your question correctly then above line is true)
My code is somewhat like this:
<?php
if($_REQUEST['post'])
{
$title=$_REQUEST['title'];
$body=$_REQUEST['body'];
echo $title.$body;
}
?>
<script type="text/javascript" src="texteditor.js">
</script>
<form action="" method="post">
Title: <input type="text" name="title"/><br>
<a id="bold" class="font-bold"> B </a>
<a id="italic" class="italic"> I </a>
Post: <iframe id="textEditor" name="body"></iframe>
<input type="submit" name="post" value="Post" />
</form>
The texteditor.js file code is:
$(document).ready(function(){
document.getElementById('textEditor').contentWindow.document.designMode="on";
document.getElementById('textEditor').contentWindow.document.close();
$("#bold").click(function(){
if($(this).hasClass("selected")){
$(this).removeClass("selected");
}
else{
$(this).addClass("selected");
}
boldIt();
});
$("#italic").click(function(){
if($(this).hasClass("selected")){
$(this).removeClass("selected");
}
else{
$(this).addClass("selected");
}
ItalicIt();
});
});
function boldIt(){
var edit = document.getElementById("textEditor").contentWindow;
edit.focus();
edit.document.execCommand("bold", false, "");
edit.focus();
}
function ItalicIt(){
var edit = document.getElementById("textEditor").contentWindow;
edit.focus();
edit.document.execCommand("italic", false, "");
edit.focus();
}
function post(){
var iframe = document.getElementById("body").contentWindow;
}
Actually I want to fetch data from this text editor (which is created using iframe and javascript) and store it in some other place. I'm not able to fetch the content that is entered in the editor (i.e. iframe).
Please help me out in this.
You can't do that because it is not allowed.
Javascript is not allowed access to other frames/iframes, as it would result in a security breach.
Try to implement your editor inline in a <div></div> instead of iframe