Skip to content

Instantly share code, notes, and snippets.

@zhepa zhepa/previews.js
Created Aug 8, 2018

Embed
What would you like to do?
превьюхи
const Sharp = require('sharp');
const Vibrant = require('node-vibrant');
const fs = require('fs');
const inputImage = process.argv[2];
const blur = process.argv[3] ? process.argv[3] : 20; // 20
const size = 380; // 412
const source = inputImage || 'DM9VEJgW4AUmLX.png';
Promise.all([
new Promise((resolve, reject) => {
Vibrant.from(source).getPalette((error, palette) => {
if (error) return reject(error);
resolve(palette);
})
}),
Sharp(source).resize(512, 512),
Sharp(source).resize(size, size).toBuffer(),
])
.then(([ imagePalette, background, image ]) => {
const paletteNames = {
'Muted': 'muted',
'Vibrant': 'vibrant',
'LightVibrant': 'light-vibrant',
'DarkVibrant': 'dark-vibrant',
'LightMuted': 'light-muted',
'DarkMuted': 'dark-muted'
};
for (const paletteType in paletteNames) {
if (imagePalette[paletteType]) {
createCover(imagePalette[paletteType]._rgb[0], imagePalette[paletteType]._rgb[1], imagePalette[paletteType]._rgb[2], '!' + paletteNames[paletteType]);
}
}
function createCover(r,g,b,name) {
background
.overlayWith({
create: {
width: 512,
height: 512,
channels: 4,
background: { r, g, b, alpha: 0.5 }
}
})
.blur(blur)
.toBuffer()
.then(buffer => {
return Sharp(buffer)
.overlayWith(image)
.toFile(name + '.png');
});
background
.tint({ r, g, b, alpha: 1 })
.blur(blur)
.toBuffer()
.then(background => {
return Sharp(background)
.overlayWith(image)
.toFile(name + '[alt]' + '.png');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.