下载地址
说明:https://developers.google.com/speed/webp/docs/precompiled
下载链接:https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html
cwebp
(compress WebP)将 JPEG、PNG 或 TIFF 格式的图片编码成 WebP 格式,而 dwebp
(decompress WebP)将其解码为 PNG 格式。gif2webp
可以将 gif 转换为 webp 格式
使用
使用说明:https://developers.google.com/speed/webp/docs/cwebp#description
当你需要把图片转成 WebP
格式的时候,可以通过 -q
指定压缩的质量等级,它的范围是从 0 ~ 100,0 表示质量最差,100 是最好。默认是 75
快捷
为了方便使用将下载文件的 bin
目录添加到环境变量的 Path
变量里
结合之前的 为 Windows 下的 cmd 设置别名(alias)
新增加一条(我在桌面新建了一个 img 的文件夹,将需要转换的图片放到里面,转换好的图片输出到桌面)
第一个参数是 js 文件路径,第二个是需要压缩图片的路径,第三个是图片输出路径,第四个是转换完后是否删除原图(不需要删除的话最后一个参数去掉即可,运行需要配置好 Node.js 运行环境
1
| @doskey img=node D:\Green\webp\main.js D:\ShiGuang\Desktop\img D:\ShiGuang\Desktop -y
|
main.js
文件我放到了 D:\Green\webp
目录下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| 'use strict' const path = require('path') const fs = require('fs') const child_process = require('child_process')
const compressPath = process.argv[2] const outputPath = process.argv[3] let isDel = process.argv[4]
if (!compressPath) { console.warn('compressPath not exist=', compressPath) return }
let minPathList = [] let minPathFileList = [] let fileNameList = [] let fileSuffixList = []
let minArr = getAllDirectory(compressPath)
minPathList = minArr[0] minPathFileList = minArr[1] fileNameList = minArr[2] fileSuffixList = minArr[3]
for (let i = 0; i < minPathFileList.length; i++) { let cmdPath = ''
if (fileSuffixList[i] == 'gif') { cmdPath = 'gif2webp ' + minPathFileList[i] + ' -o ' + outputPath + '\\' + fileNameList[i] + '.webp' } else if ( fileSuffixList[i] == 'jpg' || fileSuffixList[i] == 'png' || fileSuffixList[i] == 'jpeg' ) { cmdPath = 'cwebp ' + minPathFileList[i] + ' -o ' + outputPath + '\\' + fileNameList[i] + '.webp' } else { console.warn('暂时不支持文件类型', fileSuffixList[i]) }
child_process.exec(cmdPath, (error, stdout, stderr) => { if (error) { console.log('转换失败', error) return } console.log('转换成功....', cmdPath) if (isDel == '-y') { fs.unlinkSync(minPathFileList[i]) console.log('删除成功') } }) }
function getAllDirectory(searthPath) { searchDirectory(searthPath) return [minPathList, minPathFileList, fileNameList, fileSuffixList] }
function searchDirectory(searchPath) { let files = fs.readdirSync(searchPath) for (let index = 0; index < files.length; index++) { const filename = files[index] let filedir = path.join(searchPath, filename) let stats = fs.statSync(filedir) if (stats.isDirectory()) { searchDirectory(filedir) } else { let filenamearr = filename.split('.') let suffix = filenamearr[filenamearr.length - 1].toLowerCase() if ( suffix == 'jpg' || suffix == 'jpeg' || suffix == 'png' || suffix == 'gif' ) { minPathList.push(searchPath) minPathFileList.push(filedir) fileNameList.push(filenamearr[0]) fileSuffixList.push(filenamearr[filenamearr.length - 1].toLowerCase()) } } } }
|