Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

Building a Zero-Dependency Browser Image Splitter & IEEE CRC-32 PKZIP Builder in Pure JavaScript

If you build a browser-based image slicer (for 3×3 grids, panoramic carousels, sprite sheets, or tiled wall posters), two technical problems show up almost immediately: The 1-Pixel Gap / Overlap Bug: Computing tile wi

If you build a browser-based image slicer (for 3×3 grids, panoramic carousels, sprite sheets, or tiled wall posters), two technical problems show up almost immediately:

  1. The 1-Pixel Gap / Overlap Bug: Computing tile widths with naive Math.floor(width / cols) drops trailing pixels or creates misaligned seams between adjacent tiles.
  2. Downloading 9+ Tiles Without a Heavy ZIP Library: Downloading 9 or 16 PNGs one by one triggers Chrome's multi-download popup blocker — yet shipping a 90 KB compression library just to bundle already-compressed PNG/JPEG/WebP tiles wastes CPU time trying to DEFLATE pixels that cannot shrink anyway!

The clean engineering answer is exact integer boundary clamping paired with a 60-line zero-dependency STORE PKZIP builder (compression method = 0) using IEEE 802.3 CRC-32.

Here is the exact open-source architecture behind our free browser-local Split Image Tool on EasyDailyTools.

1. Zero-Gap Integer Grid Boundaries & Poster Bleed Overlap

Instead of calculating tile width first and multiplying (i * tileW), compute the exact integer boundary coordinates [x0, x1, ..., width] once, then define every tile from adjacent pairs (xBounds[c], xBounds[c + 1]). Every pixel in the source image belongs to exactly one tile — zero rounding drift, zero dropped right-edge columns:

function computeTileRects(imgW, imgH, { rows = 3, cols = 3, xCuts, yCuts, overlapPx = 0 } = {}) {
  const xBounds = [0];
  for (let i = 0; i < cols - 1; i++) {
    const frac = xCuts ? xCuts[i] : (i + 1) / cols;
    const px = Math.max(xBounds[xBounds.length - 1] + 1, Math.min(imgW - (cols - 1 - i), Math.round(frac * imgW)));
    xBounds.push(px);
  }
  xBounds.push(imgW);

  const yBounds = [0];
  for (let i = 0; i < rows - 1; i++) {
    const frac = yCuts ? yCuts[i] : (i + 1) / rows;
    const px = Math.max(yBounds[yBounds.length - 1] + 1, Math.min(imgH - (rows - 1 - i), Math.round(frac * imgH)));
    yBounds.push(px);
  }
  yBounds.push(imgH);

  const tiles = [];
  for (let r = 0; r < rows; r++) {
    for (let c = 0; c < cols; c++) {
      const bx0 = xBounds[c], bx1 = xBounds[c + 1];
      const by0 = yBounds[r], by1 = yBounds[r + 1];

      // Optional poster print bleed overlap along shared inner edges
      const x0 = c > 0 ? Math.max(0, bx0 - overlapPx) : bx0;
      const x1 = c < cols - 1 ? Math.min(imgW, bx1 + overlapPx) : bx1;
      const y0 = r > 0 ? Math.max(0, by0 - overlapPx) : by0;
      const y1 = r < rows - 1 ? Math.min(imgH, by1 + overlapPx) : by1;

      tiles.push({ row: r + 1, col: c + 1, x: x0, y: y0, w: x1 - x0, h: y1 - y0 });
    }
  }
  return tiles;
}

Supporting draggable cut guides only requires clamping each dragged fraction between its two neighbors (lower = prev + 0.04, upper = next - 0.04), so users can slide grid lines around faces or text without ever collapsing a tile to zero width.

2. Why STORE PKZIP (method = 0) Beats Heavy ZIP Libraries for Images

PNG, JPEG, and WebP files are already compressed. Running DEFLATE over 12 PNG tiles burns hundreds of milliseconds on mobile CPUs for < 0.3% size reduction.

