Skip to content

Instantly share code, notes, and snippets.

@codigoconjuan
Last active November 11, 2024 02:45
Show Gist options
  • Save codigoconjuan/36e5d749d12b3eab50df5d071520daf2 to your computer and use it in GitHub Desktop.
Save codigoconjuan/36e5d749d12b3eab50df5d071520daf2 to your computer and use it in GitHub Desktop.
Convertir Imagenes a Webp
export async function imagenes(done) {
const srcDir = './src/img';
const buildDir = './build/img';
const images = await glob('./src/img/**/*{jpg,png}')
images.forEach(file => {
const relativePath = path.relative(srcDir, path.dirname(file));
const outputSubDir = path.join(buildDir, relativePath);
procesarImagenes(file, outputSubDir);
});
done();
}
function procesarImagenes(file, outputSubDir) {
if (!fs.existsSync(outputSubDir)) {
fs.mkdirSync(outputSubDir, { recursive: true })
}
const baseName = path.basename(file, path.extname(file))
const extName = path.extname(file)
const outputFile = path.join(outputSubDir, `${baseName}${extName}`)
const outputFileWebp = path.join(outputSubDir, `${baseName}.webp`)
const options = { quality: 80 }
sharp(file).jpeg(options).toFile(outputFile)
sharp(file).webp(options).toFile(outputFileWebp)
}
@thony1010
Copy link

thony1010 commented Nov 11, 2024

Excelente aporte, muy buenos sus cursos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment