<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[EasyDailyTools]]></title><description><![CDATA[EasyDailyTools]]></description><link>https://easydailytools.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 30 Nov 2026 23:56:23 GMT</lastBuildDate><atom:link href="https://easydailytools.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Convert 72 DPI to 300 DPI Losslessly in Pure JavaScript (Binary JPEG JFIF/EXIF & PNG pHYs Patching)]]></title><description><![CDATA[Patch JPEG JFIF APP0, EXIF TIFF IFD0, and PNG pHYs CRC-32 headers directly inside a Uint8Array with 0% pixel loss.]]></description><link>https://easydailytools.hashnode.dev/how-to-convert-72-dpi-to-300-dpi-losslessly-in-pure-javascript-binary-jpeg-jfif-exif-png-phys-patching</link><guid isPermaLink="true">https://easydailytools.hashnode.dev/how-to-convert-72-dpi-to-300-dpi-losslessly-in-pure-javascript-binary-jpeg-jfif-exif-png-phys-patching</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Open Source]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Frontend Development]]></category><dc:creator><![CDATA[EasyDailyTools]]></dc:creator><pubDate>Mon, 30 Nov 2026 05:32:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/6b0d03ed8bc4056f4ea09024/f7d84adc-7303-43f0-bad8-cc3f3fdd5883.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>If you have ever built a browser-based image or print utility, you have probably hit a frustrating surprise: drawing an image onto an HTML <code>&lt;canvas&gt;</code> and calling <code>canvas.toBlob('image/jpeg', 0.92)</code> does <strong>two destructive things</strong>:</p>
<ol>
<li>It re-compresses the image pixels (introducing generation loss on every JPEG save).</li>
<li>It strips all physical print density headers, leaving the file at a default <strong>72 or 96 DPI</strong> — which causes print portals and book/packaging preflight checks to reject the file immediately.</li>
</ol>
<p>Yet changing an image's embedded DPI metadata does not require touching a single compressed pixel. You can inspect and patch JPEG and PNG DPI headers directly inside a <code>Uint8Array</code> in <strong>zero-dependency pure JavaScript</strong>, right in the browser or Node.js, with <strong>0% pixel quality loss</strong>.</p>
<p>In this walkthrough, I will share the exact binary layout and working JavaScript routines we open-sourced while building the free browser-local <a href="https://easydailytools.com/image-dpi-converter">Image DPI Converter on EasyDailyTools</a>.</p>
<hr />
<h2>Why Print Portals Reject 72 DPI Files (Even With Enough Pixels)</h2>
<p>Digital screens only care about pixel dimensions (<code>width × height</code>). Print preflight validators care about <strong>two checks</strong>:</p>
<ol>
<li><strong>Pixel budget:</strong> For a <code>4 × 6 in</code> print at <code>300 DPI</code>, you need at least <code>1200 × 1800 px</code>.</li>
<li><strong>Embedded density header:</strong> Even if a camera photo is <code>3000 × 2000 px</code>, many web exports omit or leave the header at <code>72 DPI</code>. Automated print uploaders read that header first and throw a <em>"Low Resolution Image"</em> error.</li>
</ol>
<p>If you want a full breakdown of exact pixel matrices, bleed margins, and safe zones for print shops, we documented all standard dimensions in our <a href="https://easydailytools.com/guides/print-shop-pdf-image-rejection-checklist">Print Shop PDF &amp; Image Rejection Checklist</a>.</p>
<hr />
<h2>1. Patching JPEG DPI Losslessly (<code>APP0 JFIF</code> + <code>APP1 EXIF IFD0</code>)</h2>
<p>A real-world JPEG file can store its resolution in <strong>two independent binary headers</strong>:</p>
<ul>
<li><strong><code>APP0</code> (<code>0xFF 0xE0</code>, <code>JFIF\0</code>)</strong>: Stores density units at payload offset <code>+7</code> (<code>1</code> = dots per inch, <code>2</code> = dots per cm), followed by 16-bit big-endian <code>Xdensity</code> (<code>+8..+9</code>) and <code>Ydensity</code> (<code>+10..+11</code>).</li>
<li><strong><code>APP1</code> (<code>0xFF 0xE1</code>, <code>Exif\0\0</code>)</strong>: Contains an embedded TIFF header (<code>II</code> little-endian or <code>MM</code> big-endian, magic <code>42</code>) with <code>IFD0</code> rational tags:<ul>
<li><code>0x011A</code> (<code>XResolution</code>, TIFF type <code>5</code> = <code>RATIONAL</code>, numerator/denominator <code>uint32</code> pair)</li>
<li><code>0x011B</code> (<code>YResolution</code>, TIFF type <code>5</code> = <code>RATIONAL</code>)</li>
<li><code>0x0128</code> (<code>ResolutionUnit</code>, <code>2</code> = inches, <code>3</code> = centimeters)</li>
</ul>
</li>
</ul>
<p><strong>The subtle bug many scripts miss:</strong> If you only patch <code>JFIF APP0</code> and ignore an existing <code>EXIF IFD0</code> block, macOS Preview and many print engines will still read <code>72 DPI</code> from the EXIF header! You must patch <strong>both</strong> in-place — and never cross the <code>0xFF 0xDA</code> (<code>SOS</code> — Start of Scan) marker so the Huffman-coded pixel payload remains byte-for-byte identical.</p>
<pre><code class="language-js">function writeU16(bytes, off, val, le) {
  const v = val &amp; 0xffff;
  bytes[off]     = le ? (v &amp; 0xff) : ((v &gt;&gt;&gt; 8) &amp; 0xff);
  bytes[off + 1] = le ? ((v &gt;&gt;&gt; 8) &amp; 0xff) : (v &amp; 0xff);
}

