Skip to content

Instantly share code, notes, and snippets.

@codigoconjuan
Created April 21, 2024 18:52
Show Gist options
  • Save codigoconjuan/e06988ac69d74825ef9258536f3170d0 to your computer and use it in GitHub Desktop.
Save codigoconjuan/e06988ac69d74825ef9258536f3170d0 to your computer and use it in GitHub Desktop.
Crop Imagenes
export async function crop(done) {
const inputFolder = 'src/img/gallery/full'
const outputFolder = 'src/img/gallery/thumb';
const width = 250;
const height = 180;
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, { recursive: true })
}
const images = fs.readdirSync(inputFolder).filter(file => {
return /\.(jpg)$/i.test(path.extname(file));
});
try {
images.forEach(file => {
const inputFile = path.join(inputFolder, file)
const outputFile = path.join(outputFolder, file)
sharp(inputFile)
.resize(width, height, {
position: 'centre'
})
.toFile(outputFile)
});
done()
} catch (error) {
console.log(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment