Last active
November 11, 2024 02:45
-
-
Save codigoconjuan/36e5d749d12b3eab50df5d071520daf2 to your computer and use it in GitHub Desktop.
Convertir Imagenes a Webp
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 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excelente aporte, muy buenos sus cursos