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

+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>