-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2f1b11f
Showing
8 changed files
with
1,624 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"ext": "html" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
); |
Oops, something went wrong.