How to Generate Image from html using js?

Html To Image

This tutorial about generating the image from the HTML. You can just create the HTML and put this code after your code and it will generate the image from your HTML. below is the easy step to generate the image from custom HTML.

Step 1)

Create one php file like index.php as below

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Html To Image</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"> </script>
<script src="https://files.codepedia.info/files/uploads/iScripts/html2canvas.js"> </script>
<div class="bingo-box bingo-green generatedata" id="generatedata1" data-id="1">
<div class="number-row number-row1">
<div class="number number1">1</div>
<div class="number number2">2</div>
<div class="number number4">4</div>
<div class="number number5">5</div>
<div class="number number5">9</div>
</div>
</div>
<script>
        $(document).ready(function() {
            var getCanvas;
            $( ".generatedata" ).each(function( index ) {
                var dataid = $( this ).attr("id");
                var catid = $( this ).data("id");
                var datafid = parseInt(index) + parseInt(1);
                var element = $("#"+dataid);
                html2canvas(element, {
                    onrendered: function(canvas) {
                        getCanvas = canvas;
                        var imgageData = getCanvas.toDataURL("image/png");
                        var newData = imgageData.replace(/^data:image\/png/, "data:application/octet-stream");
                        $.ajax({
                          type: "POST",
                          url: "upload.php",
                          data: {
                             imgBase64: newData
                          }
                        }).done(function(o) {
                          console.log('saved');
                        });
                    }
                });
            });
        });
    </script>

Step 2)

Create another page like upload.php

<?php
$catid = $_POST['catid'];
$folderPath = "upload_img/";
$image_parts = explode(";base64,", $_POST['imgBase64']);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$filename = uniqid() . '.png';
$file = $folderPath . $filename;
file_put_contents($file, $image_base64);
?>

Step 3)

Now Just run index.php file to your browser this will generate the image in folder.

54 thoughts on “How to Generate Image from html using js?

  1. game online

    I like the valuable info you provide in your articles. I will bookmark your weblog and check again here frequently. I am quite sure I will learn lots of new stuff right here! Best of luck for the next! Ediva Si Belden

    Reply
  2. derma prime plus reviews

    Can I just say what a relief to discover a person that
    genuinely knows what they are talking about
    online. You actually understand how to bring a problem to light and make
    it important. A lot more people must read this and understand this side of the story.
    I can’t believe you aren’t more popular since you certainly have the gift.

    Reply
  3. derma prime plus reviews

    I like the helpful information you supply for
    your articles. I will bookmark your weblog and check again here regularly.
    I am slightly certain I will be told lots of new stuff
    right right here! Best of luck for the next!

    Reply
  4. derma prime plus reviews

    What i don’t realize is in reality how you
    are no longer really much more smartly-preferred
    than you may be right now. You are so intelligent. You realize therefore considerably on the subject of this
    topic, produced me in my view imagine it from numerous varied angles.
    Its Your own stuffs excellent.

    Reply
  5. HaroldEscob

    You wanted to see me, Sir?
    I was two steps into his office, my suit jacket sliding down my arms as I walked to my location between the chairs in front of his desk.
    “Something has come to my attention that might be interesting.”
    It wasn’t normal for him to blurt out what was on his mind when I was summoned to his office. We had established something of a protocol of how this was to work.
    It was akin to a tribal ritual of exchanging pleasant conversation prior to entering into serious exchange, a ritual intended to maintain civility and peace and calm prior to any interaction.
    I had thought ours was the same, though it had never been expressed or defined. I undressed and he sat back in his chair to enjoy it prior to entering into any business.

    Reply
  6. Tara

    Pretty great post. I just stumbled
    upon your weblog and wished to say
    that I’ve really enjoyed browsing your blog
    posts. After all I’ll be subscribing to your feed and I hope
    you write once more very soon!

    Reply
  7. katalog firm

    Hiya very nice site!! Guy .. Beautiful .. Amazing .. I will bookmark
    your blog and take the feeds also? I am happy to find numerous useful information here within the publish, we need work out extra techniques in this regard, thanks for
    sharing. . . . . .

    Reply
  8. goddess fiona

    I’m impressed, I have to admit. Rarely do I come across a
    blog that’s both educative and entertaining, and let
    me tell you, you’ve hit the nail on the head. The issue is
    an issue that too few men and women are speaking intelligently about.
    Now i’m very happy I stumbled across this in my search for something relating to this.

    Reply
  9. sexmex

    Thank you for another wonderful article. The place else could anyone get
    that type of information in such a perfect method of
    writing? I’ve a presentation subsequent week, and I’m on the search
    for such information.

    Reply
  10. tara tainton

    Wow, wonderful blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your website is excellent,
    let alone the content!

    Reply
  11. houston junk car buyer

    Great post. I was checking continuously this blog and I am
    impressed! Extremely useful information specifically the last part 🙂 I care for such information much.
    I was seeking this particular information for a very long time.
    Thank you and good luck.

    Reply
  12. news blog

    This is the perfect web site for anyone who would like to find out about this topic.

    You realize so much its almost hard to argue with you (not that I really would want to…HaHa).
    You certainly put a new spin on a topic that’s been written about for ages.
    Wonderful stuff, just excellent!

    Reply
  13. Rafaela

    That is a really good tip especially to those fresh to the blogosphere.
    Simple but very precise information… Appreciate your sharing this one.
    A must read article!

    Reply
  14. canadian pharmacy

    You actually make it seem so easy with your presentation but I find this topic to be actually something which I think I would never
    understand. It seems too complicated and extremely broad for me.

    I am looking forward for your next post, I will try to
    get the hang of it!

    Reply
  15. istanbul escort

    I’m not certain the place you are getting your information, but
    great topic. I must spend a while studying much more or figuring out more.

    Thank you for excellent info I was looking for this information for my mission.

    Reply
  16. bp77 login

    I’m pretty pleased to discover this web site.
    I want to to thank you for ones time just for this fantastic read!!
    I definitely liked every bit of it and i also have you saved as a favorite to see
    new information in your web site.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

  +  21  =  26