文字图片

TEXT · TO · IMAGE · API
参数配置
📁 点击选择图片(PNG / JPG / WebP / GIF)
预览
🖼
点击「生成图片」
查看预览效果
API 文档
# GET 请求(简单)
curl "https://your-worker.workers.dev/api/generate?text=Hello+World&width=600&height=300&fontSize=48&bgColor=%23000000&textColor=%23ffffff"   --output image.svg

# POST 请求(完整参数)
curl -X POST "https://your-worker.workers.dev/api/generate"   -H "Content-Type: application/json"   -d '{
    "text": "欢迎使用
Text to Image API",
    "width": 800,
    "height": 400,
    "fontSize": 42,
    "fontWeight": 700,
    "bgColor": "#1a1208",
    "textColor": "#f5f0e8",
    "textAlign": "center",
    "padding": 60,
    "lineHeight": 1.6,
    "gradient": "linear-gradient(135deg, #667eea, #764ba2)"
  }'   --output image.svg
// GET 请求
const params = new URLSearchParams({
  text: 'Hello World',
  width: 600,
  height: 300,
  fontSize: 48,
  bgColor: '#000000',
  textColor: '#ffffff',
});
const res = await fetch(`/api/generate?${params}`);
const svgText = await res.text(); // SVG string
// Or use as image src:
const blob = await res.blob();
img.src = URL.createObjectURL(blob); // works in  tags

// POST 请求
const res = await fetch('/api/generate', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    text: '欢迎使用
Text to Image API',
    width: 800,
    height: 400,
    fontSize: 42,
    fontWeight: 700,
    bgColor: '#1a1208',
    textColor: '#f5f0e8',
    textAlign: 'center',
    padding: 60,
    gradient: 'linear-gradient(135deg, #667eea, #764ba2)',
  })
});
const svgText = await res.text();
import requests

# GET 请求
res = requests.get('https://your-worker.workers.dev/api/generate', params={
    'text': 'Hello World',
    'width': 600,
    'height': 300,
    'fontSize': 48,
    'bgColor': '#000000',
    'textColor': '#ffffff',
})
with open('image.svg', 'w') as f:
    f.write(res.text)

# POST 请求
res = requests.post('https://your-worker.workers.dev/api/generate', json={
    'text': '欢迎使用
Text to Image API',
    'width': 800,
    'height': 400,
    'fontSize': 42,
    'fontWeight': 700,
    'bgColor': '#1a1208',
    'textColor': '#f5f0e8',
    'padding': 60,
    'gradient': 'linear-gradient(135deg, #667eea, #764ba2)',
})
with open('image.svg', 'w') as f:
    f.write(res.text)

# 如需 PNG,可用 cairosvg 转换:
# pip install cairosvg
# import cairosvg; cairosvg.svg2png(url='image.svg', write_to='image.png')
# format=base64 → 返回 JSON,包含 base64 字符串
curl -X POST "https://your-worker.workers.dev/api/generate"   -H "Content-Type: application/json"   -d '{"text":"Hello","width":600,"height":300,"format":"base64"}'

# 响应示例:
{
  "data": "PHN2ZyB4bWxucz0iaHR0cDov...",   ← 纯 base64
  "dataUri": "data:image/svg+xml;base64,PHN2...", ← 直接用于 <img src>
  "mimeType": "image/svg+xml",
  "width": 600,
  "height": 300,
  "size": 1024   ← 原始字节数
}

# format=datauri → 直接返回 data URI 字符串(Content-Type: text/plain)
curl -X POST "https://your-worker.workers.dev/api/generate"   -H "Content-Type: application/json"   -d '{"text":"Hello","format":"datauri"}'
# 响应: data:image/svg+xml;base64,PHN2ZyB4bWxucz0i...

// JavaScript 用法:
const res = await fetch('/api/generate', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ text: 'Hello', format: 'base64' })
});
const { dataUri, data, size } = await res.json();
document.querySelector('img').src = dataUri; // 直接显示
// 或存储 data 字段用于数据库/传输
text           string   必填 - 要转换的文字,支持 \n 换行
width          number   图片宽度 px(默认 800,最大 3000)
height         number   图片高度 px(默认 400,最大 3000)
fontSize       number   字体大小 px(默认 42)
fontWeight     number   字重 300/400/700/900(默认 400)
lineHeight     number   行高倍数(默认 1.6)
textAlign      string   对齐 left/center/right(默认 center)
bgColor        string   背景色 #rrggbb(默认 #ffffff)
textColor      string   文字色 #rrggbb(默认 #000000)
padding        number   内边距 px(默认 40)
borderRadius   number   圆角 px(默认 0)
gradient       string   CSS 渐变,覆盖 bgColor
                        例: linear-gradient(135deg, #667eea, #764ba2)
bgImage        string   背景图片:https:// URL 或 base64 data URI
                        例: https://example.com/photo.jpg
                        例: data:image/png;base64,iVBORw0KGgo...
bgImageOpacity number   背景图片透明度 0~1(默认 1.0)
bgImageFit     string   填充方式 cover/contain/fill/none(默认 cover)
format         string   返回格式(默认 svg):
                          svg     → Content-Type: image/svg+xml
                          base64  → JSON { data, dataUri, mimeType, width, height, size }
                          datauri → 纯文本 "data:image/svg+xml;base64,..."

响应: format=svg     → image/svg+xml 二进制流
      format=base64  → application/json
      format=datauri → text/plain (直接放入 <img src>)
错误: application/json  { "error": "..." }