Skip to content

Instantly share code, notes, and snippets.

@queses
Created May 13, 2020 08:23
Show Gist options
  • Save queses/c89dc00ae99941b20278969d6aea411c to your computer and use it in GitHub Desktop.
Save queses/c89dc00ae99941b20278969d6aea411c to your computer and use it in GitHub Desktop.
PlusAgent Remarketing Image With Agent
const htmlToImage = require('node-html-to-image')
const sharp = require('sharp')
const axios = require('axios')
const path = require('path')
const fs = require('fs')
const { promisify } = require('util')
const readFile = promisify(fs.readFile)
async function main() {
const [agentPhotoRes, objectPhotoRes] = await Promise.all([
getImage('https://intrum11-55.intrumnet.com/images/avatars/d0e2c1bb.jpg?1558013449'),
getImage('https://intrum11-55.intrumnet.com/files/crm/product/resized800x600/f6/84/5ea035720c3f7.jpg')
])
return htmlToImage({
output: __dirname + '/data/image.png',
html: htmlTemplate,
content: {
agentPhoto: 'data:image/jpeg;base64,' + agentPhotoRes.toString('base64'),
objectPhoto: 'data:image/jpeg;base64,' + objectPhotoRes.toString('base64')
}
})
}
function getImage(url) {
return axios.get(url, { responseType: 'stream' }).then(
(res) =>
new Promise((resolve, reject) => {
const buffers = []
res.data
.on('data', (c) => buffers.push(c))
.on('end', () => resolve(Buffer.concat(buffers)))
.on('error', reject)
})
)
}
function printHrtimeDiff(t0) {
const diff = process.hrtime(t0)
console.log(`Action took ${diff[0] * 1e3 + Math.round(diff[1] / 1e6)} ms`)
}
const htmlTemplate = `
<html>
<style>
body {
margin: 0;
width: 600px;
height: 600px;
position: relative;
}
#object {
width: 600px;
height: 600px;
}
#agent {
width: 120px;
height: 120px;
position: absolute;
bottom: 30px;
left: 30px;
border-radius: 50%;
box-shadow: 3px 3px 10px 0px rgba(0, 0, 0, 0.5);
object-fit: cover;
}
</style>
<body>
<img id="agent" src="{{agentPhoto}}" />
<img id="object" src="{{objectPhoto}}" />
</body>
</html>
`
const t0 = process.hrtime()
main().then(() => printHrtimeDiff(t0))
{
"name": "sharp-remarketing-image",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"axios": "^0.19.2",
"node-html-to-image": "^2.0.0",
"sharp": "^0.25.2"
},
"devDependencies": {
"prettier": "^2.0.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment