nomid2 commited on
Commit
5d5f4dc
·
verified ·
1 Parent(s): 6c9fd31

Upload imageSlicer.js

Browse files
Files changed (1) hide show
  1. src/lib/imageSlicer.js +26 -12
src/lib/imageSlicer.js CHANGED
@@ -55,15 +55,16 @@ class ImageSlicer {
55
 
56
  if (isVerticalLong) {
57
  // 垂直长图:沿高度方向裁切
58
- const rows = Math.ceil(height / this.maxSize)
59
- const cols = 1 // 垂直长图通常不需要水平裁切
60
-
61
- // 增加重叠区域以保持内容连续性
62
  const enhancedOverlap = Math.min(this.overlapPixels * 2, this.maxSize * 0.1)
63
- const sliceHeight = Math.min(this.maxSize, height)
64
  const stepY = Math.max(1, sliceHeight - enhancedOverlap)
65
 
 
 
 
 
66
  console.log(`[${new Date().toISOString()}] 📏 垂直长图策略: ${rows}行x${cols}列, 重叠=${enhancedOverlap}px`)
 
67
 
68
  return {
69
  rows,
@@ -77,15 +78,16 @@ class ImageSlicer {
77
  }
78
  } else {
79
  // 水平长图:沿宽度方向裁切
80
- const cols = Math.ceil(width / this.maxSize)
81
- const rows = 1 // 水平长图通常不需要垂直裁切
82
-
83
- // 增加重叠区域以保持内容连续性
84
  const enhancedOverlap = Math.min(this.overlapPixels * 2, this.maxSize * 0.1)
85
- const sliceWidth = Math.min(this.maxSize, width)
86
  const stepX = Math.max(1, sliceWidth - enhancedOverlap)
87
 
 
 
 
 
88
  console.log(`[${new Date().toISOString()}] 📏 水平长图策略: ${rows}行x${cols}列, 重叠=${enhancedOverlap}px`)
 
89
 
90
  return {
91
  rows,
@@ -182,7 +184,19 @@ class ImageSlicer {
182
  // 确保不超出图片边界
183
  const actualWidth = Math.min(grid.sliceWidth, metadata.width - x)
184
  const actualHeight = Math.min(grid.sliceHeight, metadata.height - y)
185
-
 
 
 
 
 
 
 
 
 
 
 
 
186
  // 裁切图片并转换为JPEG格式(更好的兼容性)
187
  const sliceBuffer = await image
188
  .extract({
@@ -210,7 +224,7 @@ class ImageSlicer {
210
  coordinates: { x, y }
211
  })
212
 
213
- console.log(`[${new Date().toISOString()}] 🔪 生成切片 ${sliceIndex + 1}/${grid.totalSlices}: ${actualWidth}x${actualHeight} at (${x},${y})`)
214
  }
215
  }
216
 
 
55
 
56
  if (isVerticalLong) {
57
  // 垂直长图:沿高度方向裁切
 
 
 
 
58
  const enhancedOverlap = Math.min(this.overlapPixels * 2, this.maxSize * 0.1)
59
+ const sliceHeight = this.maxSize
60
  const stepY = Math.max(1, sliceHeight - enhancedOverlap)
61
 
62
+ // 重新计算行数,确保覆盖整个图片
63
+ const rows = Math.ceil((height - enhancedOverlap) / stepY)
64
+ const cols = 1 // 垂直长图通常不需要水平裁切
65
+
66
  console.log(`[${new Date().toISOString()}] 📏 垂直长图策略: ${rows}行x${cols}列, 重叠=${enhancedOverlap}px`)
67
+ console.log(`[${new Date().toISOString()}] 📏 切片尺寸: ${width}x${sliceHeight}, 步长=${stepY}`)
68
 
69
  return {
70
  rows,
 
78
  }
79
  } else {
80
  // 水平长图:沿宽度方向裁切
 
 
 
 
81
  const enhancedOverlap = Math.min(this.overlapPixels * 2, this.maxSize * 0.1)
82
+ const sliceWidth = this.maxSize
83
  const stepX = Math.max(1, sliceWidth - enhancedOverlap)
84
 
85
+ // 重新计算列数,确保覆盖整个图片
86
+ const cols = Math.ceil((width - enhancedOverlap) / stepX)
87
+ const rows = 1 // 水平长图通常不需要垂直裁切
88
+
89
  console.log(`[${new Date().toISOString()}] 📏 水平长图策略: ${rows}行x${cols}列, 重叠=${enhancedOverlap}px`)
90
+ console.log(`[${new Date().toISOString()}] 📏 切片尺寸: ${sliceWidth}x${height}, 步长=${stepX}`)
91
 
92
  return {
93
  rows,
 
184
  // 确保不超出图片边界
185
  const actualWidth = Math.min(grid.sliceWidth, metadata.width - x)
186
  const actualHeight = Math.min(grid.sliceHeight, metadata.height - y)
187
+
188
+ // 验证裁切参数
189
+ if (x < 0 || y < 0 || actualWidth <= 0 || actualHeight <= 0 ||
190
+ x + actualWidth > metadata.width || y + actualHeight > metadata.height) {
191
+ console.error(`[${new Date().toISOString()}] ❌ 无效的裁切参数:`)
192
+ console.error(`[${new Date().toISOString()}] 图片尺寸: ${metadata.width}x${metadata.height}`)
193
+ console.error(`[${new Date().toISOString()}] 裁切位置: (${x}, ${y})`)
194
+ console.error(`[${new Date().toISOString()}] 裁切尺寸: ${actualWidth}x${actualHeight}`)
195
+ continue // 跳过这个无效的切片
196
+ }
197
+
198
+ console.log(`[${new Date().toISOString()}] 🔪 生成切片 ${row * grid.cols + col + 1}/${grid.totalSlices}: ${actualWidth}x${actualHeight} at (${x},${y})`)
199
+
200
  // 裁切图片并转换为JPEG格式(更好的兼容性)
201
  const sliceBuffer = await image
202
  .extract({
 
224
  coordinates: { x, y }
225
  })
226
 
227
+
228
  }
229
  }
230