feat: 多图拼接扩展为6种主流方式 — 横向/纵向长图/九宫格(方格cover裁剪)/瀑布流(保持比例最短列)/交错双列(错落排版)/拍立得(白边框+阴影), 网格类支持2-4列可调, 间距通用
This commit is contained in:
1 parent
739c575bfb
commit
eb13d3c2b8
1 file changed
+152
-10
@@ -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<string, string> = {
|
||||
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<string, string> = { 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(() => {
|
||||
<span class="it-val">{{ wmOpacity }}%</span>
|
||||
</template>
|
||||
<template v-else-if="tab.key === 'stitch'">
|
||||
<label>方向</label>
|
||||
<button class="it-chip" :class="{ on: stitchDir === 'h' }" @click="stitchDir = 'h'">
|
||||
水平
|
||||
<label>拼接方式</label>
|
||||
<button class="it-chip" :class="{ on: stitchMode === 'h' }" @click="stitchMode = 'h'">
|
||||
横向
|
||||
</button>
|
||||
<button class="it-chip" :class="{ on: stitchDir === 'v' }" @click="stitchDir = 'v'">
|
||||
垂直
|
||||
<button class="it-chip" :class="{ on: stitchMode === 'v' }" @click="stitchMode = 'v'">
|
||||
纵向长图
|
||||
</button>
|
||||
<button class="it-chip" :class="{ on: stitchMode === 'grid' }" @click="stitchMode = 'grid'">
|
||||
九宫格
|
||||
</button>
|
||||
<button class="it-chip" :class="{ on: stitchMode === 'masonry' }" @click="stitchMode = 'masonry'">
|
||||
瀑布流
|
||||
</button>
|
||||
<button class="it-chip" :class="{ on: stitchMode === 'interleave' }" @click="stitchMode = 'interleave'">
|
||||
交错双列
|
||||
</button>
|
||||
<button class="it-chip" :class="{ on: stitchMode === 'polaroid' }" @click="stitchMode = 'polaroid'">
|
||||
拍立得
|
||||
</button>
|
||||
<template v-if="stitchMode === 'grid' || stitchMode === 'masonry' || stitchMode === 'polaroid'">
|
||||
<label>列数</label>
|
||||
<button v-for="c in [2, 3, 4]" :key="c" class="it-chip" :class="{ on: stitchCols === c }" @click="stitchCols = c">
|
||||
{{ c }} 列
|
||||
</button>
|
||||
</template>
|
||||
<label>间距</label>
|
||||
<input v-model.number="stitchGap" type="range" min="0" max="40" step="2" class="it-range">
|
||||
<span class="it-val">{{ stitchGap }}px</span>
|
||||
|
||||
Reference in new issue
Block a user