diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a54cbd..cd5f2c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file. See [standa ## Version 2026.10.16-it-tools-pro +### ✨ 图片编辑升级为 PS 风格图层编辑器 +- **9 工具**:移动/选框/画笔/橡皮/文字/油漆桶(容差可调)/吸管取色/裁剪/抓手,快捷键 V M B E T G I C H,`[ ]` 调笔刷、`0` 适应、`1` 原尺寸 +- **图层面板**:背景/画笔/文字/色块四类图层,显示隐藏、不透明度、上移下移、删除、新建画笔层;文字层改内容/字号/颜色/加粗,色块改底色 +- **历史记录面板**:快照式撤销重做(Ctrl+Z / Ctrl+Y,最多 20 步),点击任意步骤可跳转 +- **画布**:滚轮缩放(3%-3200%)、空格/抓手平移、棋盘格透明背景、画笔光圈预览、选区/裁剪框蚂蚁线 +- **选区操作**:框选后填充/清除选区、Delete 快捷清除;裁剪框选后一键应用(全图层同步裁剪) +- **保留 OCR 强项**:一键识别图中文字 → 点框替换(自动取底色)/只擦除,生成独立图层可再编辑 +- 重构:image-tools.vue 2284→1660 行,编辑器拆分为独立组件 PsEditor.vue;功能数 12 → 13 + ### 🔧 授权系统:通用卡激活传 project_id(适配 CardAuth 新版) - CardAuth 新版支持「通用卡」(不绑定项目的跨项目卡),激活时必须指定项目;relay 激活请求现在固定携带 `project_id`(IT-TOOLS-PRO=6),通用卡可在本站正常激活,专属卡不受影响 - 授权校验/撤销同步改用 `project_id` + 历史项目名(IT-Tools / IT-TOOLS-PRO)双重匹配,项目改名后新旧授权都能正确识别 diff --git a/components.d.ts b/components.d.ts index d34cfd6..c2b603c 100644 --- a/components.d.ts +++ b/components.d.ts @@ -207,6 +207,7 @@ declare module '@vue/runtime-core' { PhoneParserAndFormatter: typeof import('./src/tools/phone-parser-and-formatter/phone-parser-and-formatter.vue')['default'] PinyinConverter: typeof import('./src/tools/pinyin-converter/pinyin-converter.vue')['default'] 'Pricing.page': typeof import('./src/pages/Pricing.page.vue')['default'] + PsEditor: typeof import('./src/tools/image-tools/PsEditor.vue')['default'] QrCodeGenerator: typeof import('./src/tools/qr-code-generator/qr-code-generator.vue')['default'] RandomPortGenerator: typeof import('./src/tools/random-port-generator/random-port-generator.vue')['default'] RegexMemo: typeof import('./src/tools/regex-memo/regex-memo.vue')['default'] diff --git a/locales/en.yml b/locales/en.yml index 0078de7..7b6c7e4 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -310,7 +310,7 @@ tools: image-tools: title: Image tools - description: "In-browser image toolbox: compress, convert, resize, crop, rotate, filters, mosaic/redaction, image editing (OCR text replacement / add text), EXIF viewer, QR generate/scan, watermark and stitching. Images never leave your device." + description: "In-browser image toolbox: compress, convert, resize, crop, rotate, filters, mosaic/redaction, Photoshop-style editor (layers, brush, text, selection, OCR text replacement), EXIF viewer, QR generate/scan, watermark and stitching. Images never leave your device." pdf-tools: title: PDF tools diff --git a/locales/zh.yml b/locales/zh.yml index be45ca1..fa8c174 100644 --- a/locales/zh.yml +++ b/locales/zh.yml @@ -307,7 +307,7 @@ tools: image-tools: title: 图片工具 - description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、图片编辑(OCR 改文字/加文字)、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。 + description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、PS 风格图片编辑(图层/画笔/文字/选区/OCR 改字)、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。 pdf-tools: title: PDF工具 diff --git a/src/pages/About.vue b/src/pages/About.vue index 355b276..cd8c087 100644 --- a/src/pages/About.vue +++ b/src/pages/About.vue @@ -53,7 +53,7 @@ const groups: { title: string; items: Item[] }[] = [ { name: '多图拼接', icon: '拼', desc: '横向/纵向长图/九宫格/瀑布流/交错双列/拍立得 6 种方式,网格类 2-4 列可调,间距可调', tag: '改造' }, { name: 'JSON 美化和格式化', icon: 'J', desc: '双栏专业工作台:左侧行号编辑,右侧树视图/语法高亮切换,含 Dict/Get参数/XML/CSV 等 7 种转换', tag: '改造' }, { name: 'PDF工具', icon: 'P', desc: '合并/拆分/旋转/重排/压缩/加密/解密/水印/提取文本/页码/转图片,11 功能全程浏览器本地处理,需授权使用', tag: '授权' }, - { name: '图片工具', icon: '图', desc: '压缩/转换/尺寸/裁剪/旋转/滤镜/打码/图片编辑(OCR改文字+加文字)/EXIF/二维码生成识别/水印/拼接,13 功能本地处理,需授权使用', tag: '授权' }, + { name: '图片工具', icon: '图', desc: '压缩/转换/尺寸/裁剪/旋转/滤镜/打码/PS 风格编辑器(图层+画笔+文字+选区+历史记录+OCR 改字)/EXIF/二维码生成识别/水印/拼接,13 功能本地处理,需授权使用', tag: '授权' }, ], }, { diff --git a/src/tools/image-tools/PsEditor.vue b/src/tools/image-tools/PsEditor.vue new file mode 100644 index 0000000..8505ff4 --- /dev/null +++ b/src/tools/image-tools/PsEditor.vue @@ -0,0 +1,1715 @@ + + + + + + + + + {{ t.icon }} + + + ↶ + ↷ + + + + 大小 + + {{ brushSize }}px + + 颜色 + + + + + + 字号 + + + 加粗 + + + 容差 + + {{ tolerance }} + 颜色 + + + + 填充选区 + 清除选区 + 取消选区 + + + ✂ 应用裁剪 {{ dragRect.w }}×{{ dragRect.h }} + 拖拽框选裁剪区域 + + + {{ ocrBusy ? '识别中…' : '🔍 识别图中文字' }} + + + + ✓ 替换 + 只擦除 + + + 适应 + 100% + {{ Math.round(zoom * 100) }}% + + + + + + + + + + + + 图层 + + + + + + {{ l.visible ? '👁' : '🚫' }} + {{ l.type === 'text' ? 'T' : l.type === 'fill' ? '■' : l.type === 'paint' ? '🖌' : '🖼' }} + {{ l.name }} + + ▲ + ▼ + ✕ + + + + + + 属性 + + 不透明度 + + {{ activeLayer.opacity }}% + + + + 内容 + + + + 字号 + + + 加粗 + + + + + 底色 + + + + + + 历史记录 + + + {{ h.label }} + + + + + + + + + {{ docW }}×{{ docH }}px · 图层 {{ layers.length }} · Ctrl+Z 撤销 + + {{ statusHint }} + 编辑完成后点下方「执行」导出成品 + + + + + diff --git a/src/tools/image-tools/image-tools.vue b/src/tools/image-tools/image-tools.vue index 1fe3246..168c6f3 100644 --- a/src/tools/image-tools/image-tools.vue +++ b/src/tools/image-tools/image-tools.vue @@ -8,6 +8,7 @@ import { useStorage } from '@vueuse/core'; import GenHistory from '@/tools/gen-history/GenHistory.vue'; import { useGenHistory } from '@/tools/gen-history/gen-history'; import { getMachineId, licenseHeaders } from '@/tools/license/license'; +import PsEditor from './PsEditor.vue'; prepareZXingModule({ overrideWasmUrl: wasmUrl }); const genHist = useGenHistory('image-tools'); @@ -22,7 +23,7 @@ const TABS: TabDef[] = [ { key: 'rotate', label: '旋转', desc: '旋转或翻转图片' }, { key: 'filter', label: '滤镜', desc: '为图片应用滤镜效果(灰度/复古/模糊等)' }, { key: 'mosaic', label: '打码', desc: '在图片上拖拽框选区域,马赛克或涂黑打码' }, - { key: 'edit', label: '图片编辑', desc: '修改图中的文字(OCR 自动定位原文字,覆盖底色后替换)或点击图片任意位置添加新文字,可拖动调整' }, + { key: 'edit', label: '图片编辑', desc: 'PS 风格图层编辑器:画笔/橡皮/文字/选区/油漆桶/吸管/裁剪,支持缩放平移、图层面板与历史记录;OCR 一键替换图中文字' }, { key: 'exif', label: 'EXIF 查看', desc: '查看拍摄设备、时间、参数等 EXIF 信息' }, { key: 'qrgen', label: '二维码生成', desc: '把文字/链接生成为二维码图片', noFile: true }, { key: 'qrread', label: '二维码识别', desc: '识别图片中的二维码/条码内容' }, @@ -133,7 +134,6 @@ function loadMainImage() { qrResult.value = ''; decodeText.value = ''; mosaicBox.value = null; - resetEditState(); mainImg.value = null; const f = files.value[0]; if (!f || tab.value.noFile) { @@ -145,7 +145,6 @@ function loadMainImage() { mainImg.value = img; nextTick(() => { drawMosaicCanvas(); - drawEditCanvas(); }); }; img.src = url; @@ -277,463 +276,24 @@ function mosaicPos(e: MouseEvent) { return { x: e.clientX - r.left, y: e.clientY - r.top }; } -// ---------- 图片编辑(改文字 + 加文字) ---------- -// 坐标约定:covers / layers / ocrHits 全部存原图像素坐标,渲染时乘 displayScale -interface EditCover { id: string; x: number; y: number; w: number; h: number; color: string } -interface EditLayer { id: string; text: string; x: number; y: number; size: number; color: string; bold: boolean } -interface OcrHit { id: string; text: string; score: number; box: number[][]; x: number; y: number; w: number; h: number; used: boolean } +// ---------- 图片编辑(PS 风格编辑器,组件内自带工具栏/图层/历史) ---------- +const psEditor = ref<{ exportDoc: () => Promise<{ blob: Blob; detail: string; w: number; h: number }> } | null>(null); -const editCanvas = ref(); -const editMode = useStorage<'ocr' | 'add'>('image-tools:edit-mode', 'ocr'); -const editCovers = ref([]); -const editLayers = ref([]); -const ocrHits = ref([]); -const ocrBusy = ref(false); -const ocrDone = ref(false); -const selLayerId = ref(null); -const selOcrId = ref(null); -const replaceText = ref(''); -const coverColor = ref('#ffffff'); -const coverAuto = ref(true); -const addText = useStorage('image-tools:edit-add-text', '新文字'); -const addSize = useStorage('image-tools:edit-add-size', 32); -const addColor = useStorage('image-tools:edit-add-color', '#d03050'); -const addBold = useStorage('image-tools:edit-add-bold', false); -let editDragging = false; -let editDragLayer: EditLayer | null = null; -let editDragOff = { x: 0, y: 0 }; -let editDrawing = false; -let editNewBox: { x1: number; y1: number; x2: number; y2: number } | null = null; - -const selLayer = computed(() => editLayers.value.find(l => l.id === selLayerId.value) ?? null); -const unusedOcr = computed(() => ocrHits.value.filter(h => !h.used)); - -function editPos(e: MouseEvent) { - const c = editCanvas.value!; - const r = c.getBoundingClientRect(); - return { x: e.clientX - r.left, y: e.clientY - r.top }; -} - -// 采样文字框外圈像素的中位色,作为覆盖底色(白底/灰底都自然) -function sampleBoxColor(box: { x: number; y: number; w: number; h: number }): string { - const img = mainImg.value; - if (!img) { - return coverColor.value; - } - try { - const c = document.createElement('canvas'); - c.width = img.naturalWidth; - c.height = img.naturalHeight; - const ctx = c.getContext('2d', { willReadFrequently: true })!; - ctx.drawImage(img, 0, 0); - const pad = Math.max(3, Math.round(Math.min(box.w, box.h) * 0.35)); - const sx = Math.max(0, Math.round(box.x - pad)); - const sy = Math.max(0, Math.round(box.y - pad)); - const sw = Math.min(c.width - sx, Math.round(box.w + pad * 2)); - const sh = Math.min(c.height - sy, Math.round(box.h + pad * 2)); - if (sw <= 0 || sh <= 0) { - return coverColor.value; - } - const d = ctx.getImageData(sx, sy, sw, sh).data; - const rs: number[] = []; - const gs: number[] = []; - const bs: number[] = []; - for (let py = 0; py < sh; py++) { - for (let px = 0; px < sw; px++) { - const ix = sx + px; - const iy = sy + py; - // 跳过框内(文字本身),只取外圈 - if (ix >= box.x && ix < box.x + box.w && iy >= box.y && iy < box.y + box.h) { - continue; - } - const i = (py * sw + px) * 4; - rs.push(d[i]); - gs.push(d[i + 1]); - bs.push(d[i + 2]); - } - } - if (!rs.length) { - return coverColor.value; - } - const med = (a: number[]) => { - a.sort((p, q) => p - q); - return a[a.length >> 1]; - }; - return `rgb(${med(rs)}, ${med(gs)}, ${med(bs)})`; - } - catch { - return coverColor.value; - } -} - -async function runEditOcr() { - const f = files.value[0]; - if (!f) { - errorMsg.value = '请先选择图片'; - return; - } - ocrBusy.value = true; - errorMsg.value = ''; - try { - const form = new FormData(); - form.append('image_file', f, f.name || 'image.png'); - const res = await fetch('/api/ocr', { method: 'POST', body: form, headers: licenseHeaders('image-tools') }); - if (!res.ok) { - throw new Error(res.status === 403 ? 'OCR 需要授权或试用' : `OCR 服务 HTTP ${res.status}`); - } - const data = await res.json().catch(() => ({})); - const hits: OcrHit[] = []; - for (const k of Object.keys(data)) { - const pts = data[k]?.dt_boxes; - if (!Array.isArray(pts) || pts.length < 4) { - continue; - } - const text = String(data[k]?.rec_txt ?? '').trim(); - if (!text) { - continue; - } - const xs = pts.map((p: any) => Number(p[0])); - const ys = pts.map((p: any) => Number(p[1])); - const x = Math.min(...xs); - const y = Math.min(...ys); - hits.push({ - id: `ocr-${k}`, - text, - score: Number(data[k]?.score ?? 0), - box: pts, - x, - y, - w: Math.max(...xs) - x, - h: Math.max(...ys) - y, - used: false, - }); - } - ocrHits.value = hits; - ocrDone.value = true; - if (!hits.length) { - errorMsg.value = '未识别到文字,可截图更清晰后重试,或用「添加文字」模式'; - } - else { - statusMsg.value = `识别到 ${hits.length} 处文字,点击虚线框或列表项即可替换`; - genHist.add({ title: `编辑 OCR:${f.name}`, detail: `${hits.length} 处文字` }); - } - nextTick(drawEditCanvas); - } - catch (e: any) { - errorMsg.value = `识别失败:${e?.message ?? e}`; - ocrDone.value = false; - nextTick(drawEditCanvas); - } - finally { - ocrBusy.value = false; - } -} - -// 选中一处 OCR 文字:盖底色 + 在原位创建可编辑文字图层(内容预填原文) -function applyOcrReplace(hit: OcrHit) { - if (hit.used) { - return; - } - const color = coverAuto.value ? sampleBoxColor({ x: hit.x, y: hit.y, w: hit.w, h: hit.h }) : coverColor.value; - const padX = Math.max(1, Math.round(hit.w * 0.02)); - const padY = Math.max(1, Math.round(hit.h * 0.08)); - editCovers.value.push({ - id: `c${Date.now()}${Math.random().toString(36).slice(2, 6)}`, - x: Math.max(0, hit.x - padX), - y: Math.max(0, hit.y - padY), - w: hit.w + padX * 2, - h: hit.h + padY * 2, - color, - }); - const layer: EditLayer = { - id: `l${Date.now()}${Math.random().toString(36).slice(2, 6)}`, - text: replaceText.value.trim() || hit.text, - x: hit.x, - y: hit.y + hit.h * 0.06, - size: Math.max(10, Math.round(hit.h * 0.82)), - color: '#000000', - bold: false, - }; - editLayers.value.push(layer); - hit.used = true; - selLayerId.value = layer.id; - selOcrId.value = null; - replaceText.value = ''; - coverColor.value = color.startsWith('rgb') ? '#ffffff' : color; - nextTick(drawEditCanvas); -} - -// 仅擦除(盖底色,不加字) -function applyOcrErase(hit: OcrHit) { - if (hit.used) { - return; - } - const color = coverAuto.value ? sampleBoxColor({ x: hit.x, y: hit.y, w: hit.w, h: hit.h }) : coverColor.value; - const padX = Math.max(1, Math.round(hit.w * 0.02)); - const padY = Math.max(1, Math.round(hit.h * 0.08)); - editCovers.value.push({ - id: `c${Date.now()}${Math.random().toString(36).slice(2, 6)}`, - x: Math.max(0, hit.x - padX), - y: Math.max(0, hit.y - padY), - w: hit.w + padX * 2, - h: hit.h + padY * 2, - color, - }); - hit.used = true; - selOcrId.value = null; - nextTick(drawEditCanvas); -} - -function removeCover(id: string) { - editCovers.value = editCovers.value.filter(c => c.id !== id); - drawEditCanvas(); -} - -function removeLayer(id: string) { - editLayers.value = editLayers.value.filter(l => l.id !== id); - if (selLayerId.value === id) { - selLayerId.value = null; - } - drawEditCanvas(); -} - -function resetEditState() { - editCovers.value = []; - editLayers.value = []; - ocrHits.value = []; - ocrDone.value = false; - selLayerId.value = null; - selOcrId.value = null; - replaceText.value = ''; - editNewBox = null; -} - -function drawEditCanvas() { - const c = editCanvas.value; - const img = mainImg.value; - if (!c || !img) { - return; - } - const maxW = 640; - const scale = Math.min(1, maxW / img.naturalWidth); - displayScale.value = scale; - c.width = Math.round(img.naturalWidth * scale); - c.height = Math.round(img.naturalHeight * scale); - const ctx = c.getContext('2d')!; - ctx.drawImage(img, 0, 0, c.width, c.height); - // 1) 底色覆盖(擦除原文字) - for (const cv of editCovers.value) { - ctx.fillStyle = cv.color; - ctx.fillRect(cv.x * scale, cv.y * scale, cv.w * scale, cv.h * scale); - } - // 2) 未处理的 OCR 虚线框 - for (const h of ocrHits.value) { - if (h.used) { - continue; - } - const selected = h.id === selOcrId.value; - ctx.strokeStyle = selected ? '#2563eb' : '#d03050'; - ctx.lineWidth = selected ? 2.5 : 1.5; - ctx.setLineDash([6, 4]); - ctx.strokeRect(h.x * scale, h.y * scale, h.w * scale, h.h * scale); - ctx.setLineDash([]); - // 框顶小标签 - const label = h.text.length > 12 ? `${h.text.slice(0, 12)}…` : h.text; - ctx.font = `${Math.max(10, Math.round(11 / Math.max(scale, 0.4)))}px sans-serif`; - const tw = ctx.measureText(label).width + 8; - const ly = h.y * scale - 16; - ctx.fillStyle = selected ? '#2563eb' : 'rgba(208,48,80,0.92)'; - ctx.fillRect(h.x * scale, ly < 0 ? h.y * scale : ly, tw, 15); - ctx.fillStyle = '#fff'; - ctx.textBaseline = 'middle'; - ctx.fillText(label, h.x * scale + 4, (ly < 0 ? h.y * scale : ly) + 8); - } - // 3) 文字图层 - for (const l of editLayers.value) { - ctx.font = `${l.bold ? 'bold ' : ''}${l.size * scale}px "Microsoft YaHei","PingFang SC","Noto Sans CJK SC",sans-serif`; - ctx.fillStyle = l.color; - ctx.textBaseline = 'top'; - ctx.fillText(l.text, l.x * scale, l.y * scale); - if (l.id === selLayerId.value) { - const w = ctx.measureText(l.text).width; - ctx.strokeStyle = '#18a058'; - ctx.lineWidth = 1.5; - ctx.setLineDash([5, 3]); - ctx.strokeRect(l.x * scale - 4, l.y * scale - 3, w + 8, l.size * scale * 1.25 + 6); - ctx.setLineDash([]); - } - } - // 4) 拖拽框选预览(add 模式手动框选覆盖) - if (editNewBox) { - const b = editNewBox; - ctx.strokeStyle = '#d03050'; - ctx.lineWidth = 2; - ctx.setLineDash([6, 4]); - ctx.strokeRect(Math.min(b.x1, b.x2), Math.min(b.y1, b.y2), Math.abs(b.x2 - b.x1), Math.abs(b.y2 - b.y1)); - ctx.setLineDash([]); - } -} - -function layerHitTest(ix: number, iy: number): EditLayer | null { - const c = editCanvas.value; - if (!c) { - return null; - } - const ctx = c.getContext('2d')!; - for (let i = editLayers.value.length - 1; i >= 0; i--) { - const l = editLayers.value[i]; - ctx.font = `${l.bold ? 'bold ' : ''}${l.size}px "Microsoft YaHei","PingFang SC",sans-serif`; - const w = ctx.measureText(l.text).width; - const h = l.size * 1.25; - if (ix >= l.x - 6 && ix <= l.x + w + 6 && iy >= l.y - 6 && iy <= l.y + h + 6) { - return l; - } - } - return null; -} - -function ocrHitTest(ix: number, iy: number): OcrHit | null { - for (const h of ocrHits.value) { - if (!h.used && ix >= h.x - 3 && ix <= h.x + h.w + 3 && iy >= h.y - 3 && iy <= h.y + h.h + 3) { - return h; - } - } - return null; -} - -function onEditDown(e: MouseEvent) { - if (!editCanvas.value) { - return; - } - const p = editPos(e); - const s = displayScale.value; - const ix = p.x / s; - const iy = p.y / s; - // 1) 点到已有文字图层 → 选中并拖动 - const lh = layerHitTest(ix, iy); - if (lh) { - selLayerId.value = lh.id; - editDragging = true; - editDragLayer = lh; - editDragOff = { x: ix - lh.x, y: iy - lh.y }; - drawEditCanvas(); - return; - } - // 2) OCR 模式:点到虚线框 → 替换 - if (editMode.value === 'ocr') { - const oh = ocrHitTest(ix, iy); - if (oh) { - selOcrId.value = oh.id; - replaceText.value = ''; - drawEditCanvas(); - return; - } - return; - } - // 3) add 模式空白处:开始框选(松手后按框擦除)或直接加字 - if (editMode.value === 'add') { - editDrawing = true; - editNewBox = { x1: p.x, y1: p.y, x2: p.x, y2: p.y }; - } -} - -function onEditMove(e: MouseEvent) { - if (!editCanvas.value) { - return; - } - const p = editPos(e); - const s = displayScale.value; - if (editDragging && editDragLayer) { - editDragLayer.x = Math.max(0, Math.min(editCanvas.value.width / s, p.x / s - editDragOff.x)); - editDragLayer.y = Math.max(0, Math.min(editCanvas.value.height / s, p.y / s - editDragOff.y)); - drawEditCanvas(); - return; - } - if (editDrawing && editNewBox) { - editNewBox.x2 = p.x; - editNewBox.y2 = p.y; - drawEditCanvas(); - } -} - -function onEditUp() { - if (editDragging) { - editDragging = false; - editDragLayer = null; - return; - } - if (editDrawing && editNewBox) { - const b = editNewBox; - editDrawing = false; - editNewBox = null; - const s = displayScale.value; - const x = Math.round(Math.min(b.x1, b.x2) / s); - const y = Math.round(Math.min(b.y1, b.y2) / s); - const w = Math.round(Math.abs(b.x2 - b.x1) / s); - const h = Math.round(Math.abs(b.y2 - b.y1) / s); - if (w >= 6 && h >= 6) { - editCovers.value.push({ - id: `c${Date.now()}${Math.random().toString(36).slice(2, 6)}`, - x, - y, - w, - h, - color: coverAuto.value ? sampleBoxColor({ x, y, w, h }) : coverColor.value, - }); - } - drawEditCanvas(); - } -} - -function addTextLayer() { - const img = mainImg.value; - const text = addText.value.trim(); - if (!img) { - return; - } - if (!text) { - errorMsg.value = '请输入要添加的文字'; - return; - } - errorMsg.value = ''; - const layer: EditLayer = { - id: `l${Date.now()}${Math.random().toString(36).slice(2, 6)}`, - text, - x: Math.round(img.naturalWidth * 0.08), - y: Math.round(img.naturalHeight * 0.08), - size: addSize.value, - color: addColor.value, - bold: addBold.value, - }; - editLayers.value.push(layer); - selLayerId.value = layer.id; - nextTick(drawEditCanvas); +function onPsError(msg: string) { + errorMsg.value = msg; } async function doEdit() { - const img = mainImg.value!; - if (!editCovers.value.length && !editLayers.value.length) { - throw new Error('还没有任何修改:先「识别文字」点框替换,或在「添加文字」模式下框选/加字'); + const ed = psEditor.value; + if (!ed) { + throw new Error('编辑器未就绪,请重新选择图片'); } - const c = loadCanvasFromImg(img); - const ctx = c.getContext('2d')!; - for (const cv of editCovers.value) { - ctx.fillStyle = cv.color; - ctx.fillRect(cv.x, cv.y, cv.w, cv.h); - } - for (const l of editLayers.value) { - ctx.font = `${l.bold ? 'bold ' : ''}${l.size}px "Microsoft YaHei","PingFang SC","Noto Sans CJK SC","Source Han Sans SC",sans-serif`; - ctx.fillStyle = l.color; - ctx.textBaseline = 'top'; - ctx.fillText(l.text, l.x, l.y); - } - const keepPng = files.value[0].type === 'image/png'; - const blob = await canvasToBlob(c, keepPng ? 'image/png' : 'image/jpeg', 94); + const { blob, detail, w, h } = await ed.exportDoc(); setOutput(blob, `${baseName(files.value[0])}-edited.${extOf(blob.type)}`, { title: `编辑 ${files.value[0].name}`, - detail: `擦除 ${editCovers.value.length} 处 · 文字 ${editLayers.value.length} 个`, + detail, }); - resultDim.value = `${c.width}×${c.height}`; + resultDim.value = `${w}×${h}`; } @@ -1326,7 +886,6 @@ function fit() { watch(tabKey, () => nextTick(() => { drawMosaicCanvas(); - drawEditCanvas(); })); onMounted(() => { @@ -1395,25 +954,7 @@ onUnmounted(() => { - - - - - - 点击虚线框选中文字 → 在下方输入新文字 → 替换(自动取底色覆盖原字) - - - 在图片上拖拽框选要涂掉的区域;点「添加文字」放新文字,拖动文字可调整位置 - - + @@ -1494,84 +1035,7 @@ onUnmounted(() => { 在左侧图片上按住拖拽框选打码区域 - 模式 - - 🔍 改图中文字 - - - ✏️ 涂改/加字 - - - {{ ocrBusy ? '识别中…' : ocrDone ? '重新识别文字' : '识别图中文字' }} - - 待处理 {{ unusedOcr.length }} 处 - - - 新文字 - h.id === selOcrId)!)" - > - h.id === selOcrId)!)"> - ✓ 替换 - - h.id === selOcrId)!)"> - 只擦除 - - - 全部文字已处理 ✓ - - - 文字 - - - + 添加文字 - - 字号 - - 颜色 - - 加粗 - 底色 - - 自动取图 - - - 手动 - - - - - - - 图层 - - - - - - ✕ - - - ⬤ 擦除 {{ Math.round(c.w) }}×{{ Math.round(c.h) }} - - ✕ - - - + 编辑器内含:工具栏(移动/选框/画笔/橡皮/文字/油漆桶/吸管/裁剪/抓手)、图层面板、历史记录;Ctrl+Z 撤销、滚轮缩放、空格拖动。编辑完成后点下方「执行」导出成品。 尺寸 @@ -1935,23 +1399,7 @@ onUnmounted(() => { } } -// ---------- 图片编辑 ---------- -.edit-ocr-mode { - cursor: pointer; -} - -.edit-add-mode { - cursor: crosshair; -} - -.it-edit-hint { - width: 100%; - font-size: 12px; - color: #2563eb; - opacity: 0.9; - line-height: 1.5; - padding-top: 4px; -} +// ---------- 图片编辑(组件内样式见 PsEditor.vue) ---------- .it-colorswatch { width: 34px; @@ -1963,79 +1411,6 @@ onUnmounted(() => { cursor: pointer; } -.it-edit-layers { - width: 100%; - display: flex; - flex-direction: column; - gap: 5px; - max-height: 168px; - overflow-y: auto; - border: 1px solid rgba(128, 128, 128, 0.22); - border-radius: 8px; - padding: 7px; - box-sizing: border-box; - - .it-edit-layer { - display: flex; - align-items: center; - gap: 7px; - background: rgba(128, 128, 128, 0.05); - border: 1px solid transparent; - border-radius: 6px; - padding: 4px 7px; - - &.on { - border-color: #18a058; - background: rgba(24, 160, 88, 0.08); - } - - &.cover { - opacity: 0.85; - } - - .it-edit-lt { - flex: 1; - min-width: 0; - border: 1px solid rgba(128, 128, 128, 0.3); - border-radius: 5px; - padding: 3px 7px; - font-size: 12.5px; - outline: none; - - &:focus { - border-color: #18a058; - } - } - - .it-edit-ls { - width: 54px; - border: 1px solid rgba(128, 128, 128, 0.3); - border-radius: 5px; - padding: 3px 5px; - font-size: 12px; - outline: none; - } - - .it-edit-ct { - flex: 1; - font-size: 12px; - opacity: 0.65; - } - - .it-edit-del { - color: #999; - text-decoration: none; - font-size: 13px; - padding: 0 3px; - flex-shrink: 0; - - &:hover { - color: #d03050; - } - } - } -} - .it-qrbox { flex: 1; min-height: 120px; diff --git a/src/tools/image-tools/index.ts b/src/tools/image-tools/index.ts index 9ec3c00..e349e10 100644 --- a/src/tools/image-tools/index.ts +++ b/src/tools/image-tools/index.ts @@ -6,7 +6,7 @@ export const tool = defineTool({ name: translate('tools.image-tools.title'), path: '/image-tools', description: translate('tools.image-tools.description'), - keywords: ['image', 'compress', 'convert', 'resize', 'crop', 'rotate', 'filter', 'mosaic', 'exif', 'watermark', 'stitch', 'edit', 'text', 'replace', '图片', '压缩', '转换', '裁剪', '旋转', '滤镜', '打码', '水印', '拼接', '编辑', '改文字', '文字替换', '加文字'], + keywords: ['image', 'compress', 'convert', 'resize', 'crop', 'rotate', 'filter', 'mosaic', 'exif', 'watermark', 'stitch', 'edit', 'text', 'replace', 'photoshop', 'layer', 'brush', 'history', '图片', '压缩', '转换', '裁剪', '旋转', '滤镜', '打码', '水印', '拼接', '编辑', '改文字', '文字替换', '加文字', '画笔', '图层', '历史记录'], component: () => import('./image-tools.vue'), icon: Photo, });