feat: 文字生成图片 — 多行文字canvas渲染, 字体/字号/颜色/背景/位置/行高/导出倍数(1x-3x)可调, PNG/JPG实时预览下载, 受控授权

This commit is contained in:
personal82555 committed 2026-10-12 18:03:53 +08:00
1 parent b93a155c41
commit 9c849e78ba
8 files changed
+367 -2

No files matched your search

+1
View File
@@ -223,6 +223,7 @@ declare module '@vue/runtime-core' {
TextDiff: typeof import('./src/tools/text-diff/text-diff.vue')['default']
TextStatistics: typeof import('./src/tools/text-statistics/text-statistics.vue')['default']
TextToBinary: typeof import('./src/tools/text-to-binary/text-to-binary.vue')['default']
TextToImage: typeof import('./src/tools/text-to-image/text-to-image.vue')['default']
TextToNatoAlphabet: typeof import('./src/tools/text-to-nato-alphabet/text-to-nato-alphabet.vue')['default']
TextToUnicode: typeof import('./src/tools/text-to-unicode/text-to-unicode.vue')['default']
TokenDisplay: typeof import('./src/tools/otp-code-generator-and-validator/token-display.vue')['default']
+1 -1
View File
@@ -44,7 +44,7 @@ AUTH_PASS = os.environ.get("AUTH_PASS", "")
PROJECT_NAME = os.environ.get("PROJECT_NAME", "IT-Tools")
TRIAL_FILE = os.environ.get("TRIAL_FILE", "/app/data/trials.json")
TRIAL_MS = int(os.environ.get("TRIAL_MS_MIN", "30")) * 60 * 1000
PROTECTED_TOOLS = {"translator", "image-ocr", "pdf-ocr", "ai-retouch", "label-designer", "doc-convert", "image-tools", "pdf-tools", "video-tools", "audio-tools", "seal-maker", "signature-pad"}
PROTECTED_TOOLS = {"translator", "image-ocr", "pdf-ocr", "ai-retouch", "label-designer", "doc-convert", "image-tools", "pdf-tools", "video-tools", "audio-tools", "seal-maker", "signature-pad", "text-to-image"}
MACHINE_RE = re.compile(r"^\d{10,16}$")
# verify() messages that unconditionally block activation
+3
View File
@@ -275,6 +275,9 @@ tools:
title: Image OCR
description: Extract text from images with OCR - click, drag & drop or paste a screenshot, get recognized lines with confidence scores, copy or download as TXT.
text-to-image:
title: 'Text to image'
description: 'Render multi-line text into images with custom font, color, background, export PNG/JPG up to 3x'
signature-pad:
title: 'Signature pad'
description: 'Draw handwritten signatures in browser with realistic pen strokes, export transparent PNG, sign images and PDFs'
+3
View File
@@ -272,6 +272,9 @@ tools:
title: 图片OCR文字识别
description: 上传、拖拽或粘贴截图识别图片中的文字,逐行输出识别结果和置信度,支持复制全部和下载TXT。
text-to-image:
title: '文字生成图片'
description: '多行文字一键生成图片,字体/字号/颜色/背景/位置/行高可调,PNG/JPG 导出,支持 2x/3x 高清倍数'
signature-pad:
title: '手写签名'
description: '浏览器本地手写签名(鼠标/触屏/手写笔),真实笔触模拟,透明底 PNG 下载,可一键签章到图片与 PDF 合同'
+2 -1
View File
@@ -85,6 +85,7 @@ import { tool as videoTools } from './video-tools';
import { tool as audioTools } from './audio-tools';
import { tool as sealMaker } from './seal-maker';
import { tool as signaturePad } from './signature-pad';
import { tool as textToImage } from './text-to-image';
import { tool as docConvert } from './doc-convert';
import { tool as aiRetouch } from './ai-retouch';
import { tool as wifiQrCodeGenerator } from './wifi-qr-code-generator';
@@ -156,7 +157,7 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Images and videos',
components: [qrCodeGenerator, wifiQrCodeGenerator, batchBarcodeGenerator, codeReader, imageOcr, pdfOcr, pdfTools, imageTools, videoTools, audioTools, sealMaker, signaturePad, docConvert, aiRetouch, labelDesigner, siteBookmarks, svgPlaceholderGenerator, cameraRecorder],
components: [qrCodeGenerator, wifiQrCodeGenerator, batchBarcodeGenerator, codeReader, imageOcr, pdfOcr, pdfTools, imageTools, videoTools, audioTools, sealMaker, signaturePad, textToImage, docConvert, aiRetouch, labelDesigner, siteBookmarks, svgPlaceholderGenerator, cameraRecorder],
},
{
name: 'Development',
+1
View File
@@ -18,6 +18,7 @@ export const PROTECTED_TOOLS: Record<string, string> = {
'/audio-tools': '音频工具',
'/seal-maker': '印章生成',
'/signature-pad': '手写签名',
'/text-to-image': '文字生成图片',
};
export const TRIAL_MS = 30 * 60 * 1000; // 每个功能免费 30 分钟
+12
View File
@@ -0,0 +1,12 @@
import { Typography } from '@vicons/tabler';
import { defineTool } from '../tool';
import { translate } from '@/plugins/i18n.plugin';
export const tool = defineTool({
name: translate('tools.text-to-image.title'),
path: '/text-to-image',
description: translate('tools.text-to-image.description'),
keywords: ['text', 'image', '文字', '生成图片', '文字图片', '排版'],
component: () => import('./text-to-image.vue'),
icon: Typography,
});
+344
View File
@@ -0,0 +1,344 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { useHead } from '@vueuse/head';
import GenHistory from '@/tools/gen-history/GenHistory.vue';
import { useGenHistory } from '@/tools/gen-history/gen-history';
useHead({ title: '文字生成图片 - IT-TOOLS-PRO' });
const genHist = useGenHistory('text-to-image');
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) {
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;
}, mime, 0.92);
}
}
function download() {
render(true);
genHist.add({ title: `文字生成图片: ${text.value.slice(0, 20)}`, detail: `${width.value}×${height.value} ${format.value.toUpperCase()} · ${fontSize.value}px` });
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-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)">
🚀 生成
</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>
<GenHistory tool="text-to-image" label="生成记录" class="mt-10px" />
</div>
</template>
<style lang="less" scoped>
.ti-root {
padding-bottom: 40px;
}
.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>