A valid standard .ZIP archive using STORE (method = 0) only requires three small binary structures:

  1. Local File Header (0x04034b50) + UTF-8 filename + raw tile bytes (per file)
  2. Central Directory Header (0x02014b50) + UTF-8 filename (per file)
  3. End of Central Directory (0x06054b50) (22 bytes)

Plus a 10-line IEEE 802.3 CRC-32 checksum table:

const CRC_TABLE = new Uint32Array(256);
for (let n = 0; n < 256; n++) {
  let c = n;
  for (let k = 0; k < 8; k++) c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1);
  CRC_TABLE[n] = c >>> 0;
}

function crc32Bytes(bytes) {
  let crc = 0xffffffff;
  for (let i = 0; i < bytes.length; i++) crc = CRC_TABLE[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
  return (crc ^ 0xffffffff) >>> 0;
}

function writeU16LE(b, o, v) { b[o] = v & 0xff; b[o + 1] = (v >>> 8) & 0xff; }
function writeU32LE(b, o, v) {
  b[o] = v & 0xff; b[o + 1] = (v >>> 8) & 0xff;
  b[o + 2] = (v >>> 16) & 0xff; b[o + 3] = (v >>> 24) & 0xff;
}

function buildStoreZipUint8Array(entries) {
  const enc = new TextEncoder();
  const items = entries.map((e) => ({
    name: enc.encode(e.name),
    data: e.data,
    crc: crc32Bytes(e.data)
  }));

  let localSize = 0, centralSize = 0;
  for (const it of items) {
    localSize += 30 + it.name.length + it.data.length;
    centralSize += 46 + it.name.length;
  }

  const out = new Uint8Array(localSize + centralSize + 22);
  let off = 0;
  const offsets = [];

  for (const it of items) {
    offsets.push(off);
    writeU32LE(out, off, 0x04034b50);      // Local file header signature
    writeU16LE(out, off + 4, 20);          // Version needed
    writeU16LE(out, off + 6, 0x0800);      // UTF-8 filename flag
    writeU16LE(out, off + 8, 0);           // STORE (uncompressed)
    writeU32LE(out, off + 14, it.crc);
    writeU32LE(out, off + 18, it.data.length);
    writeU32LE(out, off + 22, it.data.length);
    writeU16LE(out, off + 26, it.name.length);
    out.set(it.name, off + 30);
    out.set(it.data, off + 30 + it.name.length);
    off += 30 + it.name.length + it.data.length;
  }

  const centralStart = off;
  items.forEach((it, idx) => {
    writeU32LE(out, off, 0x02014b50);      // Central directory signature
    writeU16LE(out, off + 4, 20);
    writeU16LE(out, off + 6, 20);
    writeU16LE(out, off + 8, 0x0800);
    writeU16LE(out, off + 10, 0);
    writeU32LE(out, off + 16, it.crc);
    writeU32LE(out, off + 20, it.data.length);
    writeU32LE(out, off + 24, it.data.length);
    writeU16LE(out, off + 28, it.name.length);
    writeU32LE(out, off + 42, offsets[idx]);
    out.set(it.name, off + 46);
    off += 46 + it.name.length;
  });

  // End of Central Directory (22 bytes)
  writeU32LE(out, off, 0x06054b50);
  writeU16LE(out, off + 8, items.length);
  writeU16LE(out, off + 10, items.length);
  writeU32LE(out, off + 12, off - centralStart);
  writeU32LE(out, off + 16, centralStart);
  return out;
}

Wrapping the returned Uint8Array in new Blob([zipBytes], { type: 'application/zip' }) produces a standard .ZIP that opens natively on Windows Explorer, macOS Finder, Linux unzip, and iOS/Android Files — with zero dependencies and < 2 ms build time!

Practical Tips & Live Tools

If you are cutting images for multi-panel carousels, grid galleries, or taped multi-sheet posters, check out our Grid, Carousel & Poster Splitting Guide.

We use this exact pure-JS engine architecture across our browser-local Image Tools Hub, including:

Try It, Watch the Demo & Grab the Code

Do you still pull in a full ZIP library on the frontend when bundling already-compressed media, or do you write a tiny STORE PKZIP packager too? Let me know below!

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.