Files
it-tools-pro/src/tools/text-to-image/text-to-image.vue
T
jinghui 3c126277eb feat: 历史记录100条上限 + 后台文件管理升级收口
- 所有功能历史最多显示100条:relay img-history vis[:150]→[:100]、mcp-usage 流水 LIMIT 300→100
- 前端三处兜底:loadImgHist/ai-gen/poster slice(0,100)
- 上轮遗留收口:后台文件管理全量视图/50条分页/100x100缩略图/下载/三类删除、图片历史铁规则 ImgHist 共享层接入7工具
- Home NEW_FEATURES 与 README 同步
2026-11-14 18:58:48 +08:00

369 lines
10 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useHead } from '@vueuse/head';
import ImgHist from '@/tools/img-hist/ImgHist.vue';
import { blobToB64, saveImgHist } from '@/tools/img-hist/img-hist';
useHead({ title: '文字生成图片 - IT-TOOLS-PRO' });
const histRef = ref<InstanceType<typeof ImgHist> | null>(null);
/** 生成结果保存到服务器历史(失败静默,不影响本地下载) */
async function persist(b64: string) {
try {
const ok = await saveImgHist({ kind: 'text-to-image', tool: 'text-to-image', b64, model: '文字生成图片', prompt: (text.value || '').slice(0, 100) });
if (ok) histRef.value?.reload();
}
catch { /* 静默 */ }
}
const text = ref('在线免费文字生成图片工具\nIT-TOOLS-PRO 让你更高效地工作\n如果你觉得这个工具好用,可以分享给你的小伙伴们');
const format = ref<'png' | 'jpg'>('png');
const width = ref(700);
const height = ref(300);
const posX = ref(20);
const posY = ref(20);
const lineHeight = ref(40);
const fontFamily = ref('"SimSun", "宋体", serif');
const fontSize = ref(24);
const textColor = ref('#009688');
const bgColor = ref('#EEEEEE');
const scale = ref(1); // 导出缩放(DPI 概念)
const FONTS = [
{ label: '宋体', value: '"SimSun", "宋体", serif' },
{ label: '黑体', value: '"SimHei", "黑体", sans-serif' },
{ label: '楷体', value: '"KaiTi", "楷体", serif' },
{ label: '仿宋', value: '"FangSong", "仿宋", serif' },
{ label: '微软雅黑', value: '"Microsoft YaHei", "微软雅黑", sans-serif' },
{ label: 'Arial', value: 'Arial, sans-serif' },
{ label: 'Times New Roman', value: '"Times New Roman", serif' },
];
const canvas = ref<HTMLCanvasElement>();
const generated = ref(false);
const resultUrl = ref('');
const resultSize = ref(0);
function render(toBlob = false, save = false) {
const c = canvas.value;
if (!c) {
return;
}
const s = scale.value;
c.width = Math.round(width.value * s);
c.height = Math.round(height.value * s);
const ctx = c.getContext('2d')!;
ctx.fillStyle = bgColor.value;
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = textColor.value;
ctx.font = `${fontSize.value * s}px ${fontFamily.value}`;
ctx.textBaseline = 'top';
const lines = text.value.split('\n');
lines.forEach((line, i) => {
ctx.fillText(line, posX.value * s, (posY.value + i * lineHeight.value) * s);
});
generated.value = true;
if (toBlob) {
const mime = format.value === 'jpg' ? 'image/jpeg' : 'image/png';
c.toBlob((blob) => {
if (!blob) {
return;
}
if (resultUrl.value) {
URL.revokeObjectURL(resultUrl.value);
}
resultUrl.value = URL.createObjectURL(blob);
resultSize.value = blob.size;
if (save) {
blobToB64(blob).then((b64) => { if (b64) persist(b64); });
}
}, mime, 0.92);
}
}
function download() {
render(true);
setTimeout(() => {
const a = document.createElement('a');
a.href = resultUrl.value;
a.download = `文字图片.${format.value}`;
a.click();
}, 200);
}
function resetAll() {
text.value = '在线免费文字生成图片工具\nIT-TOOLS-PRO 让你更高效地工作\n如果你觉得这个工具好用,可以分享给你的小伙伴们';
format.value = 'png';
width.value = 700;
height.value = 300;
posX.value = 20;
posY.value = 20;
lineHeight.value = 40;
fontFamily.value = '"SimSun", "宋体", serif';
fontSize.value = 24;
textColor.value = '#009688';
bgColor.value = '#EEEEEE';
scale.value = 1;
generated.value = false;
resultUrl.value = '';
}
onMounted(() => {
render();
});
</script>
<template>
<div class="ti-root">
<div class="ti-main">
<div class="ti-panel">
<div class="ti-panel-title">📝 文本内容</div>
<textarea v-model="text" class="ti-textarea" rows="6" placeholder="输入要生成图片的文字,支持多行…" @input="render()" />
</div>
<div class="ti-panel">
<div class="ti-panel-title">⚙️ 参数设置</div>
<div class="ti-grid">
<div class="ti-field">
<label class="ti-label">格式</label>
<select v-model="format" class="ti-select" @change="render()">
<option value="png">png</option>
<option value="jpg">jpg</option>
</select>
</div>
<div class="ti-field">
<label class="ti-label">宽度(px)</label>
<input v-model.number="width" type="number" min="50" max="4000" class="ti-num" @change="render()">
</div>
<div class="ti-field">
<label class="ti-label">高度(px)</label>
<input v-model.number="height" type="number" min="50" max="4000" class="ti-num" @change="render()">
</div>
<div class="ti-field">
<label class="ti-label">位置 X</label>
<input v-model.number="posX" type="number" min="0" max="4000" class="ti-num" @change="render()">
</div>
<div class="ti-field">
<label class="ti-label">位置 Y</label>
<input v-model.number="posY" type="number" min="0" max="4000" class="ti-num" @change="render()">
</div>
<div class="ti-field">
<label class="ti-label">行高(px)</label>
<input v-model.number="lineHeight" type="number" min="10" max="400" class="ti-num" @change="render()">
</div>
<div class="ti-field">
<label class="ti-label">字体</label>
<select v-model="fontFamily" class="ti-select" @change="render()">
<option v-for="f in FONTS" :key="f.value" :value="f.value">{{ f.label }}</option>
</select>
</div>
<div class="ti-field">
<label class="ti-label">文字大小(px)</label>
<input v-model.number="fontSize" type="number" min="8" max="400" class="ti-num" @change="render()">
</div>
<div class="ti-field">
<label class="ti-label">文字颜色</label>
<div class="ti-color-row">
<input v-model="textColor" class="ti-color-input" @input="render()">
<input v-model="textColor" type="color" class="swatch" @input="render()">
</div>
</div>
<div class="ti-field">
<label class="ti-label">背景色</label>
<div class="ti-color-row">
<input v-model="bgColor" class="ti-color-input" @input="render()">
<input v-model="bgColor" type="color" class="swatch" @input="render()">
</div>
</div>
<div class="ti-field">
<label class="ti-label">导出倍数(DPI)</label>
<select v-model.number="scale" class="ti-select" @change="render()">
<option :value="1">1x(标准)</option>
<option :value="2">2x(高清)</option>
<option :value="3">3x(印刷级)</option>
</select>
</div>
</div>
<div class="ti-btns">
<button class="ti-btn" @click="render(true, true)">
🚀 生成
</button>
<button class="ti-btn ti-btn-ghost" :disabled="!resultUrl" @click="download">
⬇ 下载
</button>
<button class="ti-btn ti-btn-ghost" @click="resetAll">
重置
</button>
</div>
<div v-if="resultUrl" class="ti-result">
✓ 已生成 {{ width }}×{{ height }} {{ format.toUpperCase() }}{{ scale > 1 ? `(${width * scale}×${height * scale} 输出)` : '' }} · {{ (resultSize / 1024).toFixed(0) }} KB
</div>
</div>
<div class="ti-panel">
<div class="ti-panel-title">🖼️ 预览</div>
<div class="ti-preview-box">
<canvas ref="canvas" class="ti-canvas" />
</div>
</div>
</div>
<ImgHist ref="histRef" kind="text-to-image" />
</div>
</template>
<style lang="less" scoped>
.ti-root {
padding-bottom: 40px;
display: flex;
flex-direction: row;
gap: 12px;
align-items: flex-start;
}
.ti-main {
flex: 1;
min-width: 0;
}
@media (max-width: 760px) {
.ti-root { flex-direction: column; }
}
.ti-panel {
border: 1px solid rgba(128, 128, 128, 0.22);
border-radius: 12px;
padding: 12px 14px;
background: #fff;
margin-bottom: 12px;
.ti-panel-title {
font-size: 14px;
font-weight: 800;
margin-bottom: 10px;
}
}
.ti-textarea {
width: 100%;
box-sizing: border-box;
border: 1px solid rgba(128, 128, 128, 0.35);
border-radius: 8px;
padding: 10px 12px;
font-size: 14px;
font-family: inherit;
resize: vertical;
outline: none;
&:focus {
border-color: #18a058;
}
}
.ti-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px 14px;
}
.ti-field {
.ti-label {
display: block;
font-size: 11.5px;
opacity: 0.65;
margin-bottom: 3px;
}
.ti-num,
.ti-select,
.ti-color-input {
width: 100%;
box-sizing: border-box;
border: 1px solid rgba(128, 128, 128, 0.35);
border-radius: 7px;
padding: 5px 9px;
font-size: 12.5px;
background: #fff;
outline: none;
&:focus {
border-color: #18a058;
}
}
}
.ti-color-row {
display: flex;
gap: 6px;
.ti-color-input {
flex: 1;
}
.swatch {
width: 32px;
height: 28px;
border: 1px solid rgba(128, 128, 128, 0.35);
border-radius: 7px;
padding: 1px;
background: #fff;
cursor: pointer;
flex-shrink: 0;
}
}
.ti-btns {
display: flex;
gap: 8px;
margin-top: 14px;
}
.ti-btn {
background: #18a058;
color: #fff;
border: none;
border-radius: 8px;
padding: 8px 20px;
font-size: 13px;
font-weight: 700;
cursor: pointer;
&:hover:not(:disabled) {
opacity: 0.85;
}
&:disabled {
opacity: 0.45;
cursor: not-allowed;
}
&.ti-btn-ghost {
background: #fff;
color: inherit;
border: 1px solid rgba(128, 128, 128, 0.35);
font-weight: 400;
}
}
.ti-result {
margin-top: 10px;
font-size: 12px;
color: #18a058;
font-weight: 600;
}
.ti-preview-box {
display: flex;
justify-content: center;
background: rgba(128, 128, 128, 0.05);
border-radius: 10px;
padding: 12px;
overflow: auto;
.ti-canvas {
max-width: 100%;
border: 1px solid rgba(128, 128, 128, 0.2);
}
}
</style>