In this example, this scripts will allow you to take screenshot of the div directly on the user browser.
In div, what you write content in this tag it will be convert in the image and can save image of div as a screenshot. If you put image in div tag, then we can also take screenshot of image what we mention between div tag.
type="text/javascript"src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">type="text/javascript"src="js/html2canvas.js">type="text/javascript"src="js/jquery.plugin.html2canvas.js">html2canvas With JavaScript and PHPmethod="POST"enctype="multipart/form-data"action="save.php"id="form1">type="hidden"name="img_value"id="img_value"value=""/>valign="top"style="padding: 10px;">Div:id="target"style="border: 1px solid #CCC;padding: 5px;margin: 5px;">PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language.colspan="2">width="100%">type="submit"value="Screenshot"onclick="capture();"/>type="text/javascript">functioncapture()$('#target').html2canvas( onrendered:function(canvas)//Set hidden field's value to image data$('#img_value').val(canvas.toDataURL("image/png"));//Submit the form1document.getElementById("form1").submit();>>);>
$filterData=substr($_POST['img_value'],strpos($_POST['img_value'],",")+1);//Get the base-64 string from data$decodeData=base64_decode($filterData);//Decode the stringfile_put_contents('screenshot.png',$decodeData);//Save the image?>
Save the image and show to user
echo'.$_POST['img_value'].'" />';?>
If you want to put image in div , then just change this code in index,php file. See this code and change it.
id="target"style="border: 1px solid #CCC;padding: 5px;margin: 5px;">PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language.
id="target"style="border: 1px solid #CCC;padding: 5px;margin: 5px;">src ="a.png"/>//image which is available in the main folder where code file is available
For more details on how this works, see Creating an image.
Example API response:
Authentication with PHP
Your username is your User ID and your password is your API Key. Both of these are available from the dashboard. The PHP code sample demonstrates how to authenticate your request.
You can signup for a free API key to get started.
PHP example code
This PHP code example sends an HTTP POST to the https://hcti.io/v1/image API. Converting your HTML/CSS to an image with PHP.
For more details on how this works, see Creating an image.
Example API response:
Authentication with PHP
Your username is your User ID and your password is your API Key. Both of these are available from the dashboard. The PHP code sample demonstrates how to authenticate your request.
You can signup for a free API key to get started.
PHP example code
This PHP code example sends an HTTP POST to the https://hcti.io/v1/image API. Converting your HTML/CSS to an image with PHP.
Convert (div) Html to Image using Javascript html2canvas with Example and Save using Ajax, PHP.
Sometimes you need to convert your entire div to the image. In this post, I am going to Convert the div or HTML to image with using jquery, javascript html2canvas with an example. and you can copy the code and apply to your website. This code is tested by my self as a developer having 5 years of experience in the same field. If you have the knowledge of jQuery, PHP coding then it will be more easy to convert HTML to image using javascript html2canvas and saving using Ajax, PHP. Here i am going to use https://html2canvas.hertzen.com/ 1.For example i have HTML like below:
Developergang.com
Welcome to Developergang.com
$imagedata = base64_decode($_POST['imgdata']); $filename = md5(uniqid(rand(), true)); //path where you want to upload image $file = $_SERVER['DOCUMENT_ROOT'] . '/uploads/'.$filename.'.png'; $imageurl = 'http://example.com/uploads/'.$filename.'.png'; file_put_contents($file,$imagedata); echo $imageurl;
Full Code
Developergang.com
Welcome to Developergang.com
save_image.php
$imagedata = base64_decode($_POST['imgdata']); $filename = md5(uniqid(rand(), true)); //path where you want to upload image $file = $_SERVER['DOCUMENT_ROOT'] . '/uploads/'.$filename.'.png'; $imageurl = 'http://example.com/uploads/'.$filename.'.png'; file_put_contents($file,$imagedata); echo $imageurl;
Hope that helps to understand html2canvas and Convert html to image javascript. If you still have queries, we will happy to help. Do comment below. Also if you want to Submit your post with the answer (Solution) or Submit Blog Post. You can visit submit blog post page.
I am trying to make an image editor kind of page. Person can add text and styles to the background image. Everything i have done through HTML, Jquery and css, but struck up with export it to an image. I want to convert the particular div into image.
Note: I already know HTML to image conversion is possible through SVG and HTML5 Canvas. I cannot do those things because of browser compatibility.
If any PHP classes or jquery plugin is available please help me.
Answer
Solution:
I think it is not possible without HTML5 features on the client side directly. May be I wrong?
My solution is to send the entire div (with styles) to backend server, where capture a screenshot with PhantomJS or Qt Webkit. On the server side you shoold open the DIV user created as it displayed in user browser and then make screenshot of this DIV.
Share solution ↓
Additional Information:
Didn’t find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license. https://www.php.net/
JQuery
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience. https://jquery.com/
CSS
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language. It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL. https://www.w3.org/TR/CSS/#css
HTML
HTML (English «hyper text markup language» — hypertext markup language) is a special markup language that is used to create sites on the Internet. Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone. https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.