title: create image with HTML content using PHP GD library
body: <p>I am new in PHP GD library. Basically, my requirement is to create an image using HTML content. HTML content might have HTML tags like b, i, img etc (include all available HTML tags).
  We will also have to set font-name, font size, font-color, font-weight and x-y position of HTML element. All HTML tags need to be rendered on a PNG image called background image (which will be dynamic and different in every image).</p>
  
  <p>I have also tried the solution from the <a href="https://stackoverflow.com/questions/10721884/render-html-to-an-image">Render HTML to an image</a> but unfortunately it does not render image tag. My code is as follows: </p>
  
  <pre><code>const {body} = document;
  
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  canvas.width = canvas.height = 500;
  
  const tempImg = document.createElement('img');
  tempImg.addEventListener('load', onTempImageLoad);
  tempImg.src = 'data:image/svg+xml,' + encodeURIComponent('&lt;svg xmlns="http://www.w3.org/2000/svg" width="500" height="500"&gt;&lt;foreignObject width="100%" height="100%"&gt;&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;div style="background-color: #F0F0F1; color: #00cc65; width: 500px; padding-left: 25px; padding-top: 10px;"&gt;&lt;strong&gt;Codepedia.info&lt;/strong&gt;&lt;hr/&gt;&lt;h3 style="color: #3e4b51;"&gt;Html to canvas, and canvas to proper image &lt;/h3&gt;&lt;img src="https://www.dike.lib.ia.us/images/sample-1.jpg/" height="100" width="100" /&gt;&lt;p style="color: #3e4b51;"&gt; &lt;b&gt;Codepedia.info&lt;/b&gt; is a programming blog. Tutorials focused on Programming ASP.Net, C#, jQuery, AngularJs, Gridview, MVC, Ajax, Javascript, XML, MS SQL-Server, NodeJs,  Web Design, Software&lt;/p&gt;&lt;p style="color: #3e4b51;"&gt;&lt;b&gt;html2canvas&lt;/b&gt; script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/foreignObject&gt;&lt;/svg&gt;');
  
  const targetImg = document.createElement('img');
  body.appendChild(targetImg);
  
  function onTempImageLoad(e) {
    ctx.drawImage(e.target, 0, 0);
    targetImg.src = canvas.toDataURL();
  }
  </code></pre>
  
  <p>I have used img tag in above content but it does not render by using above code, please review and suggest solution for this.</p>
  
  <p>I have also tried html2canvas.js to generate image (reference link is <a href="https://codepedia.info/convert-html-to-image-in-jquery-div-or-table-to-jpg-png/" rel="nofollow noreferrer">html2canvas</a>). It successfully rendered img tag but i am unable to download the image using toDataURL() function. If i removed the img tag then using the same code snippet i am able to download the image. htmltocanvas code is as follows:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
   &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"&gt;&lt;/script&gt;
   &lt;script src="https://files.codepedia.info/files/uploads/iScripts/html2canvas.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px; padding-left: 25px; padding-top: 10px;"&gt;
          &lt;strong&gt;Codepedia.info&lt;/strong&gt;&lt;hr/&gt;
          &lt;h3 style="color: #3e4b51;"&gt;
              Html to canvas, and canvas to proper image
          &lt;/h3&gt;
          &lt;img src="https://www.dike.lib.ia.us/images/sample-1.jpg" height="100" width="100" /&gt;
          &lt;p style="color: #3e4b51;"&gt; 
              &lt;b&gt;Codepedia.info&lt;/b&gt; is a programming blog. Tutorials focused on Programming ASP.Net,
              C#, jQuery, AngularJs, Gridview, MVC, Ajax, Javascript, XML, MS SQL-Server, NodeJs,
              Web Design, Software
          &lt;/p&gt;
          &lt;p style="color: #3e4b51;"&gt;
              &lt;b&gt;html2canvas&lt;/b&gt; script allows you to take "screenshots" of webpages or parts
              of it, directly on the users browser. The screenshot is based on the DOM and as
              such may not be 100% accurate to the real representation.
          &lt;/p&gt;
      &lt;/div&gt;
  
      &lt;a id="btn-Convert-Html2Image" href="#"&gt;Download&lt;/a&gt;&lt;br/&gt;
      &lt;h3&gt;Preview :&lt;/h3&gt;
      &lt;div id="previewImage"&gt;&lt;/div&gt;
  
  &lt;script&gt;
  $(document).ready(function(){
  
  var element = $("#html-content-holder"); // global variable
  var getCanvas; // global variable
  
      window.onload = (function() {
          html2canvas(
              element, {allowTaint : true,
              onrendered: function (canvas) {
                  $("#previewImage").append(canvas);
                  getCanvas = canvas;
              }
          });
      });
  
      $("#btn-Convert-Html2Image").on('click', function () {
          var imageData = getCanvas.toDataURL("image/png");
          // Now browser starts downloading it instead of just showing it
          var newData = imageData.replace(/^data:image\/png/, "data:application/octet-stream");
          $("#btn-Convert-Html2Image").attr("download", "your_pic_name.png").attr("href", newData);
      });
  });
  
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;   
  </code></pre>
  
  <p>Please suggest solution for any of above method.
  Please suggest. How to create an image using HTML content? 
  Apart from the GD library, is there another way to do this?</p>