Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello!

I'm building a site where I'm letting people overlay images and then saving a screenshot of this on my server, using html2canvas and PHP. I'm not very experienced with either of them.

The problem is that the canvas only captures one of the overlaying images when saving the content of the canvas. Any suggestions on how I can go about solving this?

What I have tried:

I've built a test versjon of the page here: SKJER?[^] (I'm also having trouble with it sometimes not working when I overlay two or three images, getting the error: "Request Entity Too Large. The requested resource /bokbodentest/save.php does not allow request data with POST requests, or the amount of data provided in the request exceeds the capacity limit.") I've sent an email to my webhost about this.

My code is as follows:

Index.php:

<div class="left">
  <button class='toggleButton'>Hide / Show</button>
  <button class='toggleButton2'>Hide / Show</button>
  <button class='toggleButton3'>Hide / Show</button>
  
<form method="POST" enctype="multipart/form-data" action="save.php" id="myForm">
  <input type="hidden" name="img_val" id="img_val" value="">
</form>

            <input type="submit" value="Take Screenshot Of Div Below" onclick="capture();" />
</div>

<div id="target">
				<div id='toggleBox'><img src="bilde.jpg"></div>
        <div id='toggleBox2'><img src="bilde2.jpg"></div>
        <div id='toggleBox3'><img src="bilde3.jpg"></div>
      
<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>


PHP:

PHP
<?php
//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.png', $unencodedData);
?>
<h2>Save the image and show to user</h2>
<table>
    <tr>
        <td>
            <a href="img.png" target="blank">
            	Click Here to See The Image Saved to Server</a>
        </td>
        <td align="right">
            <a href="index.php">
            	Click Here to Go Back</a>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <br />
            <br />
			<span>
				Here is Client-sided image:
			</span>
            <br />
<?php
//Show the image
echo '<img src="'.$_POST['img_val'].'" />';
?>
        </td>
    </tr>
</table>
<style type="text/css">
body, a, span {
	font-family: Tahoma; font-size: 10pt; font-weight: bold;
}
</style>


JS:

JavaScript
$('.toggleButton').click(function(){
		$('#toggleBox').toggle();
	});

$('.toggleButton2').click(function(){
		$('#toggleBox2').toggle();
	});

$('.toggleButton3').click(function(){
		$('#toggleBox3').toggle();
	});

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();
        }
    });
}


<pre><? php //save.php code
 
//Show the image
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.png', $unencodedData);

?> 
Posted
Updated 5-Feb-17 8:16am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900