Created
April 21, 2024 18:52
-
-
Save codigoconjuan/e06988ac69d74825ef9258536f3170d0 to your computer and use it in GitHub Desktop.
Crop Imagenes
This file contains 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
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