logo
0
0
WeChat Login

Shot - 网页首屏截图服务

基于 Puppeteer 的网页截图服务,支持高并发、文件缓存。

功能特性

  • ✅ 支持 HTTP/HTTPS 协议网页截图
  • ✅ 浏览器资源池管理(最大 5 并发)
  • ✅ 文件缓存(1 小时 TTL)
  • ✅ JPG 格式输出
  • ✅ 自定义截图尺寸
  • ✅ 健康检查和服务状态接口
  • ✅ Docker 容器化部署

Docker 部署

docker run -d \
  -p 8080:8080 \
  --name shot \
  docker.cnb.cool/shellingford/shot:latest

访问 http://localhost:8080/screenshot?url=https://www.example.com 即可获取截图。

自定义配置

通过环境变量或挂载配置文件自定义配置:

docker run -d \
  -p 8080:8080 \
  -v ./config.js:/app/src/config.js \
  --name shot \
  docker.cnb.cool/shellingford/shot:latest

自行构建镜像

docker build -t shot .

快速开始

安装依赖

npm install

启动服务

npm start

服务默认运行在 http://localhost:8080

API 接口

获取截图

GET /screenshot?url=<目标 URL>&width=<宽度>&height=<高度>

参数说明:

参数必填默认值说明
url-目标网页 URL
width1280视口宽度
height720视口高度

请求示例:

# 默认尺寸
curl -o screenshot.jpg "http://localhost:8080/screenshot?url=https://www.example.com"

# 自定义尺寸
curl -o screenshot.jpg "http://localhost:8080/screenshot?url=https://www.example.com&width=1920&height=1080"

响应头:

  • X-Screenshot-From-Cache: true/false 表示是否命中缓存

健康检查

GET /health

服务状态

GET /status

返回浏览器资源池和缓存统计信息。

缓存统计

GET /cache/stats

清理缓存

POST /cache/cleanup

配置

编辑 src/config.js 修改配置:

module.exports = {
  port: 8080,              // 服务端口
  screenshot: {
    format: 'jpeg',        // 图片格式
    quality: 80,           // JPG 质量 (1-100)
    width: 1280,           // 默认宽度
    height: 720,           // 默认高度
    timeout: 15000,        // 导航超时时间 (ms)
    waitUntil: 'load',     // 等待事件:'load' 或 'domcontentloaded'
    extraWait: 3000,       // 额外等待时间 (ms)
    fontWaitTimeout: 3000, // 字体加载最大等待时间 (ms)
  },
  cache: {
    ttl: 3600000,          // 缓存时长 (ms)
    dir: './cache',        // 缓存目录
  },
  pool: {
    maxPages: 5,           // 最大并发数
    minPages: 1,           // 最小页面数
  }
}

项目结构

shot/
├── src/
│   ├── config.js           # 配置文件
│   ├── browser-pool.js     # 浏览器资源池
│   ├── cache-manager.js    # 缓存管理
│   ├── screenshot-service.js # 截图服务
│   ├── server.js           # HTTP 服务
│   └── index.js            # 入口文件
├── cache/                  # 缓存目录(自动生成)
├── package.json
└── README.md

注意事项

  1. 首次启动会自动下载 Chromium(约 170MB)
  2. 缓存目录会自动创建,无需手动创建
  3. 服务会自动定期清理过期缓存
  4. 按 Ctrl+C 可优雅关闭服务
  5. Docker 镜像已内置 Chromium 和字体,无需额外配置

About

网站截图服务

shotrepo-named
Language
JavaScript100%