# webp_server_go **Repository Path**: adtk/webp_server_go ## Basic Information - **Project Name**: webp_server_go - **Description**: 基于 golang 语言的动态将图片缩小 和 转 webp 的服务 - **Primary Language**: Go - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2022-03-10 - **Last Updated**: 2023-03-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: webp, img, Go语言 ## README 基于 golang 语言的动态将图片缩小 和 转 webp 的服务 默认转换`jpg,jpeg,png,gif`类型文件为 webp,可以通过`config.json`参数指定转换的文件类型 - JPEG, PNG, BMP, GIF 格式的图片可转为 webp 格式 - JPEG, PNG, BMP 格式支持缩放,gif 缩放太慢所以不支持 - 对于不支持 webp 格式的浏览器,发送原图格式 ## 使用步骤 ### 下载并构建二进制文件 [https://gitee.com/adtk/webp_server_go](https://gitee.com/adtk/webp_server_go) ### 构建 ``` go build . ``` ### 运行 ```sh ./webp_server_go # or ./webp_server_go --config=config.json ``` ```json // config.json { "HOST": "127.0.0.1", "PORT": "3333", "QUALITY": "80", //压缩质量 0-100 默认80 "INPUT": { "app.adtk.cn": "/www/wwwroot/app.adtk.cn/img", "pics": "./pics" }, // 图片存放目录,支持多路径 "EXHAUST_PATH": "/path/to/exhaust", // 图片缩小转换后存放目录 "ALLOWED_TYPES": ["jpg", "png", "jpeg", "bmp", "gif"] } ``` ``` 将提供以下服务路径: http://127.0.0.1:3333/app.adtk.cn http://127.0.0.1:3333/pics ``` ```nginx # nginx 将/img路径转发到webp_server_go服务 # nginx 配置文件 location /img { proxy_pass http://127.0.0.1:3333/app.adtk.cn; proxy_set_header Host $host:$server_port; } ``` ```sh # http://yourdomain/img/png.jpg?m=lfit&w=200&h=200&q=100 # 转发为 # http://127.0.0.1:3333/app.adtk.cn/png.jpg?m=lfit&w=200&h=200&q=100&f=webp # 其中app.adtk.cn是config.json中INPUT的key # 其中/png.jpg是/www/wwwroot/app.adtk.cn/img的文件夹下的png.jpg ``` ```go // 地址参数 ?m=lfit&w=200&h=200&q=100 type Resize struct { Mode string `query:"m" json:"m"` // mode: lfit等比缩放,original或留空返回原图(不压缩不转换) Format string `query:"f" json:"f"` // format: "webp","", 要转换的格式仅支持webp,留空不转换 Width uint `query:"w" json:"w"` // width 最大宽度,图片宽度超过这个值,则不变,默认不变 Height uint `query:"h" json:"h"` // height 最大高度,图片高度超过这个值,则不变,默认不变 Quality uint `query:"q" json:"q"` // quality 图片质量0-100,默认80 } ``` ## 更多文档请参考原作者文档 [https://docs.webp.sh/](https://docs.webp.sh/) ## License WebP Server is under the GPLv3. See the [LICENSE](./LICENSE) file for details.