2024-11-05|今天我做了什么

图片转换

使用 sip 将 JPG 图片转为 PNG

macos 上先安装 sip:

brew install sip

然后批量执行:

for file in *.jpg; do
    sips -s format png "$file" --out "${file%.jpg}.png"
done

for file in *.jpeg; do
    sips -s format png "$file" --out "${file%.jpeg}.png"
done

for file in *.JPG; do
    sips -s format png "$file" --out "${file%.JPG}.png"
done

rm -rf *.jpeg *.jpg *.JPG

使用 imagemin 将 jpg、png 转换为 webp

参考 optimize-images.mjs

import imagemin from "imagemin";
import imageminWebp from "imagemin-webp";
import imageminPngquant from "imagemin-pngquant";

(async () => {
    let fullImages = await convertFullImages();
    let previewImages = await convertPreviewImages();
    let total = fullImages.length + previewImages.length;
    console.log('Converted ' + total + ' images to WEBP format.');
})();

function convertFullImages(){
    return imagemin(['static/raw-images/*.{jpg,jpeg,JPG,png}'], {
        destination: 'static/images',
        plugins: [
            imageminWebp({quality: 85})
        ]
    });
}

function convertPreviewImages(){
    return imagemin(['static/raw-images/*.{jpg,jpeg,png}'], {
        destination: 'static/preview-images',
        plugins: [
            imageminWebp({
                resize: {
                    width: 600,
                    height: 340
                }
            })
        ]
    });
}
Share this post:

Related content