Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcircuit3 committed Jun 9, 2020
0 parents commit 2f1b11f
Show file tree
Hide file tree
Showing 8 changed files with 1,624 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
Binary file added LeagueSpartan-Bold.otf
Binary file not shown.
113 changes: 113 additions & 0 deletions image.html

Large diffs are not rendered by default.

Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ext": "html"
}
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "image-api",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"add": "^2.0.6",
"express": "^4.17.1",
"node-html-to-image": "^2.1.1",
"yarn": "^1.22.4"
},
"devDependencies": {
"nodemon": "^2.0.4"
},
"scripts": {
"start": "nodemon server.js"
}
}
80 changes: 80 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const express = require("express");
const app = express();
const nodeHtmlToImage = require("node-html-to-image");
const fs = require("fs");
const path = require("path");

const port = 5000;

const html = fs.readFileSync(path.resolve(__dirname, "image.html"), "utf8");

const colors = {
yellow: "#F9DE62",
magenta: "#C66EDE",
green: "#7FD958",
blue: "#34B8FF",
turquoise: "#5CE1E6",
orange: "#FB8F4E",
purple: "#8A54F4",
pink: "#FF62C8",
};

const randomColor = () => {
const colorArray = Object.values(colors);
return colorArray[Math.floor(Math.random() * colorArray.length)];
};

app.get(`/api/insta`, async function (req, res) {
let color = randomColor();

if (!req.query.path) {
res.status(400).send("You need to specify website path");
}

if (!req.query.city) {
res.status(400).send("You need to specify city");
}

if (req.query.color && colors.hasOwnProperty(req.query.color)) {
color = colors[req.query.color];
}

const { path, city } = req.query;

const image = await nodeHtmlToImage({
output: "./image.png",
quality: 100,
content: { path, city, color },
html,
// html: `<html>
// <head>
// <link
// href="https://fonts.googleapis.com/css?family=Poppins:400,700,900"
// rel="stylesheet"
// />
// <style>
// body {
// font-family: "Poppins";
// font-weight: 700;
// }

// img {
// max-width: 100%;
// }
// </style>
// </head>
// <body>
// Hello world 🙌!

// </body>
// </html>`,
});
res.writeHead(200, { "Content-Type": "image/png" });
res.end(image, "binary");
});

app.get("/", (req, res) => res.send("Hello World!"));

app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);
Loading

0 comments on commit 2f1b11f

Please sign in to comment.