352 lines
18 KiB
HTML
352 lines
18 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<title>封面 AI 助手 · 生图 / 修图</title>
|
||
<style>
|
||
:root{--bg:#0b0f17;--panel:#121a26;--line:#223047;--txt:#e6edf7;--mut:#8fa3bd;--acc:#38bdf8;--acc2:#a78bfa;--ok:#34d399;--warn:#fbbf24;--err:#f87171}
|
||
*{box-sizing:border-box}
|
||
body{margin:0;background:var(--bg);color:var(--txt);font:15px/1.6 -apple-system,"Noto Sans SC","Microsoft YaHei",system-ui,sans-serif}
|
||
header{padding:18px 22px;border-bottom:1px solid var(--line);display:flex;align-items:center;gap:14px;flex-wrap:wrap;background:linear-gradient(90deg,#0b0f17,#101a2b)}
|
||
h1{font-size:18px;margin:0;font-weight:700}
|
||
.tag{font-size:12px;color:var(--mut);border:1px solid var(--line);border-radius:999px;padding:3px 10px}
|
||
.wrap{max-width:1240px;margin:0 auto;padding:20px}
|
||
.tabs{display:flex;gap:8px;margin-bottom:18px;flex-wrap:wrap}
|
||
.tabs button{background:var(--panel);color:var(--mut);border:1px solid var(--line);border-radius:10px;padding:9px 16px;cursor:pointer;font-size:14px}
|
||
.tabs button.on{color:#08131f;background:linear-gradient(90deg,var(--acc),var(--acc2));border-color:transparent;font-weight:700}
|
||
.grid{display:grid;grid-template-columns:1.05fr 1fr;gap:18px}
|
||
@media(max-width:900px){.grid{grid-template-columns:1fr}}
|
||
.card{background:var(--panel);border:1px solid var(--line);border-radius:14px;padding:16px}
|
||
label{display:block;font-size:13px;color:var(--mut);margin:12px 0 6px}
|
||
textarea,select,input[type=text]{width:100%;background:#0e1622;color:var(--txt);border:1px solid var(--line);border-radius:10px;padding:11px 12px;font:inherit}
|
||
textarea{min-height:96px;resize:vertical}
|
||
.chips{display:flex;flex-wrap:wrap;gap:7px;margin-top:7px}
|
||
.chips span{font-size:12.5px;color:var(--mut);border:1px dashed var(--line);border-radius:999px;padding:4px 11px;cursor:pointer}
|
||
.chips span:hover{color:var(--acc);border-color:var(--acc)}
|
||
.btn{display:inline-flex;align-items:center;gap:8px;background:linear-gradient(90deg,var(--acc),var(--acc2));color:#08131f;font-weight:800;border:0;border-radius:10px;padding:12px 22px;cursor:pointer;font-size:15px}
|
||
.btn:disabled{opacity:.55;cursor:not-allowed}
|
||
.btn.ghost{background:transparent;color:var(--txt);border:1px solid var(--line);font-weight:600}
|
||
.row{display:flex;gap:10px;flex-wrap:wrap;align-items:center}
|
||
.hint{font-size:12.5px;color:var(--mut)}
|
||
.status{margin-top:12px;font-size:13.5px;min-height:22px}
|
||
.status.err{color:var(--err)}.status.ok{color:var(--ok)}.status.warn{color:var(--warn)}
|
||
.results{display:grid;grid-template-columns:repeat(auto-fill,minmax(220px,1fr));gap:12px;margin-top:14px}
|
||
.shot{background:#0e1622;border:1px solid var(--line);border-radius:12px;overflow:hidden;display:flex;flex-direction:column}
|
||
.shot img{width:100%;display:block;background:#0a0f18}
|
||
.shot .bar{display:flex;gap:6px;padding:8px;flex-wrap:wrap}
|
||
.shot .bar a,.shot .bar button{font-size:12px;color:var(--mut);background:#0b1220;border:1px solid var(--line);border-radius:8px;padding:5px 9px;cursor:pointer;text-decoration:none}
|
||
.shot .bar a:hover,.shot .bar button:hover{color:var(--acc);border-color:var(--acc)}
|
||
.drop{border:2px dashed var(--line);border-radius:12px;padding:26px;text-align:center;color:var(--mut);cursor:pointer}
|
||
.drop.on{border-color:var(--acc);color:var(--acc)}
|
||
.cmp{display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-top:12px}
|
||
.cmp figure{margin:0}
|
||
.cmp figcaption{font-size:12px;color:var(--mut);margin-bottom:5px}
|
||
.cmp img{width:100%;border-radius:10px;border:1px solid var(--line)}
|
||
.badge{font-size:11.5px;border-radius:6px;padding:2px 7px;margin-left:6px}
|
||
.badge.ok{background:rgba(52,211,153,.15);color:var(--ok)}
|
||
.badge.no{background:rgba(248,113,113,.13);color:var(--err)}
|
||
.mini{font-size:12.5px;color:var(--mut);background:#0b1220;border:1px solid var(--line);border-radius:8px;padding:6px 11px;cursor:pointer;text-decoration:none}
|
||
.mini:hover{color:var(--acc);border-color:var(--acc)}
|
||
.loading{display:inline-block;width:14px;height:14px;border:2px solid #33465f;border-top-color:var(--acc);border-radius:50%;animation:sp .8s linear infinite}
|
||
@keyframes sp{to{transform:rotate(360deg)}}
|
||
footer{padding:16px 22px;color:var(--mut);font-size:12.5px;border-top:1px solid var(--line);margin-top:26px}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<header>
|
||
<h1>封面 AI 助手</h1>
|
||
<span class="tag">omniroute · cheaperinference</span>
|
||
<span class="tag" id="modelTag">Grok Imagine</span>
|
||
<span class="tag">生成的图可直接在「快图设计」的素材面板里点用</span>
|
||
</header>
|
||
|
||
<div class="wrap">
|
||
<div class="tabs">
|
||
<button class="on" data-tab="gen">AI 生图</button>
|
||
<button data-tab="edit">AI 修图</button>
|
||
<button data-tab="lib">素材库</button>
|
||
</div>
|
||
|
||
<!-- ============ 生图 ============ -->
|
||
<section id="tab-gen">
|
||
<div class="grid">
|
||
<div class="card">
|
||
<label>画面描述(中文或英文都行)</label>
|
||
<textarea id="prompt" placeholder="例:扁平风科技插画,深蓝渐变背景,发光的笔记本电脑和悬浮 App 图标,左侧留出标题空间,不要文字"></textarea>
|
||
<div class="chips" id="styleChips"></div>
|
||
|
||
<div class="row" style="margin-top:14px">
|
||
<div style="flex:1;min-width:210px">
|
||
<label style="margin-top:0">出图比例 / 尺寸</label>
|
||
<select id="size"></select>
|
||
</div>
|
||
<div style="flex:1;min-width:210px">
|
||
<label style="margin-top:0">模型</label>
|
||
<select id="model"></select>
|
||
</div>
|
||
</div>
|
||
<div class="row" style="margin-top:16px">
|
||
<button class="btn" id="genBtn">生成图片</button>
|
||
<span class="hint" id="genTime"></span>
|
||
</div>
|
||
<div class="status" id="genStatus"></div>
|
||
</div>
|
||
<div class="card">
|
||
<label style="margin-top:0">结果</label>
|
||
<div class="hint" id="genEmpty">生成后会显示在这里,可下载或按平台尺寸裁剪。</div>
|
||
<div class="results" id="genResults"></div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
<!-- ============ 修图 ============ -->
|
||
<section id="tab-edit" style="display:none">
|
||
<div class="grid">
|
||
<div class="card">
|
||
<label style="margin-top:0">上传要改的图(封面、截图、照片都行)</label>
|
||
<div class="drop" id="drop">点击选择图片,或把图片拖到这里 / 直接 Ctrl+V 粘贴</div>
|
||
<input type="file" id="file" accept="image/*" hidden />
|
||
|
||
<label>修改要求</label>
|
||
<textarea id="editPrompt" placeholder="例:把背景换成浅灰渐变,去掉所有文字,主体换成扁平插画风格"></textarea>
|
||
<div class="chips" id="editChips"></div>
|
||
|
||
<label>修图方式</label>
|
||
<div class="row" style="gap:16px">
|
||
<label class="row" style="margin:0;color:var(--txt);font-size:13.5px">
|
||
<input type="radio" name="mode" value="redraw" checked> AI 重绘(看图后按你的要求重画一版,现在可用)
|
||
</label>
|
||
<label class="row" style="margin:0;color:var(--txt);font-size:13.5px">
|
||
<input type="radio" name="mode" value="edit"> 精细修图(保持原图像素,nano-banana 需余额)
|
||
</label>
|
||
</div>
|
||
<div class="row" style="margin-top:14px">
|
||
<div style="flex:1;min-width:200px">
|
||
<label style="margin-top:0">输出尺寸</label>
|
||
<select id="editSize"></select>
|
||
</div>
|
||
</div>
|
||
<div class="row" style="margin-top:16px">
|
||
<button class="btn" id="editBtn">开始修图</button>
|
||
</div>
|
||
<div class="status" id="editStatus"></div>
|
||
</div>
|
||
<div class="card">
|
||
<label style="margin-top:0">对比</label>
|
||
<div class="cmp">
|
||
<figure><figcaption>原图</figcaption><img id="oriImg" alt="原图" style="display:none" /></figure>
|
||
<figure><figcaption>结果</figcaption><img id="outImg" alt="结果" style="display:none" /></figure>
|
||
</div>
|
||
<div class="hint" id="editHint" style="margin-top:10px"></div>
|
||
<div class="results" id="editResults"></div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
<!-- ============ 素材库 ============ -->
|
||
<section id="tab-lib" style="display:none">
|
||
<div class="card">
|
||
<div class="row" style="justify-content:space-between">
|
||
<label style="margin:0">已生成的图(<span id="libCount">0</span> 张)</label>
|
||
<button class="btn ghost" id="libRefresh">刷新</button>
|
||
</div>
|
||
<div class="hint">「快图设计 → 素材」面板里会出现这些图,点一下就能放进画布。</div>
|
||
<div class="results" id="libResults"></div>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
|
||
<footer>
|
||
模型走 omniroute(<span id="omniBase"></span>);生图默认 cheaperinference/grok-imagine,修图精细模式用 cheaperinference/nano-banana-2。
|
||
</footer>
|
||
|
||
<script>
|
||
const $ = (s) => document.querySelector(s);
|
||
const SIZES = {
|
||
'1280x720':'横版 1280×720(头条/视频封面)',
|
||
'900x383':'公众号封面 900×383',
|
||
'1080x1440':'小红书 1080×1440',
|
||
'1200x675':'知乎 1200×675',
|
||
'1146x717':'B站 1146×717',
|
||
'1024x1024':'方形 1024×1024',
|
||
'1080x1920':'竖版海报 1080×1920',
|
||
};
|
||
const STYLES = ['扁平矢量插画','3D 质感渲染','极简留白','深色科技渐变','柔和马卡龙配色','手绘水彩','中式水墨','赛博霓虹','毛玻璃拟物','杂志编辑风'];
|
||
const EDITS = ['把背景换成浅灰渐变','改成暖米色调','去掉图中所有文字','换成扁平插画风格','主体放大并留白给标题','换成纯色背景便于排版','增加柔和光效','改成深色科技风'];
|
||
const SAFE = '画面中不要出现任何文字、字母、数字或水印。';
|
||
|
||
let currentImg = null;
|
||
|
||
async function api(path, opts = {}) {
|
||
const r = await fetch(path, { headers: { 'Content-Type': 'application/json' }, ...opts });
|
||
const d = await r.json().catch(() => ({ ok: false, error: '返回解析失败' }));
|
||
return d;
|
||
}
|
||
|
||
/* ---- 初始化 ---- */
|
||
(async function init() {
|
||
const h = await api('/api/health');
|
||
$('#omniBase').textContent = 'omniroute';
|
||
const sizeSel = $('#size'), editSel = $('#editSize');
|
||
for (const [k, v] of Object.entries(SIZES)) {
|
||
sizeSel.insertAdjacentHTML('beforeend', `<option value="${k}"${k==='1280x720'?' selected':''}>${v}</option>`);
|
||
editSel.insertAdjacentHTML('beforeend', `<option value="${k}"${k==='1280x720'?' selected':''}>${v}</option>`);
|
||
}
|
||
const msel = $('#model');
|
||
(h.models || []).forEach(m => {
|
||
const label = `${m.id.includes('grok') ? 'Grok Imagine' : m.id.split('/').pop()} ${m.ok ? '✅' : '(余额不足)'}`;
|
||
msel.insertAdjacentHTML('beforeend', `<option value="${m.id}">${label}</option>`);
|
||
});
|
||
$('#styleChips').innerHTML = STYLES.map(s => `<span>${s}</span>`).join('');
|
||
$('#editChips').innerHTML = EDITS.map(s => `<span>${s}</span>`).join('');
|
||
document.querySelectorAll('#styleChips span').forEach(el => el.onclick = () => {
|
||
const t = $('#prompt'); t.value = (t.value ? t.value.replace(/[,,]\s*$/, '') + ',' : '') + el.textContent; t.focus();
|
||
});
|
||
document.querySelectorAll('#editChips span').forEach(el => el.onclick = () => {
|
||
const t = $('#editPrompt'); t.value = (t.value ? t.value.replace(/[,,]\s*$/, '') + ',' : '') + el.textContent; t.focus();
|
||
});
|
||
$('#libCount').textContent = (await api('/api/gallery')).items?.length || 0;
|
||
})();
|
||
|
||
/* ---- Tab ---- */
|
||
document.querySelectorAll('.tabs button').forEach(b => b.onclick = () => {
|
||
document.querySelectorAll('.tabs button').forEach(x => x.classList.toggle('on', x === b));
|
||
['gen','edit','lib'].forEach(t => $('#tab-' + t).style.display = (t === b.dataset.tab ? '' : 'none'));
|
||
if (b.dataset.tab === 'lib') loadLib();
|
||
});
|
||
|
||
/* ---- 生图 ---- */
|
||
$('#genBtn').onclick = async () => {
|
||
const prompt = ($('#prompt').value || '').trim();
|
||
if (!prompt) { setS('#genStatus', '先写点画面描述', 'err'); return; }
|
||
$('#genBtn').disabled = true;
|
||
setS('#genStatus', '<span class="loading"></span> 正在生成(约 10 秒)…');
|
||
const t0 = Date.now();
|
||
const r = await api('/api/gen', { method: 'POST', body: JSON.stringify({
|
||
prompt: prompt + '。' + SAFE, size: $('#size').value, model: $('#model').value, n: 1 }) });
|
||
$('#genBtn').disabled = false;
|
||
$('#genTime').textContent = ((Date.now() - t0) / 1000).toFixed(1) + ' 秒';
|
||
if (!r.ok) { setS('#genStatus', r.error || '生成失败', 'err'); return; }
|
||
setS('#genStatus', '生成成功,共 ' + r.images.length + ' 张', 'ok');
|
||
$('#genEmpty').style.display = 'none';
|
||
r.images.forEach(img => $('#genResults').insertAdjacentHTML('afterbegin', cardHtml(img, $('#size').value)));
|
||
bindCards('#genResults');
|
||
};
|
||
|
||
/* ---- 修图 ---- */
|
||
const drop = $('#drop'), fileInput = $('#file');
|
||
drop.onclick = () => fileInput.click();
|
||
drop.ondragover = e => { e.preventDefault(); drop.classList.add('on'); };
|
||
drop.ondragleave = () => drop.classList.remove('on');
|
||
drop.ondrop = e => { e.preventDefault(); drop.classList.remove('on'); if (e.dataTransfer.files[0]) readFile(e.dataTransfer.files[0]); };
|
||
fileInput.onchange = e => e.target.files[0] && readFile(e.target.files[0]);
|
||
window.addEventListener('paste', e => {
|
||
const it = [...(e.clipboardData?.items || [])].find(x => x.type.startsWith('image/'));
|
||
if (it) readFile(it.getAsFile());
|
||
});
|
||
function readFile(f) {
|
||
if (!f || !f.type.startsWith('image/')) return;
|
||
const fr = new FileReader();
|
||
fr.onload = () => {
|
||
currentImg = fr.result;
|
||
$('#oriImg').src = currentImg; $('#oriImg').style.display = '';
|
||
drop.textContent = '已选择:' + f.name + '(点这里可重选)';
|
||
setS('#editStatus', '图片已载入,填好修改要求后点「开始修图」');
|
||
};
|
||
fr.readAsDataURL(f);
|
||
}
|
||
|
||
$('#editBtn').onclick = async () => {
|
||
if (!currentImg) { setS('#editStatus', '先上传一张图', 'err'); return; }
|
||
const mode = document.querySelector('input[name=mode]:checked').value;
|
||
const prompt = ($('#editPrompt').value || '').trim();
|
||
if (!prompt) { setS('#editStatus', '写点修改要求(例如:换成浅灰背景)', 'err'); return; }
|
||
$('#editBtn').disabled = true;
|
||
setS('#editStatus', '<span class="loading"></span> ' + (mode === 'edit' ? '正在精细修图…' : '正在分析原图并重绘(约 25 秒)…'));
|
||
const path = mode === 'edit' ? '/api/edit' : '/api/redraw';
|
||
const body = { image: currentImg, prompt, size: $('#editSize').value };
|
||
const r = await api(path, { method: 'POST', body: JSON.stringify(body) });
|
||
$('#editBtn').disabled = false;
|
||
if (!r.ok) {
|
||
setS('#editStatus', (r.error || '失败') + (mode === 'edit' ? ' → 可切到「AI 重绘」立刻出图' : ''), 'err');
|
||
return;
|
||
}
|
||
const img = r.images[0];
|
||
setS('#editStatus', '完成' + (r.described ? '(原图分析:' + r.described.slice(0, 60) + '…)' : ''), 'ok');
|
||
$('#outImg').src = img.url; $('#outImg').style.display = '';
|
||
const sz = $('#editSize').value;
|
||
$('#editResults').innerHTML = `<div style="display:flex;gap:8px;flex-wrap:wrap;margin-top:12px">
|
||
<a class="mini" href="${img.url}" download>下载结果</a>
|
||
<button class="mini" data-url="${img.url}" data-size="${sz}" data-act="crop">裁剪到 ${sz.replace('x','×')}</button>
|
||
<button class="mini" data-url="${img.url}" data-act="copy">复制地址</button>
|
||
</div>`;
|
||
$('#editResults').querySelectorAll('button').forEach(b => b.onclick = async () => {
|
||
if (b.dataset.act === 'copy') { navigator.clipboard?.writeText(b.dataset.url); b.textContent = '已复制'; setTimeout(() => b.textContent = '复制地址', 1200); }
|
||
if (b.dataset.act === 'crop') await cropDownload(b.dataset.url, b.dataset.size);
|
||
});
|
||
};
|
||
|
||
/* ---- 素材库 ---- */
|
||
async function loadLib() {
|
||
const r = await api('/api/gallery');
|
||
const items = r.items || [];
|
||
$('#libCount').textContent = items.length;
|
||
$('#libResults').innerHTML = items.map(i => cardHtml(i, i.size, true)).join('');
|
||
bindCards('#libResults');
|
||
}
|
||
$('#libRefresh').onclick = loadLib;
|
||
|
||
/* ---- 卡片 ---- */
|
||
function cardHtml(img, size, withDel = true) {
|
||
return `<div class="shot" data-name="${img.name}" data-url="${img.url}" data-size="${size || ''}">
|
||
<img src="${img.url}" loading="lazy" alt="" />
|
||
<div class="bar">
|
||
<a href="${img.url}" download>下载</a>
|
||
<button data-act="crop">裁剪到 ${(size || '').replace('x','×')}</button>
|
||
<button data-act="copy">复制地址</button>
|
||
${withDel ? '<button data-act="del">删除</button>' : ''}
|
||
</div>
|
||
</div>`;
|
||
}
|
||
|
||
function bindCards(sel) {
|
||
document.querySelectorAll(sel + ' .shot').forEach(card => {
|
||
card.querySelectorAll('button').forEach(btn => btn.onclick = async () => {
|
||
const act = btn.dataset.act, url = card.dataset.url, size = card.dataset.size;
|
||
if (act === 'copy') { navigator.clipboard?.writeText(url); btn.textContent = '已复制'; setTimeout(() => btn.textContent = '复制地址', 1200); }
|
||
if (act === 'del') {
|
||
await fetch('/api/gallery/' + card.dataset.name, { method: 'DELETE' });
|
||
card.remove(); loadLib();
|
||
}
|
||
if (act === 'crop') await cropDownload(url, size);
|
||
});
|
||
});
|
||
}
|
||
|
||
/* 按平台尺寸居中裁剪(cover 裁切),浏览器本地完成 */
|
||
async function cropDownload(url, size) {
|
||
if (!size) return;
|
||
const [tw, th] = size.split('x').map(Number);
|
||
const img = new Image(); img.crossOrigin = 'anonymous'; img.src = url;
|
||
await img.decode();
|
||
const scale = Math.max(tw / img.width, th / img.height);
|
||
const sw = tw / scale, sh = th / scale;
|
||
const sx = (img.width - sw) / 2, sy = (img.height - sh) / 2;
|
||
const c = document.createElement('canvas'); c.width = tw; c.height = th;
|
||
c.getContext('2d').drawImage(img, sx, sy, sw, sh, 0, 0, tw, th);
|
||
c.toBlob(b => {
|
||
const a = document.createElement('a');
|
||
a.href = URL.createObjectURL(b);
|
||
a.download = `cover-${tw}x${th}-${Date.now()}.png`;
|
||
a.click();
|
||
}, 'image/png');
|
||
}
|
||
|
||
function setS(sel, html, cls = '') {
|
||
const el = $(sel); el.className = 'status ' + cls; el.innerHTML = html;
|
||
}
|
||
</script>
|
||
</body>
|
||
</html>
|