How to Generate Image from html using js?

How to Generate Image from html using js?

How to Generate Image from html using js?

This tutorial is 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>

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 the index.php file in your browser; this will generate the image in the folder.

24 responses

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

  1. Escort Bayan

    This is really attention-grabbing, You are an excessively professional blogger. Kira Sheff Chancey

  2. Saymarilyn

    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

  3. kavin

    Hi i am kavin, its my first occasion to commenting anyplace, when i read this paragraph i
    thought i could also create comment due to this good
    piece of writing.

  4. Derma Prime

    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.

  5. Marijuana Gummies

    Really appreciate you sharing this article.Really looking forward to read more. Fantastic.

  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!

  7. Santo Winter

    terrific as well as fantastic blog. I really intend to thank
    you, for providing us far better details.

  8. Shanon Lavarack

    I like what you guys are usually up too. This sort of clever work and exposure!

    Keep up the very good works guys I’ve included you
    guys to blogroll.

  9. Cesared Mundlatouche

    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. . . . . .

  10. 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.

  11. Larisafiery

    We make shirts for the kind of person who has had a three-hour conversation about their Golden. With strangers. While the dog was right there. snarkpaws.com

  12. FFscamRah

    FixedFloat.com is a scam exchange—they stole my money! ff.io send cryptocurrency with HIGH AML! ff.io linked to gambling and drugs!

Leave a Reply

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