function writeU32(bytes, off, val, le) {
  const v = val &gt;&gt;&gt; 0;
  if (le) {
    bytes[off] = v &amp; 0xff; bytes[off + 1] = (v &gt;&gt;&gt; 8) &amp; 0xff;
    bytes[off + 2] = (v &gt;&gt;&gt; 16) &amp; 0xff; bytes[off + 3] = (v &gt;&gt;&gt; 24) &amp; 0xff;
  } else {
    bytes[off] = (v &gt;&gt;&gt; 24) &amp; 0xff; bytes[off + 1] = (v &gt;&gt;&gt; 16) &amp; 0xff;
    bytes[off + 2] = (v &gt;&gt;&gt; 8) &amp; 0xff; bytes[off + 3] = v &amp; 0xff;
  }
}

function patchJpegDpiInPlace(rawBytes, targetDpi = 300) {
  const out = new Uint8Array(rawBytes);
  let pos = 2; // Skip SOI (FF D8)

  while (pos + 4 &lt;= out.length &amp;&amp; out[pos] === 0xff) {
    while (pos &lt; out.length &amp;&amp; out[pos] === 0xff) pos++;
    const marker = out[pos++];
    if (marker === 0xd9 || marker === 0xda) break; // EOI or SOS

    const segLen = (out[pos] &lt;&lt; 8) | out[pos + 1];
    const segStart = pos + 2;
    const segEnd = Math.min(out.length, pos + segLen);

    if (marker === 0xe0 &amp;&amp; segLen &gt;= 16 &amp;&amp;
        String.fromCharCode(...out.slice(segStart, segStart + 5)) === 'JFIF\x00') {
      out[segStart + 7] = 1; // 1 = DPI
      writeU16(out, segStart + 8, targetDpi, false);
      writeU16(out, segStart + 10, targetDpi, false);
    }

    if (marker === 0xe1 &amp;&amp; segLen &gt;= 14 &amp;&amp;
        String.fromCharCode(...out.slice(segStart, segStart + 6)) === 'Exif\x00\x00') {
      const tiffStart = segStart + 6;
      const endian = String.fromCharCode(out[tiffStart], out[tiffStart + 1]);
      if (endian === 'II' || endian === 'MM') {
        const le = endian === 'II';
        const read16 = (o) =&gt; le ? (out[o] | (out[o + 1] &lt;&lt; 8)) : ((out[o] &lt;&lt; 8) | out[o + 1]);
        const read32 = (o) =&gt; le
          ? ((out[o] | (out[o + 1] &lt;&lt; 8) | (out[o + 2] &lt;&lt; 16) | (out[o + 3] &lt;&lt; 24)) &gt;&gt;&gt; 0)
          : (((out[o] &lt;&lt; 24) | (out[o + 1] &lt;&lt; 16) | (out[o + 2] &lt;&lt; 8) | out[o + 3]) &gt;&gt;&gt; 0);

        const ifdPos = tiffStart + read32(tiffStart + 4);
        const count = read16(ifdPos);
        for (let i = 0; i &lt; count; i++) {
          const ent = ifdPos + 2 + i * 12;
          const tag = read16(ent);
          const type = read16(ent + 2);
          if ((tag === 0x011a || tag === 0x011b) &amp;&amp; type === 5) {
            const ratPos = tiffStart + read32(ent + 8);
            writeU32(out, ratPos, targetDpi, le);
            writeU32(out, ratPos + 4, 1, le);
          } else if (tag === 0x0128) {
            writeU16(out, ent + 8, 2, le); // Unit 2 = Inches
          }
        }
      }
    }

    pos = segEnd;
  }
  return out;
}
</code></pre>
<p>If a JPEG lacks an <code>APP0</code> segment altogether (common with stripped web images), you simply inject an 18-byte <code>JFIF APP0</code> header immediately after the <code>FF D8</code> signature.</p>
<hr />
<h2>2. Patching PNG DPI Losslessly (<code>pHYs</code> Chunk + IEEE CRC-32)</h2>
<p>PNG files do not store DPI directly — they store <strong>pixels per meter</strong> inside a 9-byte <code>pHYs</code> chunk (<code>Math.round(300 / 0.0254) = 11811</code> pixels per meter for 300 DPI).</p>
<p>Because PNG decoders strictly validate chunk checksums, any new or modified <code>pHYs</code> chunk <strong>must</strong> end with a valid <strong>IEEE 802.3 CRC-32</strong> computed across the 4-byte ASCII chunk type (<code>pHYs</code>) plus the 9 payload bytes (13 bytes total):</p>
<pre><code class="language-js">const CRC_TABLE = new Uint32Array(256);
for (let n = 0; n &lt; 256; n++) {
  let c = n;
  for (let k = 0; k &lt; 8; k++) c = (c &amp; 1) ? (0xedb88320 ^ (c &gt;&gt;&gt; 1)) : (c &gt;&gt;&gt; 1);
  CRC_TABLE[n] = c &gt;&gt;&gt; 0;
}

function crc32(bytes, start, len) {
  let c = 0xffffffff;
  for (let i = start; i &lt; start + len; i++) {
    c = CRC_TABLE[(c ^ bytes[i]) &amp; 0xff] ^ (c &gt;&gt;&gt; 8);
  }
  return (c ^ 0xffffffff) &gt;&gt;&gt; 0;
}

function buildPngPhysChunk(targetDpi = 300) {
  const ppm = Math.round(targetDpi / 0.0254); // 300 DPI -&gt; 11811 px/meter
  const chunk = new Uint8Array(21);           // 4 len + 4 type + 9 data + 4 crc
  writeU32(chunk, 0, 9, false);
  chunk.set([0x70, 0x48, 0x59, 0x73], 4);     // "pHYs"
  writeU32(chunk, 8, ppm, false);
  writeU32(chunk, 12, ppm, false);
  chunk[16] = 1;                              // Unit = meter
  writeU32(chunk, 17, crc32(chunk, 4, 13), false);
  return chunk;
}
</code></pre>
<p>To apply it cleanly, walk the PNG chunks starting at byte offset <code>8</code>, replace any existing <code>pHYs</code> chunk (or insert our 21-byte chunk right after <code>IHDR</code>), and copy all <code>IDAT</code> image chunks untouched.</p>
<hr />
<h2>3. Verifying the Result With Binary Re-Scan Proof</h2>
<p>One rule we enforce across all of our browser-local <a href="https://easydailytools.com/image-tools">Image Tools</a> is <strong>never trusting the write path blindly</strong>: after generating the patched <code>Uint8Array</code>, we immediately feed the output bytes back through <code>inspectImageDpi()</code> to verify that <code>xDpi</code>, <code>yDpi</code>, and <code>dpiSource</code> match the target resolution — and that pixel dimensions did not change unless the user explicitly enabled physical resampling.</p>
<p>We use this exact same zero-re-encode binary parsing philosophy in our <a href="https://easydailytools.com/remove-exif-metadata">EXIF &amp; GPS Metadata Remover</a> (which strips GPS, camera serials, XMP, and PNG AI-prompt chunks while preserving ICC color profiles) and our <a href="https://easydailytools.com/split-image">Split Image &amp; Zero-Dependency PKZIP Studio</a>.</p>
<hr />
<h2>Try It Live &amp; Grab the MIT Open-Source Code</h2>
<ul>
<li><strong>Live Browser Tool (No Upload, Batch 30 Photos):</strong> <a href="https://easydailytools.com/image-dpi-converter">EasyDailyTools Image DPI Converter</a></li>
<li><strong>Full MIT Source Code (Node.js &amp; Browser UMD):</strong> <a href="https://github.com/ansar6666/easydailytools-browser-engines">github.com/ansar6666/easydailytools-browser-engines</a></li>
<li><strong>API &amp; Engine Documentation:</strong> <a href="https://ansar6666.github.io/easydailytools-browser-engines/">EasyDailyTools Browser Engines Docs</a></li>
<li><strong>80-Second Video Walkthrough:</strong> <a href="https://youtu.be/QP0dobdMsTQ">How to Convert 72 DPI to 300 DPI Without Losing Quality</a></li>
</ul>
<p>If you have ever hit weird print-shop header edge cases (like conflicting <code>JFIF</code> vs <code>EXIF</code> density units or <code>cm</code> vs <code>inch</code> mismatches), drop them in the comments — I'd love to compare notes!</p>
]]></content:encoded></item></channel></rss>