- 🚀 高性能: 基于 Sharp 库,比 ImageMagick 快 4-5 倍
- 📦 开箱即用: 单一 API 端点,无需复杂配置
- 🎨 多种尺寸: 支持 1200x1200 和 500x500 两种输出尺寸
- 🔄 智能转换: 自动转换为 WebP 格式,压缩率高达 99%
- 🐳 Docker 就绪: 完整的 Docker 支持,可一键部署
- 🔒 安全可靠: 内置输入验证、大小限制和安全响应头
- 📊 低内存占用: 流式处理,适合各种服务器配置
- 🌐 支持所有格式: JPEG、PNG、GIF、TIFF、SVG 等
# 克隆仓库
git clone https://github.com/ailingqu/pub-compress-image.git
cd pub-compress-image
# 安装依赖
npm install
# 启动开发服务器
npm run dev
# 访问 http://localhost:3000# 构建镜像
docker build -t image-compression-api .
# 运行容器
docker run -p 3000:3000 image-compression-api
# 测试 API
curl -X POST -H "Content-Type: image/jpeg" \
--data-binary @image.jpg \
http://localhost:3000/api/compress-image \
-o compressed.webp# 使用项目中的测试图片和脚本
chmod +x test-api.sh
./test-api.shPOST /api/compress-image
| 参数 | 类型 | 必填 | 默认值 | 描述 |
|---|---|---|---|---|
size |
string | 否 | 1200 |
输出尺寸:1200 (1200x1200) 或 500 (500x500) |
| Header | 值 | 描述 |
|---|---|---|
Content-Type |
image/* |
图片 MIME 类型(如 image/jpeg, image/png) |
原始图片的二进制数据(最大 100MB)
- Content-Type:
image/webp - Body: 压缩后的 WebP 图片二进制数据
- Headers:
X-Image-Size: 输出尺寸信息(如1200x1200)Content-Length: 图片大小(字节)Cache-Control: 缓存策略
| 状态码 | 描述 |
|---|---|
400 |
无效的内容类型或参数 |
413 |
图片过大(超过 100MB) |
500 |
服务器内部错误 |
# 默认尺寸(1200x1200)
curl -X POST \
-H "Content-Type: image/jpeg" \
--data-binary @image.jpg \
https://your-domain.com/api/compress-image \
-o compressed-1200.webp
# 小尺寸(500x500)
curl -X POST \
-H "Content-Type: image/jpeg" \
--data-binary @image.jpg \
"https://your-domain.com/api/compress-image?size=500" \
-o compressed-500.webp// 使用 Fetch API
async function compressImage(file, size = '1200') {
const response = await fetch(`/api/compress-image?size=${size}`, {
method: 'POST',
headers: {
'Content-Type': file.type
},
body: await file.arrayBuffer()
});
if (!response.ok) {
throw new Error(`Compression failed: ${response.status}`);
}
return await response.blob();
}
// 使用示例
const input = document.querySelector('input[type="file"]');
input.addEventListener('change', async (e) => {
const file = e.target.files[0];
const compressed = await compressImage(file, '500');
// 下载压缩后的图片
const url = URL.createObjectURL(compressed);
const a = document.createElement('a');
a.href = url;
a.download = 'compressed.webp';
a.click();
});import requests
def compress_image(image_path, size='1200'):
"""压缩图片到指定尺寸"""
with open(image_path, 'rb') as f:
response = requests.post(
'https://your-domain.com/api/compress-image',
params={'size': size},
headers={'Content-Type': 'image/jpeg'},
data=f
)
if response.status_code == 200:
with open(f'compressed-{size}.webp', 'wb') as f:
f.write(response.content)
print(f'Compressed to {len(response.content)} bytes')
else:
print(f'Error: {response.json()}')
# 使用示例
compress_image('image.jpg', '1200') # 大尺寸
compress_image('image.jpg', '500') # 小尺寸const fs = require('fs');
const FormData = require('form-data');
const axios = require('axios');
async function compressImage(imagePath, size = '1200') {
const imageBuffer = fs.readFileSync(imagePath);
const response = await axios.post(
`https://your-domain.com/api/compress-image?size=${size}`,
imageBuffer,
{
headers: {
'Content-Type': 'image/jpeg'
},
responseType: 'arraybuffer'
}
);
fs.writeFileSync(`compressed-${size}.webp`, response.data);
console.log(`Saved compressed image (${response.data.length} bytes)`);
}
// 使用示例
compressImage('./image.jpg', '1200');Dokploy 是最简单的部署方式,只需 3 步即可完成!
1️⃣ 推送代码到 Git 仓库
git clone https://github.com/ailingqu/pub-compress-image.git
cd pub-compress-image
# 推送到您的仓库
git remote set-url origin YOUR_GIT_REPO_URL
git push -u origin master2️⃣ 在 Dokploy 创建应用
- 登录 Dokploy 控制台
- 点击 "Create Application"
- 选择 "Docker" 部署类型
- 连接您的 Git 仓库(GitHub/GitLab/Bitbucket)
- Dokploy 会自动检测
Dockerfile并开始构建 - 等待部署完成(约 3-5 分钟)
3️⃣ 配置域名
进入应用的 Domains 标签页:
- 点击 "Add Domain"
- Host: 输入您的域名(如
processimage.yourdomain.com) - Path:
/ - Container Port:
3000⚠️ 重要:必须是 3000 - HTTPS: 如使用 Cloudflare,保持关闭
- 如使用 Cloudflare:勾选 "Behind Cloudflare"
- 点击 "Update"
4️⃣ 配置 DNS 解析
在您的 DNS 服务商(如 Cloudflare):
| 设置项 | 值 |
|---|---|
| 类型 | A |
| 名称 | processimage(或您的子域名前缀) |
| 内容 | 您的 Dokploy 服务器 IP 地址 |
| 代理状态 | 已代理(橙色云朵,推荐) |
| TTL | Auto |
如果使用 Cloudflare,还需设置:
- 进入 SSL/TLS → Overview
- 加密模式选择:Flexible
✅ 完成! 等待 1-2 分钟 DNS 生效,即可访问您的域名。
- Container Port 必须设置为 3000(这是 Dockerfile 中定义的端口)
- 如果服务器上已有其他服务占用 3000 端口也没关系,Dokploy 的 Traefik 会自动处理容器隔离
- 启用 Git Webhook 后,每次推送代码都会自动重新部署
在 Dokploy 应用设置中:
- 找到 Git Webhook 配置
- 复制 Webhook URL
- 在 GitHub/GitLab 仓库设置中添加 Webhook
- 每次
git push后自动重新部署
# 1. 构建并推送镜像
docker build -t yourusername/image-compression-api:latest .
docker push yourusername/image-compression-api:latest
# 2. 在 Dokploy 中配置
# - 选择 "Docker Image" 部署
# - 输入: yourusername/image-compression-api:latest
# - 端口: 3000
# - 点击 "Deploy"# 安装 Vercel CLI
npm i -g vercel
# 部署
vercel --prod注意:Vercel 有文件大小限制(4.5MB),建议用于轻量级场景。
创建 docker-compose.yml:
version: '3.8'
services:
image-api:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- PORT=3000
restart: unless-stopped
mem_limit: 512m
cpus: 1.0运行:
docker-compose up -d创建 deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: image-compression-api
spec:
replicas: 3
selector:
matchLabels:
app: image-api
template:
metadata:
labels:
app: image-api
spec:
containers:
- name: api
image: yourusername/image-compression-api:latest
ports:
- containerPort: 3000
resources:
limits:
memory: "512Mi"
cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
name: image-api-service
spec:
selector:
app: image-api
ports:
- port: 80
targetPort: 3000
type: LoadBalancer部署:
kubectl apply -f deployment.yaml创建 .env.local 文件:
# 端口(默认: 3000)
PORT=3000
# Node 环境
NODE_ENV=production
# 禁用遥测(可选)
NEXT_TELEMETRY_DISABLED=1修改 app/api/compress-image/route.ts 以自定义:
// 修改文件大小限制
const maxSize = 200 * 1024 * 1024; // 改为 200MB
// 修改压缩质量
.webp({ quality: 90 }) // 改为 90% 质量
// 添加新的尺寸
if (sizeParam === '2000') {
size = 2000; // 添加 2000x2000 选项
}项目包含完整的测试脚本 test-api.sh:
chmod +x test-api.sh
./test-api.sh测试内容:
- ✅ 1200x1200 尺寸压缩
- ✅ 500x500 尺寸压缩
- ✅ 默认参数测试
- ✅ 文件完整性验证
# 测试 JPEG
curl -X POST \
-H "Content-Type: image/jpeg" \
--data-binary @test.jpg \
http://localhost:3000/api/compress-image \
-o output.webp
# 验证输出
file output.webp # 应显示: Web/P image# 大尺寸
curl -X POST \
-H "Content-Type: image/png" \
--data-binary @image.png \
"http://localhost:3000/api/compress-image?size=1200" \
-o large.webp
# 小尺寸
curl -X POST \
-H "Content-Type: image/png" \
--data-binary @image.png \
"http://localhost:3000/api/compress-image?size=500" \
-o small.webp
# 对比大小
ls -lh large.webp small.webp# 测试无效尺寸参数
curl -v "http://localhost:3000/api/compress-image?size=999" \
-X POST -H "Content-Type: image/jpeg" \
--data-binary @image.jpg
# 预期: 400 错误
# 测试无效内容类型
curl -v http://localhost:3000/api/compress-image \
-X POST -H "Content-Type: text/plain" \
--data-binary @test.txt
# 预期: 400 错误
# 测试文件过大(需要 >100MB 文件)
curl -v http://localhost:3000/api/compress-image \
-X POST -H "Content-Type: image/jpeg" \
--data-binary @huge-image.jpg
# 预期: 413 错误使用 Apache Bench:
# 安装 Apache Bench (macOS)
brew install httpd
# 并发测试(10 个并发,100 个请求)
ab -n 100 -c 10 -p image.jpg \
-T image/jpeg \
http://localhost:3000/api/compress-image使用 wrk:
# 安装 wrk
brew install wrk
# 基准测试(30 秒,10 个线程,100 个连接)
wrk -t10 -c100 -d30s \
-s post.lua \
http://localhost:3000/api/compress-image基于 Next.js 14 + Sharp 的实测数据:
| 指标 | 值 |
|---|---|
| 处理速度 | 100-500ms/图片 |
| 吞吐量 | ~100 请求/秒(单核) |
| 内存占用 | ~150MB(空闲) |
| Docker 镜像 | ~100MB |
| 压缩比 | 90-99%(取决于原图) |
| 支持格式 | JPEG, PNG, GIF, TIFF, SVG, WebP |
| 原始格式 | 原始大小 | 输出大小 (1200) | 输出大小 (500) | 压缩率 |
|---|---|---|---|---|
| PNG 4096x4096 | 11.0 MB | 113 KB | 28.5 KB | 99.0% |
| JPEG 3000x2000 | 2.5 MB | 85 KB | 22 KB | 96.6% |
| PNG 1920x1080 | 1.2 MB | 48 KB | 15 KB | 96.0% |
pub-compress-image/
├── app/
│ ├── layout.tsx # 根布局组件
│ ├── page.tsx # 首页(API 文档)
│ └── api/
│ └── compress-image/
│ └── route.ts # API 端点核心逻辑
├── public/ # 静态资源
├── tests/
│ ├── test.png # 测试图片
│ ├── test-api.sh # 自动化测试脚本
│ └── test-output*.webp # 测试输出示例
├── .dockerignore # Docker 构建排除文件
├── .env.example # 环境变量模板
├── .gitignore # Git 忽略文件
├── Dockerfile # 多阶段 Docker 构建
├── next.config.js # Next.js 配置
├── package.json # 项目依赖
├── tsconfig.json # TypeScript 配置
└── README.md # 项目文档(本文件)
症状: npm install 时 Sharp 安装错误
解决方案:
# 清除缓存并重新安装
rm -rf node_modules package-lock.json
npm cache clean --force
npm install
# 或使用特定版本
npm install sharp@0.33.0症状: Docker 构建时出现 "COPY failed" 错误
解决方案:
- 确保在项目根目录运行
docker build - 检查
.dockerignore没有排除必要文件 - 使用
docker build --no-cache -t image-api .清除缓存
症状: 部署后访问 API 返回 404
解决方案:
- 确保
app/layout.tsx文件存在 - 检查 Next.js 构建日志是否有错误
- 验证路由文件路径:
app/api/compress-image/route.ts
症状: 处理大图片时请求超时
解决方案:
修改 next.config.js:
const nextConfig = {
// ... 其他配置
experimental: {
// 增加 API 超时时间(默认 60s)
serverActions: {
bodySizeLimit: '100mb',
},
},
}症状: Docker 容器频繁重启或 OOM 错误
解决方案:
# 增加 Docker 容器内存限制
docker run -p 3000:3000 --memory="1g" image-compression-api
# 或在 docker-compose.yml 中
services:
app:
mem_limit: 1g症状: 部署到 Dokploy 后,通过 Cloudflare 访问返回错误
解决方案:
- SSL/TLS 模式设置为 Flexible(后端是 HTTP)
- 如果使用非标准端口,配置 Origin Rules 指定端口
- 或将 Dokploy Port 改为
80(推荐)
# 使用环境变量
NODE_ENV=production PORT=3000
# 启用 PM2 进程管理(Node.js 生产部署)
npm install -g pm2
pm2 start npm --name "image-api" -- start
pm2 startup
pm2 save// 添加 API 认证(在 route.ts 中)
const apiKey = req.headers.get('x-api-key');
if (apiKey !== process.env.API_KEY) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}使用 next-rate-limit:
npm install next-rate-limitimport rateLimit from 'next-rate-limit';
const limiter = rateLimit({
interval: 60 * 1000, // 60 秒
uniqueTokenPerInterval: 500,
});
export async function POST(req: NextRequest) {
try {
await limiter.check(req, 10, 'CACHE_TOKEN'); // 每分钟 10 个请求
} catch {
return NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 });
}
// ... 其余代码
}// 添加请求日志
console.log(`[${new Date().toISOString()}] Processing image: ${buffer.length} bytes, size: ${size}`);
// 使用 Sentry 错误追踪
import * as Sentry from '@sentry/nextjs';
Sentry.captureException(error);欢迎所有形式的贡献!
- Fork 本仓库
- 创建功能分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 开启 Pull Request
# 克隆您的 Fork
git clone https://github.com/YOUR_USERNAME/pub-compress-image.git
# 创建功能分支
git checkout -b feature/new-feature
# 安装依赖
npm install
# 开发(带热重载)
npm run dev
# 运行测试
./test-api.sh
# 提交前检查
npm run build
# 提交
git add .
git commit -m "feat: add new feature"
git push origin feature/new-feature- 使用 TypeScript
- 遵循 ESLint 规则
- 添加适当的注释
- 更新相关文档
本项目采用 MIT 许可证 - 详见 LICENSE 文件
由 ViNano AI 团队开发和维护
- 网站: https://vinano.ai/
- GitHub: @ailingqu
- 🐛 报告问题: GitHub Issues
- 💬 讨论: GitHub Discussions
- 📧 邮件: support@vinano.ai
未来计划的功能:
- 支持更多输出格式(AVIF、JPEG XL)
- 批量图片处理
- 自定义水印添加
- GraphQL API 支持
- Web UI 管理界面
- 图片处理队列(Redis)
- 统计分析仪表板
- CDN 集成
如果这个项目对您有帮助,请给它一个 ⭐️
Made with ❤️ by ViNano AI
