428 lines
19 KiB
Vue
428 lines
19 KiB
Vue
<script setup lang="ts">
|
||
import { getMachineId, licenseHeaders } from '@/tools/license/license';
|
||
|
||
// ---------- 状态 ----------
|
||
const file = ref<File | null>(null);
|
||
const previewUrl = ref('');
|
||
const prompt = ref('');
|
||
const size = ref('default');
|
||
const customW = ref(1024);
|
||
const customH = ref(1024);
|
||
const count = ref(1);
|
||
const model = ref('qwen3.8-max');
|
||
const models = ref<string[]>([]);
|
||
const loading = ref(false);
|
||
const error = ref('');
|
||
const note = ref('');
|
||
const edits = ref<{ old: string; new: string; style: string }[]>([]);
|
||
const results = ref<{ url: string; scale: number }[]>([]);
|
||
|
||
const SIZES = [
|
||
{ k: 'default', label: '默认' },
|
||
{ k: '4:3', label: '4:3' },
|
||
{ k: '3:4', label: '3:4' },
|
||
{ k: '1:1', label: '1:1' },
|
||
{ k: 'custom', label: '自定义' },
|
||
];
|
||
|
||
const EXAMPLES = [
|
||
{ n: '改数字', p: '把图中"人数8"改成"31",保持原字体样式、大小和颜色' },
|
||
{ n: '改姓名', p: '把名字"张三"改成"李四",完全匹配原字体、字号和颜色,融合背景纹理' },
|
||
{ n: '改价格', p: '把价格"99元"改成"199元",保持促销海报的立体金色渐变效果' },
|
||
{ n: '删除文字', p: '把图片底部的水印文字无痕删除' },
|
||
{ n: '改金额', p: '把转账金额"¥1000.00"改成"¥5000.00",保持界面原字体和绿色气泡背景' },
|
||
];
|
||
|
||
const canGo = computed(() => !!file.value && !!prompt.value.trim() && !loading.value);
|
||
|
||
onMounted(async () => {
|
||
try {
|
||
const res = await fetch('/relay/ai/vision-models');
|
||
const j = await res.json();
|
||
if (j.code === 200 && Array.isArray(j.data) && j.data.length) {
|
||
models.value = j.data;
|
||
if (!j.data.includes(model.value)) model.value = j.data[0];
|
||
}
|
||
}
|
||
catch { /* 用默认 */ }
|
||
loadTasks();
|
||
});
|
||
|
||
// ---------- 最近任务(localStorage 缩略图) ----------
|
||
interface SETask { id: string; time: number; prompt: string; before: string; after: string; count: number }
|
||
const tasks = ref<SETask[]>([]);
|
||
const LS_KEY = 'it-ai-text-edit:tasks';
|
||
|
||
function loadTasks() {
|
||
try {
|
||
const arr = JSON.parse(localStorage.getItem(LS_KEY) ?? '[]');
|
||
tasks.value = Array.isArray(arr) ? arr.slice(0, 12) : [];
|
||
}
|
||
catch { tasks.value = []; }
|
||
}
|
||
|
||
function saveTasks() {
|
||
try {
|
||
localStorage.setItem(LS_KEY, JSON.stringify(tasks.value.slice(0, 12)));
|
||
}
|
||
catch { /* 缩略图过大则丢弃历史 */ }
|
||
}
|
||
|
||
/** 200px 宽 JPEG 缩略图(控制 localStorage 体积) */
|
||
function thumbOf(url: string): Promise<string> {
|
||
return new Promise((resolve) => {
|
||
const im = new Image();
|
||
im.onload = () => {
|
||
const k = 200 / Math.max(im.width, im.height);
|
||
const c = document.createElement('canvas');
|
||
c.width = Math.max(1, Math.round(im.width * k));
|
||
c.height = Math.max(1, Math.round(im.height * k));
|
||
c.getContext('2d')!.drawImage(im, 0, 0, c.width, c.height);
|
||
resolve(c.toDataURL('image/jpeg', 0.7));
|
||
};
|
||
im.onerror = () => resolve('');
|
||
im.src = url;
|
||
});
|
||
}
|
||
|
||
function fmtTime(t: number): string {
|
||
const d = new Date(t);
|
||
const p = (n: number) => String(n).padStart(2, '0');
|
||
return `${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
||
}
|
||
|
||
// ---------- 上传 ----------
|
||
function onPick(e: Event) {
|
||
const f = (e.target as HTMLInputElement).files?.[0];
|
||
if (f) setFile(f);
|
||
}
|
||
|
||
function onDrop(e: DragEvent) {
|
||
const f = e.dataTransfer?.files?.[0];
|
||
if (f) setFile(f);
|
||
}
|
||
|
||
function setFile(f: File) {
|
||
if (!/^image\/(jpeg|png|webp)$/.test(f.type)) {
|
||
error.value = '仅支持 JPEG / PNG / WEBP';
|
||
return;
|
||
}
|
||
if (f.size > 24 * 1024 * 1024) {
|
||
error.value = '图片超过 24MB';
|
||
return;
|
||
}
|
||
file.value = f;
|
||
error.value = '';
|
||
if (previewUrl.value) URL.revokeObjectURL(previewUrl.value);
|
||
previewUrl.value = URL.createObjectURL(f);
|
||
}
|
||
|
||
function b64DataUrl(b64: string) {
|
||
let mime = 'image/png';
|
||
if (b64.startsWith('/9j/')) mime = 'image/jpeg';
|
||
else if (b64.startsWith('UklGR')) mime = 'image/webp';
|
||
return `data:${mime};base64,${b64}`;
|
||
}
|
||
|
||
// ---------- 提交 ----------
|
||
async function submit() {
|
||
if (!canGo.value) return;
|
||
loading.value = true;
|
||
error.value = '';
|
||
note.value = '';
|
||
edits.value = [];
|
||
results.value = [];
|
||
try {
|
||
const fd = new FormData();
|
||
fd.append('image', file.value!, file.value!.name || 'image.png');
|
||
fd.append('prompt', prompt.value.trim());
|
||
fd.append('model', model.value);
|
||
fd.append('size', size.value);
|
||
fd.append('count', String(count.value));
|
||
if (size.value === 'custom') {
|
||
fd.append('custom_w', String(customW.value));
|
||
fd.append('custom_h', String(customH.value));
|
||
}
|
||
const res = await fetch('/relay/ai/seamless-edit', {
|
||
method: 'POST',
|
||
headers: licenseHeaders('ai-text-edit'),
|
||
body: fd,
|
||
});
|
||
const j = await res.json().catch(() => ({}));
|
||
if (!res.ok || j.code !== 200) throw new Error(j.message || `HTTP ${res.status}`);
|
||
const d = j.data || {};
|
||
edits.value = Array.isArray(d.edits) ? d.edits : [];
|
||
note.value = String(d.note || '');
|
||
results.value = (Array.isArray(d.images) ? d.images : []).map((x: any) => ({ url: b64DataUrl(x.b64), scale: x.scale }));
|
||
if (results.value.length) {
|
||
const [before, after] = await Promise.all([thumbOf(previewUrl.value), thumbOf(results.value[0].url)]);
|
||
tasks.value.unshift({
|
||
id: `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`,
|
||
time: Date.now(),
|
||
prompt: prompt.value.trim(),
|
||
before,
|
||
after,
|
||
count: results.value.length,
|
||
});
|
||
tasks.value = tasks.value.slice(0, 12);
|
||
saveTasks();
|
||
}
|
||
else if (!note.value) {
|
||
note.value = '未产生结果,请调整描述后重试';
|
||
}
|
||
}
|
||
catch (e: any) {
|
||
error.value = String(e?.message || e);
|
||
}
|
||
finally {
|
||
loading.value = false;
|
||
}
|
||
}
|
||
|
||
function clearAll() {
|
||
file.value = null;
|
||
if (previewUrl.value) URL.revokeObjectURL(previewUrl.value);
|
||
previewUrl.value = '';
|
||
results.value = [];
|
||
edits.value = [];
|
||
note.value = '';
|
||
error.value = '';
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="se-root">
|
||
<div class="se-head">
|
||
<div class="se-title">无痕改字</div>
|
||
<div class="se-sub">AI 智能修改图片文字 · 自动匹配原字体/字号/颜色/角度 · 本地模型驱动</div>
|
||
</div>
|
||
|
||
<div class="se-grid">
|
||
<!-- 左:输入面板 -->
|
||
<div class="se-ctrl">
|
||
<!-- 上传 -->
|
||
<div
|
||
class="se-upload"
|
||
:class="{ has: !!previewUrl }"
|
||
@click="!previewUrl && ($refs.fileInput as HTMLInputElement).click()"
|
||
@dragover.prevent
|
||
@drop.prevent="onDrop"
|
||
>
|
||
<template v-if="previewUrl">
|
||
<img :src="previewUrl" class="se-upload-img" alt="">
|
||
<div class="se-upload-bar">
|
||
<span class="se-upload-name">{{ file?.name }}</span>
|
||
<a href="javascript:;" class="se-upload-x" @click.stop="clearAll">✕ 重新选择</a>
|
||
</div>
|
||
</template>
|
||
<template v-else>
|
||
<div class="se-upload-icon">☁️⬆️</div>
|
||
<div class="se-upload-t">拖拽图片到此处 · 点击上传</div>
|
||
<div class="se-upload-s">支持最大 24MB 的 JPEG、PNG 或 WEBP 格式,全程本地+内网处理</div>
|
||
</template>
|
||
<input ref="fileInput" type="file" accept="image/jpeg,image/png,image/webp" style="display: none" @change="onPick">
|
||
</div>
|
||
|
||
<!-- 提示词 -->
|
||
<div class="se-sec">
|
||
<div class="se-h">提示</div>
|
||
<textarea
|
||
v-model="prompt"
|
||
class="se-prompt"
|
||
maxlength="200"
|
||
placeholder="填写无痕改字的具体要求,包括需要修改的文字内容和样式,例如:把图中「人数8」改成「31」,保持原字体样式和背景"
|
||
/>
|
||
<div class="se-count">{{ prompt.length }}/200</div>
|
||
<div class="se-examples">
|
||
<button v-for="ex in EXAMPLES" :key="ex.n" class="se-example" @click="prompt = ex.p">{{ ex.n }}</button>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 尺寸 -->
|
||
<div class="se-sec">
|
||
<div class="se-h">尺寸</div>
|
||
<div class="se-btns">
|
||
<button
|
||
v-for="s in SIZES"
|
||
:key="s.k"
|
||
class="se-btn"
|
||
:class="{ on: size === s.k }"
|
||
@click="size = s.k"
|
||
>{{ s.label }}</button>
|
||
</div>
|
||
<div v-if="size === 'custom'" class="se-custom">
|
||
<input v-model.number="customW" type="number" min="64" max="2048" class="se-num">
|
||
<span>×</span>
|
||
<input v-model.number="customH" type="number" min="64" max="2048" class="se-num">
|
||
<span class="se-custom-s">px(自动补边不裁内容)</span>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 输出数量 -->
|
||
<div class="se-sec">
|
||
<div class="se-h">输出数量</div>
|
||
<div class="se-btns">
|
||
<button v-for="n in [1, 2, 3]" :key="n" class="se-btn se-btn-n" :class="{ on: count === n }" @click="count = n">{{ n }}</button>
|
||
</div>
|
||
<div class="se-tip-s">多张输出为新文字大小的微调档位(字号自动估计有误差,出 2~3 张挑最像的)</div>
|
||
</div>
|
||
|
||
<!-- 模型 -->
|
||
<div class="se-sec">
|
||
<div class="se-h">模型</div>
|
||
<select v-model="model" class="se-model">
|
||
<option v-for="m in models" :key="m" :value="m">{{ m }}(本地)</option>
|
||
<option v-if="!models.length" value="qwen3.8-max">qwen3.8-max(本地)</option>
|
||
</select>
|
||
</div>
|
||
|
||
<button class="se-go" :disabled="!canGo" @click="submit()">
|
||
<span v-if="loading" class="se-spin">⏳</span>
|
||
<span v-else>✨</span>
|
||
{{ loading ? '处理中:识别 → AI 定位 → 无缝重绘…' : '提交' }}
|
||
</button>
|
||
|
||
<div v-if="error" class="se-err">⚠️ {{ error }}</div>
|
||
|
||
<!-- 结果 -->
|
||
<div v-if="results.length" class="se-results">
|
||
<div class="se-h">修改结果</div>
|
||
<div class="se-cmp">
|
||
<div class="se-cmp-col">
|
||
<div class="se-cmp-t">原始文字</div>
|
||
<img :src="previewUrl" class="se-cmp-img" alt="">
|
||
</div>
|
||
<div class="se-cmp-col">
|
||
<div class="se-cmp-t se-cmp-t-new">新文字</div>
|
||
<img :src="results[0].url" class="se-cmp-img" alt="">
|
||
</div>
|
||
</div>
|
||
<div v-if="edits.length" class="se-edits">
|
||
<div v-for="(e, i) in edits" :key="i" class="se-edit-row">
|
||
「{{ e.old }}」→「{{ e.new }}」<span class="se-edit-style">{{ e.style }}</span>
|
||
</div>
|
||
</div>
|
||
<div v-if="results.length > 1" class="se-variants">
|
||
<a v-for="(r, i) in results" :key="i" class="se-variant" :href="r.url" target="_blank" rel="noopener">
|
||
变体 {{ i + 1 }}({{ Math.round(r.scale * 100) }}%字号)
|
||
</a>
|
||
</div>
|
||
<div class="se-acts">
|
||
<a class="se-dl" :href="results[0].url" target="_blank" rel="noopener" download>⬇ 下载图片</a>
|
||
<a class="se-dl" :href="results[0].url" target="_blank" rel="noopener">🔗 新窗口打开</a>
|
||
</div>
|
||
</div>
|
||
<div v-else-if="note" class="se-note">💡 {{ note }}</div>
|
||
</div>
|
||
|
||
<!-- 右:最近任务 -->
|
||
<div class="se-side">
|
||
<div class="se-side-h">
|
||
最近任务
|
||
<a href="javascript:;" class="se-refresh" @click="loadTasks()">刷新</a>
|
||
</div>
|
||
<div v-if="!tasks.length" class="se-side-empty">
|
||
还没有任务——左侧上传图片并描述要改的文字,提交后这里会保留前后对比记录(仅本机浏览器)
|
||
</div>
|
||
<div v-for="t in tasks" :key="t.id" class="se-task">
|
||
<div class="se-task-time">{{ fmtTime(t.time) }}<span v-if="t.count > 1" class="se-task-n">{{ t.count }}张</span></div>
|
||
<div class="se-task-p">{{ t.prompt }}</div>
|
||
<div class="se-task-imgs">
|
||
<div class="se-task-col">
|
||
<img :src="t.before" class="se-task-img" alt="">
|
||
<div class="se-task-lb">原始文字</div>
|
||
</div>
|
||
<div class="se-task-col">
|
||
<img :src="t.after" class="se-task-img" alt="">
|
||
<div class="se-task-lb se-task-lb-new">新文字</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.se-root { max-width: 1200px; margin: 0 auto; padding: 18px 12px 40px; color: var(--n-text-color, #333); }
|
||
.se-head { text-align: center; margin-bottom: 18px; }
|
||
.se-title { font-size: 30px; font-weight: 800; letter-spacing: 2px; }
|
||
.se-sub { margin-top: 6px; font-size: 13px; opacity: 0.6; }
|
||
|
||
.se-grid { display: grid; grid-template-columns: 380px 1fr; gap: 16px; align-items: start; }
|
||
@media (max-width: 900px) { .se-grid { grid-template-columns: 1fr; } }
|
||
|
||
.se-ctrl { background: #fff; border: 1px solid rgba(128,128,128,0.18); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 14px; box-shadow: 0 1px 4px rgba(0,0,0,0.04); }
|
||
html.dark .se-ctrl { background: rgba(255,255,255,0.04); }
|
||
|
||
.se-upload { border: 2px dashed rgba(128,128,128,0.45); border-radius: 10px; min-height: 150px; display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: pointer; transition: border-color 0.15s; overflow: hidden; }
|
||
.se-upload:hover { border-color: #ff7a1a; }
|
||
.se-upload.has { padding: 8px; cursor: default; }
|
||
.se-upload-icon { font-size: 28px; }
|
||
.se-upload-t { margin-top: 8px; font-size: 14px; font-weight: 600; }
|
||
.se-upload-s { margin-top: 5px; font-size: 11.5px; opacity: 0.55; text-align: center; padding: 0 14px; }
|
||
.se-upload-img { max-width: 100%; max-height: 220px; border-radius: 8px; display: block; margin: 0 auto; }
|
||
.se-upload-bar { display: flex; justify-content: space-between; align-items: center; margin-top: 7px; font-size: 12px; }
|
||
.se-upload-name { opacity: 0.7; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||
.se-upload-x { color: #e53935; text-decoration: none; flex-shrink: 0; margin-left: 8px; }
|
||
|
||
.se-sec { display: flex; flex-direction: column; gap: 7px; }
|
||
.se-h { font-size: 13px; font-weight: 700; opacity: 0.85; }
|
||
.se-prompt { width: 100%; min-height: 86px; resize: vertical; border: 1px solid rgba(128,128,128,0.35); border-radius: 8px; padding: 9px 11px; font-size: 13px; line-height: 1.55; background: transparent; color: inherit; }
|
||
.se-prompt:focus { outline: none; border-color: #ff7a1a; }
|
||
.se-count { font-size: 11px; opacity: 0.45; text-align: right; }
|
||
.se-examples { display: flex; flex-wrap: wrap; gap: 6px; }
|
||
.se-example { border: 1px solid rgba(128,128,128,0.3); background: transparent; color: inherit; border-radius: 999px; font-size: 11.5px; padding: 4px 10px; cursor: pointer; opacity: 0.8; transition: all 0.12s; }
|
||
.se-example:hover { border-color: #ff7a1a; color: #ff7a1a; opacity: 1; }
|
||
|
||
.se-btns { display: flex; gap: 7px; flex-wrap: wrap; }
|
||
.se-btn { border: 1.5px solid rgba(128,128,128,0.35); background: transparent; color: inherit; border-radius: 8px; font-size: 12.5px; padding: 6px 14px; cursor: pointer; transition: all 0.12s; }
|
||
.se-btn.on { border-color: #ff7a1a; color: #ff7a1a; background: rgba(255,122,26,0.08); font-weight: 700; }
|
||
.se-btn-n { width: 44px; text-align: center; }
|
||
.se-custom { display: flex; align-items: center; gap: 6px; font-size: 12px; }
|
||
.se-num { width: 70px; border: 1px solid rgba(128,128,128,0.35); border-radius: 6px; padding: 4px 6px; background: transparent; color: inherit; font-size: 12px; }
|
||
.se-custom-s, .se-tip-s { opacity: 0.5; font-size: 11px; }
|
||
|
||
.se-model { border: 1px solid rgba(128,128,128,0.35); border-radius: 8px; padding: 7px 10px; font-size: 12.5px; background: transparent; color: inherit; }
|
||
|
||
.se-go { margin-top: 2px; border: none; background: #ff7a1a; color: #fff; font-size: 15.5px; font-weight: 700; border-radius: 10px; padding: 12px; cursor: pointer; transition: filter 0.12s, opacity 0.12s; }
|
||
.se-go:hover:not(:disabled) { filter: brightness(1.06); }
|
||
.se-go:disabled { opacity: 0.45; cursor: not-allowed; }
|
||
.se-spin { display: inline-block; animation: se-rot 1s linear infinite; }
|
||
@keyframes se-rot { to { transform: rotate(360deg); } }
|
||
|
||
.se-err { background: rgba(229,57,53,0.08); border: 1px solid rgba(229,57,53,0.35); color: #e53935; border-radius: 8px; font-size: 12.5px; padding: 9px 11px; }
|
||
.se-note { background: rgba(255,122,26,0.08); border: 1px solid rgba(255,122,26,0.3); border-radius: 8px; font-size: 12.5px; padding: 9px 11px; }
|
||
|
||
.se-results { display: flex; flex-direction: column; gap: 10px; }
|
||
.se-cmp { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
|
||
.se-cmp-col { text-align: center; }
|
||
.se-cmp-t { font-size: 12px; font-weight: 700; opacity: 0.7; margin-bottom: 5px; }
|
||
.se-cmp-t-new { color: #ff7a1a; opacity: 1; }
|
||
.se-cmp-img { max-width: 100%; border-radius: 8px; border: 1px solid rgba(128,128,128,0.2); }
|
||
.se-edits { font-size: 12px; background: rgba(128,128,128,0.07); border-radius: 8px; padding: 8px 10px; display: flex; flex-direction: column; gap: 4px; }
|
||
.se-edit-style { opacity: 0.5; margin-left: 6px; font-size: 11px; }
|
||
.se-variants { display: flex; gap: 7px; flex-wrap: wrap; }
|
||
.se-variant { font-size: 12px; border: 1px solid rgba(128,128,128,0.35); border-radius: 999px; padding: 4px 11px; text-decoration: none; color: inherit; }
|
||
.se-variant:hover { border-color: #ff7a1a; color: #ff7a1a; }
|
||
.se-acts { display: flex; gap: 9px; }
|
||
.se-dl { flex: 1; text-align: center; font-size: 13px; font-weight: 700; text-decoration: none; color: #fff; background: #ff7a1a; border-radius: 9px; padding: 9px 0; }
|
||
.se-dl + .se-dl { background: rgba(128,128,128,0.18); color: inherit; }
|
||
|
||
.se-side { background: #fff; border: 1px solid rgba(128,128,128,0.18); border-radius: 12px; padding: 16px; box-shadow: 0 1px 4px rgba(0,0,0,0.04); }
|
||
html.dark .se-side { background: rgba(255,255,255,0.04); }
|
||
.se-side-h { font-size: 15px; font-weight: 700; display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
|
||
.se-refresh { font-size: 12px; font-weight: 400; color: #ff7a1a; text-decoration: none; border: 1px solid rgba(255,122,26,0.5); border-radius: 999px; padding: 3px 12px; }
|
||
.se-side-empty { font-size: 12.5px; opacity: 0.5; line-height: 1.7; padding: 18px 6px; }
|
||
|
||
.se-task { border: 1px solid rgba(128,128,128,0.16); border-radius: 10px; padding: 11px; margin-bottom: 11px; }
|
||
.se-task-time { font-size: 11px; opacity: 0.5; display: flex; align-items: center; gap: 6px; }
|
||
.se-task-n { background: rgba(255,122,26,0.12); color: #ff7a1a; border-radius: 999px; padding: 1px 7px; font-size: 10.5px; }
|
||
.se-task-p { font-size: 12.5px; margin: 6px 0 8px; line-height: 1.5; }
|
||
.se-task-imgs { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
|
||
.se-task-col { text-align: center; }
|
||
.se-task-img { max-width: 100%; border-radius: 7px; border: 1px solid rgba(128,128,128,0.18); }
|
||
.se-task-lb { font-size: 11px; opacity: 0.55; margin-top: 4px; }
|
||
.se-task-lb-new { color: #ff7a1a; opacity: 1; font-weight: 700; }
|
||
</style>
|