Convert Div to image using jQuery, PHP & HTML Canvas


Hi Friends, It’s been a long time, so today I’m writing an interesting topic about how to capture a DIV content and save it as an image using PHP.

And I didn’t stop simply capturing any div but also giving an option to add a text with a drag option and two backgrounds to select with the help of jQuery.

If you wish to see the demo first, please go ahead and click the demo button below:




And to capture the DIV content I’m using a special Javascript developed by Niklas von Hertzen.
You can find the script and examples here,

Ok lets’ begin what are the scripts.

What you should know to understand the script?
1. You should have the intermediate knowledge of jQuery and PHP.
2. And you should have the habit of reading comments in the script. 😉


Two files, index.html and save_png.php

Explore index.html:

<!DOCTYPE html>
    <title>Capture Webpage Screenshot</title>
	<!-- include the jquery and jquery ui -->
	<script src=""></script>
	<script src=""></script>
	<!-- this script helps us to capture any div --> 
	<script src="html2canvas.js"></script>

		/* these are just styles added for this demo page */
			width: 572px;
			height: 400px;
			background-image: url(bg_img/bg.jpg);
			margin: 0 auto;
			color: white;
			font-family: Verdana;
			cursor: move;
			position: absolute;
			width: 510px;
			margin: 0 auto;
			background: purple;
			border: 1px solid black;
			color : white;
			padding: 30px;
	<h3 align="center"> - See the tutorial here <a href="">Click Here</a></h3>
	<div id="design">
		<div id="controls">
			Text: <input type="text" value="Agurchand" id="textbox" /> <input type="button" value="Capture" id="capture" /><br /><br />				
			Font Size: <input id="slider" type ="range" min ="12" max="50" value ="0"/><br /><br />
			Select Background: <select id="background"><option value="bg.jpg">Background 1</option><option value="bg2.jpg">Background 2</option></select>
		<br />
	<div id="canvas">
		<div class="movable_div">Agurchand</div>
	<p align="center">Drag the text and place it wherever you want on the picture</p>
    <script type="text/javascript">	
			//to make a div draggable
				{containment: "#canvas", scroll: false}
			//to capture the entered text in the textbox 
				var text = $(this).val();
			//to change the background once the user select
				var background = $(this).val();
				$('#canvas').css('background', 'url(bg_img/'+background+')');
			//font size handler here. 
				fontSize = $(this).val();
				$('.movable_div').css('font-size', fontSize+'px');

			//here is the hero, after the capture button is clicked
			//he will take the screen shot of the div and save it as image.
				//get the div content
				div_content = document.querySelector("#canvas")
				//make it as html5 canvas
				html2canvas(div_content).then(function(canvas) {
					//change the canvas to jpeg image
					data = canvas.toDataURL('image/jpeg');
					//then call a super hero php to save the image
		//to save the canvas image
		function save_img(data){
			//ajax method.
			$.post('save_jpg.php', {data: data}, function(res){
				//if the file saved properly, trigger a popup to the user.
				if(res != ''){
					yes = confirm('File saved in output folder, click ok to see it!');
						location.href =document.URL+'output/'+res+'.jpg';
					alert('something wrong');

Read the comments in the script to understand the operations.

Explore save_jpg.php:


//just a random name for the image file
$random = rand(100, 1000);

//$_POST[data][1] has the base64 encrypted binary codes. 
//convert the binary to image using file_put_contents
$savefile = @file_put_contents("output/$random.jpg", base64_decode(explode(",", $_POST['data'])[1]));

//if the file saved properly, print the file name
	echo $random;

Download the code here and learn yourself by exploring it.


14 thoughts on “Convert Div to image using jQuery, PHP & HTML Canvas

  1. function save_img(data){
    //ajax method.
    $.post(‘save_jpg.php’, {data: data}, function(res){
    //if the file saved properly, trigger a popup to the user.
    if(res != ”){
    yes = confirm(‘File saved in output folder, click ok to see it!’);
    location.href =document.URL+’output/’+res+’.jpg’;
    alert(‘something wrong’);
    in this function, After $.post(‘save_jpg.php’, {data: data}, function(res){ this line nothing is Coming? What is the Problem?

  2. Hi,
    Everything works well on my local setup. On moving to remote, i have got an issue. My control doesn’t enter html2canvas(div_content).then(function(canvas) {}

    Can you please help me know what the possible issue might be.

Leave a Reply

Theme: Overlay by Kaira
%d bloggers like this: