Last active
September 26, 2023 08:19
-
-
Save sangelxyz/6465527ddf613e6ddae9b63cb959dbb8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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