Skip to content

Instantly share code, notes, and snippets.

@sangelxyz
Last active September 26, 2023 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sangelxyz/6465527ddf613e6ddae9b63cb959dbb8 to your computer and use it in GitHub Desktop.
Save sangelxyz/6465527ddf613e6ddae9b63cb959dbb8 to your computer and use it in GitHub Desktop.
import http from 'https';
import sharp from 'sharp';
import fs from 'fs';
const
remoteImageUrl = 'https://s2.coinmarketcap.com/static/img/coins/64x64/1.png',
outputPath = 'resized.jpg',
urlParts = new URL(remoteImageUrl),
options = {
hostname: urlParts.hostname,
path: urlParts.pathname,
port: 443,
method: 'GET',
};
const request = http.request(options, (response) => {
if (response.statusCode !== 200) {
console.error('Error:', response.statusCode);
return;
}
// -- write stream
const writeStream = fs.createWriteStream(outputPath);
// -- Pipe to sharp
response
.pipe(sharp().resize(300, 300))
.pipe(writeStream)
.on('finish', () => {
console.log('downloaded.');
})
.on('error', (err) => {
console.error('Error', err);
});
});
request.end();
request.on('error', (err) => {
console.error('Error:', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment