<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thofiq Imamsab</title>
    <description>The latest articles on DEV Community by Thofiq Imamsab (@developer_thofiq_923e9521).</description>
    <link>https://dev.to/developer_thofiq_923e9521</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4267284%2F2635530a-a2db-4055-a160-09d4c0fb292f.png</url>
      <title>DEV Community: Thofiq Imamsab</title>
      <link>https://dev.to/developer_thofiq_923e9521</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/developer_thofiq_923e9521"/>
    <language>en</language>
    <item>
      <title>Show DEV: hitting an exact KB limit for photos and PDFs, entirely in the browser</title>
      <dc:creator>Thofiq Imamsab</dc:creator>
      <pubDate>Thu, 26 Nov 2026 16:13:39 +0000</pubDate>
      <link>https://dev.to/developer_thofiq_923e9521/show-dev-hitting-an-exact-kb-limit-for-photos-and-pdfs-entirely-in-the-browser-2nke</link>
      <guid>https://dev.to/developer_thofiq_923e9521/show-dev-hitting-an-exact-kb-limit-for-photos-and-pdfs-entirely-in-the-browser-2nke</guid>
      <description>&lt;p&gt;Government and exam portals love rules like "photo must be between 20 KB and 50 KB" or "PDF must be under 200 KB". Most compressors give you a quality slider and leave you to guess. I wanted a tool where you type the limit and get a file that fits, without the file ever leaving the browser.&lt;/p&gt;

&lt;p&gt;That became &lt;a href="https://fitthefile.com" rel="noopener noreferrer"&gt;FitTheFile&lt;/a&gt;. This post is about the part that turned out to be more interesting than I expected: actually landing under a byte limit with plain browser APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  There is no "encode to N bytes" API
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;canvas.toBlob(cb, "image/jpeg", quality)&lt;/code&gt; takes a quality from 0 to 1 and gives you whatever size comes out. The size depends on the picture, so the only way to hit a target is to search for it.&lt;/p&gt;

&lt;p&gt;File size goes up as quality goes up, so a binary search works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bestQualityUnder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maxBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.95&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;toJpeg&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;maxBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Seven encodes narrow the quality down to under one percent, which is plenty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quality alone is not enough
&lt;/h2&gt;

&lt;p&gt;A 12 MP phone photo will not fit in 20 KB at any quality worth looking at. If the best quality that fits is below 50%, the tool shrinks the image by 20% on each side and searches again. It repeats until the quality is acceptable or the short side would drop below 160 px.&lt;/p&gt;

&lt;p&gt;The order matters. A smaller image at decent quality looks far better than a full-size image at quality 10, which is all blocks and smears.&lt;/p&gt;

&lt;p&gt;I got this wrong once during testing. I added an early exit to save time, and it started returning quality 38 results where the old code found 50 or better. The tests caught it and I took it back out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which KB?
&lt;/h2&gt;

&lt;p&gt;Is 50 KB 50,000 bytes or 51,200? Portals do not say, and they do not agree with each other.&lt;/p&gt;

&lt;p&gt;So the tool takes the safe side both ways. For a maximum it uses 1 KB = 1000 bytes, and for a minimum it uses 1024. A file that passes both passes whichever the portal picked.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the file is too small
&lt;/h2&gt;

&lt;p&gt;Some forms reject files below a minimum size. A small, clean signature scan can come out at 6 KB when the form wants at least 10.&lt;/p&gt;

&lt;p&gt;Scaling it up or adding noise would change the picture. Instead the tool adds JPEG comment segments (the &lt;code&gt;FF FE&lt;/code&gt; marker), which decoders skip. The image data stays exactly the same.&lt;/p&gt;

&lt;p&gt;One detail: the padding goes after the JFIF header, not before it. Some strict checkers expect JFIF to be the first thing after the start marker.&lt;/p&gt;

&lt;h2&gt;
  
  
  DPI is two numbers in a header
&lt;/h2&gt;

&lt;p&gt;Many forms ask for "200 DPI". Canvas output carries no DPI at all. For a JPEG it is just the density fields in the JFIF header, so the tool writes those bytes directly. No pixels change.&lt;/p&gt;

&lt;h2&gt;
  
  
  PDFs are harder
&lt;/h2&gt;

&lt;p&gt;For PDFs the tool renders each page with pdf.js, encodes the pages as JPEGs, and rebuilds the file with pdf-lib. It walks down a DPI ladder (150, 120, 100 and so on, down to 40), running the same quality search at each step.&lt;/p&gt;

&lt;p&gt;The trade-off is real and the tool says so: the output is images of pages, so text is no longer selectable. For a form upload that is usually fine. For a document you want to search, it is not.&lt;/p&gt;

&lt;p&gt;Two things I had to add after testing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A memory budget. Rendering a 200-page PDF at 150 DPI will crash a phone tab, so the ladder skips steps that would need too many pixels.&lt;/li&gt;
&lt;li&gt;A size check at the end. A PDF that is mostly text can come out bigger as images. In that case the tool gives the original back and says why.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Testing it properly
&lt;/h2&gt;

&lt;p&gt;I ran about 1,000 automated cases through headless Chrome: different sizes, formats, targets, and damaged files.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The image tool had zero misses.&lt;/li&gt;
&lt;li&gt;The PDF tool returned a file larger than the original in 4 of 68 cases. That is where the size check came from.&lt;/li&gt;
&lt;li&gt;Dropping a second file while the first was still processing could show the first file's result under the second file's name. Every run now gets an ID, and only the newest run is allowed to publish its result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What I have not done yet is test much on Safari, Firefox, or real phones. If something breaks there, I would like to hear about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it is and is not
&lt;/h2&gt;

&lt;p&gt;FitTheFile is free and needs no login. Files are processed in your browser and are not uploaded. The site shows ads, which is how I plan to pay for it.&lt;/p&gt;

&lt;p&gt;It does not read HEIC in the size tools (there is a separate HEIC to JPG converter), and it cannot make a 300-page scan fit in 100 KB and stay readable. Nothing can.&lt;/p&gt;

&lt;p&gt;Try it at &lt;a href="https://fitthefile.com" rel="noopener noreferrer"&gt;fitthefile.com&lt;/a&gt;. I would like feedback on the search approach in particular. Is there a smarter way to pick the starting dimensions than stepping down 20% at a time?&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>pdf</category>
    </item>
  </channel>
</rss>
