Skip to content

使用 Cloudflare 规则实现的不限请求次数的随机图片 API。

License

Notifications You must be signed in to change notification settings

Keduoli03/Cloudflare-Random-Image

Repository files navigation

Cloudflare Random Image

使用 Cloudflare 规则实现的不限请求次数的随机图片 API。

本项目 Fork 并修改自 Mabbs/cf-hitokoto,核心思路源于原作者。 本项目将其改造为随机图片分发服务,支持自动扫描仓库图片、横竖屏分类,并结合 jsDelivr CDN 加速。

✨ 特性

  • 无限请求: 依托 Cloudflare 边缘计算规则,不消耗服务器资源,理论上无 QPS 限制。
  • 自动维护: 只需将图片上传到 Image/ 目录,GitHub Actions 会自动扫描并生成索引。
  • 智能分类: 自动识别图片宽高比,提供“横屏 (Landscape)”和“竖屏 (Portrait)”分类接口。
  • CDN 加速: 图片资源通过 jsDelivr 全球加速 (可配置)。

🚀 部署方法

1. 准备仓库

Fork 本仓库到你的 GitHub 账号。

2. 上传图片

将你的图片文件上传到 Image/ 文件夹中(支持 .jpg, .png, .webp, .gif 等)。 建议:为了获得最佳体验,请确保文件名不包含特殊字符。

3. 修改配置

修改 gen_advanced.py 文件顶部的配置区域:

# 你的 GitHub 用户名/仓库/分支
REPO_USER = "你的用户名"
REPO_NAME = "仓库名称"
BRANCH = "main"

4. 开启 GitHub Pages

在 GitHub 仓库设置中:Settings -> Pages -> Build and deployment

  • Source: 选择 GitHub Actions

5. 推送代码

提交更改并推送到 GitHub,等待 Actions 构建完成。构建成功后,你可以在 Actions 日志或生成的 rules.txt (部署后的网页路径) 中找到 Cloudflare 规则。

6. 配置 Cloudflare

在 Cloudflare 后台,进入你的域名管理页面:

  1. Rules -> Transform Rules -> Rewrite URL -> Create rule
  2. Rule name: Random Image
  3. When incoming requests match: Hostname equals 你的域名 (或者根据需要设置)
  4. Path Rewrite -> Rewrite to ... -> Dynamic
  5. 填入下方生成的规则表达式。

Cloudflare 规则表达式

请访问 https://你的域名/rules.txt 获取最新生成的规则,它会根据你的图片数量自动调整。

通用格式参考:

  • Random Rule (全随机):

    concat("/all/", substring(uuidv4(cf.random_seed), 0, 2), ".json")

    (注意:2 是根据图片数量自动计算的 Hex 长度)

  • Category Rule (带分类):

    concat("/categories/", substring(http.request.uri.query, 2, 1), "/", substring(uuidv4(cf.random_seed), 0, 2), ".json")

🔌 API 用法

请求地址:https://你的域名/

参数说明

参数 说明 示例
c l 获取横屏图片 (Landscape) ?c=l
c p 获取竖屏图片 (Portrait) ?c=p
(无) (无) 获取全库随机图片 (不带参数)

返回示例 (JSON)

{
  "url": "https://gcore.jsdelivr.net/gh/Keduoli03/Cloudflare-Random-Image@main/image/乌贼.jpg",
  "type": "landscape"
}
  • url: 图片的直链地址 (已拼接 CDN)。
  • type: 图片类型,landscape (横屏) 或 portrait (竖屏)。

About

使用 Cloudflare 规则实现的不限请求次数的随机图片 API。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •