diff --git a/src/tools/image-tools/image-tools.vue b/src/tools/image-tools/image-tools.vue index c03f4d3..e9919b1 100644 --- a/src/tools/image-tools/image-tools.vue +++ b/src/tools/image-tools/image-tools.vue @@ -25,7 +25,7 @@ const TABS: TabDef[] = [ { key: 'qrgen', label: '二维码生成', desc: '把文字/链接生成为二维码图片', noFile: true }, { key: 'qrread', label: '二维码识别', desc: '识别图片中的二维码/条码内容' }, { key: 'watermark', label: '加水印', desc: '为图片添加文字水印(居中或平铺)' }, - { key: 'stitch', label: '多图拼接', desc: '把多张图片水平或垂直拼接为一张', multi: true }, + { key: 'stitch', label: '多图拼接', desc: '横向/纵向长图/九宫格/瀑布流/交错双列/拍立得 6 种方式', multi: true }, ]; const tabKey = useStorage('image-tools:tab', 'compress'); @@ -50,8 +50,9 @@ const qrSize = useStorage('image-tools:qr-size', 300); const wmText = useStorage('image-tools:wm-text', '© IT-TOOLS-PRO'); const wmMode = useStorage<'center' | 'tile'>('image-tools:wm-mode', 'tile'); const wmOpacity = useStorage('image-tools:wm-opacity', 30); -const stitchDir = useStorage<'h' | 'v'>('image-tools:stitch-dir', 'h'); +const stitchMode = useStorage<'h' | 'v' | 'grid' | 'masonry' | 'interleave' | 'polaroid'>('image-tools:stitch-mode', 'h'); const stitchGap = useStorage('image-tools:stitch-gap', 0); +const stitchCols = useStorage('image-tools:stitch-cols', 3); const FILTERS: Record = { none: '原图', @@ -624,7 +625,8 @@ async function doStitch() { } const gap = Math.max(0, stitchGap.value); let canvas: HTMLCanvasElement; - if (stitchDir.value === 'h') { + const mode = stitchMode.value; + if (mode === 'h') { const maxH = Math.max(...imgs.map(i => i.naturalHeight)); const totalW = imgs.reduce((s, i) => s + Math.round(i.naturalWidth * (maxH / i.naturalHeight)), 0) + gap * (imgs.length - 1); canvas = document.createElement('canvas'); @@ -640,7 +642,7 @@ async function doStitch() { x += w + gap; } } - else { + else if (mode === 'v') { const maxW = Math.max(...imgs.map(i => i.naturalWidth)); const totalH = imgs.reduce((s, i) => s + Math.round(i.naturalHeight * (maxW / i.naturalWidth)), 0) + gap * (imgs.length - 1); canvas = document.createElement('canvas'); @@ -656,10 +658,132 @@ async function doStitch() { y += h + gap; } } + else if (mode === 'grid') { + // 九宫格:等宽等高方格,cover 裁剪,微信/微博风格 + const cols = Math.max(2, Math.min(6, stitchCols.value)); + const rows = Math.ceil(imgs.length / cols); + const totalW = Math.min(6000, Math.max(...imgs.map(i => i.naturalWidth))); + const cell = Math.floor((totalW - gap * (cols + 1)) / cols); + const cellH = cell; + canvas = document.createElement('canvas'); + canvas.width = gap + cols * (cell + gap); + canvas.height = gap + rows * (cellH + gap); + const ctx = canvas.getContext('2d')!; + ctx.fillStyle = '#ffffff'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + imgs.forEach((im, i) => { + const col = i % cols; + const row = Math.floor(i / cols); + const x = gap + col * (cell + gap); + const y = gap + row * (cellH + gap); + const scale = Math.max(cell / im.naturalWidth, cellH / im.naturalHeight); + const sw = cell / scale; + const sh = cellH / scale; + const sx = (im.naturalWidth - sw) / 2; + const sy = (im.naturalHeight - sh) / 2; + ctx.drawImage(im, sx, sy, sw, sh, x, y, cell, cellH); + }); + } + else if (mode === 'masonry') { + // 瀑布流:最短列优先,保持原始比例(小红书/Pinterest 风格) + const cols = Math.max(2, Math.min(4, stitchCols.value)); + const totalW = Math.min(6000, Math.max(...imgs.map(i => i.naturalWidth))); + const colW = Math.floor((totalW - gap * (cols + 1)) / cols); + const colH: number[] = new Array(cols).fill(0); + canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d')!; + const placements: { im: HTMLImageElement; x: number; y: number; w: number; h: number }[] = []; + for (const im of imgs) { + let c = 0; + for (let j = 1; j < cols; j++) { + if (colH[j] < colH[c]) { + c = j; + } + } + const h = Math.round((colW * im.naturalHeight) / im.naturalWidth); + placements.push({ im, x: gap + c * (colW + gap), y: gap + colH[c], w: colW, h }); + colH[c] += h + gap; + } + canvas.width = gap + cols * (colW + gap); + canvas.height = gap + Math.max(...colH); + ctx.fillStyle = '#ffffff'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + for (const pl of placements) { + ctx.drawImage(pl.im, pl.x, pl.y, pl.w, pl.h); + } + } + else if (mode === 'interleave') { + // 交错双列:两列上下错落,杂志排版风格 + const totalW = Math.min(6000, Math.max(...imgs.map(i => i.naturalWidth))); + const colW = Math.floor((totalW - gap * 3) / 2); + const left = imgs.filter((_, i) => i % 2 === 0); + const right = imgs.filter((_, i) => i % 2 === 1); + const layout = (arr: HTMLImageElement[]) => { + let y = 0; + return arr.map((im) => { + const h = Math.round((colW * im.naturalHeight) / im.naturalWidth); + const item = { im, y, h }; + y += h + gap; + return item; + }); + }; + const L = layout(left); + const R = layout(right); + const off = L.length && R.length ? Math.round((L[0].h + gap) / 2) : 0; // 右列下移半格 + canvas = document.createElement('canvas'); + canvas.width = totalW; + canvas.height = Math.max(L.length ? L[L.length - 1].y + L[L.length - 1].h : 0, off + (R.length ? R[R.length - 1].y + R[R.length - 1].h : 0)) + gap; + const ctx = canvas.getContext('2d')!; + ctx.fillStyle = '#ffffff'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + for (const it of L) { + ctx.drawImage(it.im, gap, it.y, colW, it.h); + } + for (const it of R) { + ctx.drawImage(it.im, gap * 2 + colW, off + it.y, colW, it.h); + } + } + else { + // 拍立得:白底 + 每图白边框 + 阴影,按列网格排列 + const cols = Math.max(2, Math.min(4, stitchCols.value)); + const rows = Math.ceil(imgs.length / cols); + const totalW = Math.min(6000, Math.max(...imgs.map(i => i.naturalWidth))); + const pad = Math.max(10, gap * 2); + const cellW = Math.floor((totalW - pad * (cols + 1)) / cols); + const photoH = Math.round(cellW * 0.75); // 4:3 照片区 + const bottom = Math.round(pad * 2.4); // 拍立得底部留白 + const cellH = photoH + pad * 2 + bottom - pad; + canvas = document.createElement('canvas'); + canvas.width = pad + cols * (cellW + pad); + canvas.height = pad + rows * (cellH + pad); + const ctx = canvas.getContext('2d')!; + ctx.fillStyle = '#f4f4f0'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + imgs.forEach((im, i) => { + const col = i % cols; + const row = Math.floor(i / cols); + const x = pad + col * (cellW + pad); + const y = pad + row * (cellH + pad); + // 白色相纸卡片 + 阴影 + ctx.save(); + ctx.shadowColor = 'rgba(0,0,0,0.25)'; + ctx.shadowBlur = 10; + ctx.shadowOffsetY = 4; + ctx.fillStyle = '#ffffff'; + ctx.fillRect(x, y, cellW, cellH); + ctx.restore(); + // 照片 contain 居中 + const scale = Math.min((cellW - pad * 0.6) / im.naturalWidth, photoH / im.naturalHeight); + const dw = im.naturalWidth * scale; + const dh = im.naturalHeight * scale; + ctx.drawImage(im, x + (cellW - dw) / 2, y + pad / 2, dw, dh); + }); + } + const modeName: Record = { h: '水平', v: '垂直/长图', grid: '网格九宫格', masonry: '瀑布流', interleave: '交错双列', polaroid: '拍立得' }; const blob = await canvasToBlob(canvas, 'image/jpeg', 92); setOutput(blob, `stitched-${imgs.length}.jpg`, { title: `拼接 ${imgs.length} 张图片`, - detail: `${stitchDir.value === 'h' ? '水平' : '垂直'} · ${canvas.width}×${canvas.height}`, + detail: `${modeName[mode]} · ${canvas.width}×${canvas.height}`, }); resultDim.value = `${canvas.width}×${canvas.height}`; } @@ -847,13 +971,31 @@ onUnmounted(() => { {{ wmOpacity }}%