Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const nodeHtmlToImage = require('node-html-to-image');
  2. const fs = require('fs');
  3.  
  4. let colorIcon = "cup_c.png",
  5.     bwIcon = "cup_bw.png";
  6.  
  7. let red = "redsquare.png",
  8.     blue = "bluesquare.png";
  9.  
  10. // 1032x336
  11.  
  12. function generateHtml(numStamps, amtStamped, colorIcon, bwIcon) {
  13.     let numBottom = 0, numTop = 0, stampedTop = 0, stampedBottom = 0;
  14.  
  15.     if (numStamps > 5) { // Split into two rows if more than 5 stamps
  16.         numTop = Math.ceil(numStamps/2);
  17.         numBottom = numStamps - numTop;
  18.         stampedTop = amtStamped > numTop ? numTop : amtStamped;
  19.         stampedBottom = amtStamped > numTop ? amtStamped - numTop : 0;
  20.     } else {
  21.         numTop = numStamps;
  22.         stampedTop = amtStamped;
  23.     }
  24.  
  25.     let size, margins, rowSpacing = 0;
  26.  
  27.     if (!numBottom) {
  28.         size = numStamps < 4 ? 300 : numStamps < 5 ? 232 : 180;
  29.         margins = numStamps < 4 ? 22 : 13;
  30.     } else {
  31.         size = 160;
  32.         rowSpacing = 16;
  33.         margins = 23;
  34.     }
  35.  
  36.     let bodyStyle = "body {" +
  37.         "width: 1032px;" +
  38.         "height: 336px; }";
  39.     let iconStyle = ".icon {" +
  40.         "width: " + size + "px;" +
  41.         "height: " + size + "px;" +
  42.         "margin-right:" + margins + "px; " +
  43.         "margin-left:" + margins + "px;" +
  44.         "background-position:center;" +
  45.         "background-repeat: no-repeat;" +
  46.         "background-size: cover;" +
  47.         "display:inline-block;}";
  48.     let color = ".color {" +
  49.         "background-image: url('data:image/png;base64, " + toBase64(colorIcon) + "');}";
  50.     let bw = ".bw {" +
  51.         "background-image: url('data:image/png;base64, " + toBase64(bwIcon) + "');";
  52.  
  53.     let html = "<html><head><style>" +
  54.     "* {padding: 0;margin: 0;}" +
  55.     "#cont {width: 100%; height: 100%;}" +
  56.     ".row {width: fit-content;margin: 0 auto;}" +
  57.     bodyStyle +
  58.     iconStyle +
  59.     color +
  60.     bw +
  61.     "</style><body><div id='cont'>";
  62.  
  63.     html += "<div class='row' style='margin-bottom:" + rowSpacing + "px;'>";
  64.     for (let i = 0; i < stampedTop; i++) {
  65.         html += "<div class='icon color'></div>";
  66.     }
  67.     for (let j = 0; j < numTop - stampedTop; j++) {
  68.         html += "<div class='icon bw'></div>";
  69.     }
  70.     html += "</div>";
  71.    
  72.     if (numBottom) {
  73.         html += "<div class='row'>";
  74.         for (let x = 0; x < stampedBottom; x++) {
  75.             html += "<div class='icon color'></div>";
  76.         }
  77.         for (let y = 0; y < numBottom - stampedBottom; y++) {
  78.             html += "<div class='icon bw'></div>";
  79.         }
  80.     }
  81.  
  82.     html += "</div></div></body></html>";
  83.  
  84.     //fs.writeFile('./output.html', html, () => {});
  85.  
  86.     return html;
  87. }
  88.  
  89. function toBase64(url) {
  90.     let bitmap = fs.readFileSync(url);
  91.     return new Buffer(bitmap).toString('base64');
  92. }
  93.  
  94. nodeHtmlToImage({
  95.     output: './strip1.png',
  96.     html: generateHtml(9, 6, colorIcon, bwIcon)
  97. }).then(() => {
  98.     console.log("image created.");
  99. });
  100.  
  101. //console.log(generateHtml(3,3,2,'cup_c.png', 'cup_bw.png'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement