Fixed optimzied images
This commit is contained in:
parent
bf6570cad7
commit
3600f46d03
1 changed files with 21 additions and 0 deletions
21
src/templates/components/Optimizer/OptimizeImage.tsx
Normal file
21
src/templates/components/Optimizer/OptimizeImage.tsx
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import sharp from 'sharp'
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
src: string
|
||||||
|
size?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const convertImage = async (props: Props) => {
|
||||||
|
const avifOutputPath = `src/templates/images/${props.src.split('.').slice(0, -1).join('.')}.avif`
|
||||||
|
const webpOutputPath = `src/templates/images/${props.src.split('.').slice(0, -1).join('.')}.webp`
|
||||||
|
|
||||||
|
const avifBuffer = await sharp(`src/assets/images/${props.src}`).avif({ quality: 60 }).resize(props.size).toBuffer()
|
||||||
|
await sharp(avifBuffer).toFile(avifOutputPath)
|
||||||
|
|
||||||
|
const webpBuffer = await sharp(`src/assets/images/${props.src}`).webp({ quality: 75 }).resize(props.size).toBuffer()
|
||||||
|
await sharp(webpBuffer).toFile(webpOutputPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default (props: Props) => {
|
||||||
|
convertImage(props)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue