<?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: kiool</title>
    <description>The latest articles on DEV Community by kiool (@kioolfranrip).</description>
    <link>https://dev.to/kioolfranrip</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%2F4222497%2F9a8413d4-8964-45fc-a031-b733deff89f3.jpg</url>
      <title>DEV Community: kiool</title>
      <link>https://dev.to/kioolfranrip</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kioolfranrip"/>
    <language>en</language>
    <item>
      <title>Passport and Visa Photo Uploads: The Pixel, KB and Framing Rules That Actually Get You Rejected</title>
      <dc:creator>kiool</dc:creator>
      <pubDate>Thu, 12 Nov 2026 09:55:38 +0000</pubDate>
      <link>https://dev.to/kioolfranrip/passport-and-visa-photo-uploads-the-pixel-kb-and-framing-rules-that-actually-get-you-rejected-ebj</link>
      <guid>https://dev.to/kioolfranrip/passport-and-visa-photo-uploads-the-pixel-kb-and-framing-rules-that-actually-get-you-rejected-ebj</guid>
      <description>&lt;p&gt;A passport photo upload fails for one of three reasons, and they are easy to confuse:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;file size&lt;/strong&gt; is over the cap (KB)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;pixel dimensions&lt;/strong&gt; are wrong (px)&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;framing&lt;/strong&gt; is wrong (head size, background, glasses)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most guides blend all three together, which is why people compress a photo until it&lt;br&gt;
looks terrible and &lt;em&gt;still&lt;/em&gt; get rejected — because the real problem was #3.&lt;/p&gt;

&lt;p&gt;Here are the actual published numbers, then what each one means in practice.&lt;/p&gt;
&lt;h2&gt;
  
  
  The specs, from the sources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;United States — DS-160 / DS-260 / Diversity Visa&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Format&lt;/td&gt;
&lt;td&gt;JPEG only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dimensions&lt;/td&gt;
&lt;td&gt;Square, &lt;strong&gt;600×600 minimum, 1200×1200 maximum&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;File size&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;≤ 240 KB&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compression ratio&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;≤ 20:1&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Head height&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;50–69%&lt;/strong&gt; of image height&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Eye line&lt;/td&gt;
&lt;td&gt;56–69% from the bottom&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Background&lt;/td&gt;
&lt;td&gt;Plain white or off-white&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Glasses&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Not allowed&lt;/strong&gt; (medical exceptions only)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Recency&lt;/td&gt;
&lt;td&gt;Within 6 months&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Source: &lt;a href="https://travel.state.gov/content/travel/en/us-visas/visa-information-resources/photos.html" rel="noopener noreferrer"&gt;U.S. Department of State — Photo Requirements&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That "20:1 compression ratio" line is the one nobody reads. A photo pulled off WhatsApp&lt;br&gt;
has often been compressed far past that, and the system can reject it even when the&lt;br&gt;
pixel dimensions and byte size are perfect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Schengen (35×45 mm ICAO format)&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Print size&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;35 × 45 mm&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Face coverage&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;70–80%&lt;/strong&gt; of the photo&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Background&lt;/td&gt;
&lt;td&gt;Plain, light-coloured&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Recency&lt;/td&gt;
&lt;td&gt;Within 6 months&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Sources: &lt;a href="https://mzv.gov.cz/jnp/en/information_for_aliens/supporting_documents_overview/photo.html" rel="noopener noreferrer"&gt;Czech MFA&lt;/a&gt;,&lt;br&gt;
&lt;a href="https://www.france-visas.gouv.fr/documents/d/france-visas/iso_iec_fv_visa_photograph_requirements_en" rel="noopener noreferrer"&gt;France-Visas photo guidance&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note the conflict with the US: &lt;strong&gt;70–80% face coverage for Schengen vs 50–69% head height&lt;br&gt;
for the US&lt;/strong&gt;. A photo cropped for one is wrong for the other. This is the single most&lt;br&gt;
common reason people redo a photo that "already met the requirements" — it met the&lt;br&gt;
&lt;em&gt;other&lt;/em&gt; country's requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;India — Passport Seva online upload&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Pixel dimensions&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;630 × 810 exactly&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;File size&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;10 KB – 250 KB&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Format&lt;/td&gt;
&lt;td&gt;JPEG&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Face coverage&lt;/td&gt;
&lt;td&gt;80–85%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;One caveat on the Indian numbers: unlike the US and Schengen figures, these come from&lt;br&gt;
third-party photo-services documentation rather than a single official spec page. The&lt;br&gt;
630×810 figure is consistent across several sources, but if your upload keeps failing,&lt;br&gt;
check the instructions on the portal itself rather than trusting a blog (including this&lt;br&gt;
one). The point that matters here still holds: if the rejection is about exact pixel&lt;br&gt;
dimensions, compressing harder will never fix it.&lt;/p&gt;
&lt;h2&gt;
  
  
  What compression can and cannot fix
&lt;/h2&gt;

&lt;p&gt;This is the part worth being blunt about.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compression fixes:&lt;/strong&gt; file size in KB. That's it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compression does not fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wrong pixel dimensions&lt;/li&gt;
&lt;li&gt;Wrong aspect ratio&lt;/li&gt;
&lt;li&gt;Head too small or too large in frame&lt;/li&gt;
&lt;li&gt;Grey background instead of white&lt;/li&gt;
&lt;li&gt;Glasses&lt;/li&gt;
&lt;li&gt;A photo older than 6 months&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your upload was rejected for any of those reasons, running it through a compressor&lt;br&gt;
will produce a smaller file that gets rejected for the same reason.&lt;/p&gt;

&lt;p&gt;I built a small browser tool for the first problem specifically, because it is the one&lt;br&gt;
that is mechanical and easy to get wrong by hand: &lt;a href="https://imgpress-c90.pages.dev/compress-image-to-20kb/" rel="noopener noreferrer"&gt;imgpress-c90.pages.dev&lt;/a&gt;&lt;br&gt;
takes a target in KB and finds the highest JPEG/WebP quality that still fits under it.&lt;br&gt;
Everything runs in the tab — no upload, which matters for a passport scan.&lt;/p&gt;

&lt;p&gt;But it deliberately does &lt;em&gt;not&lt;/em&gt; crop to 600×600 or 630×810, because cropping is where&lt;br&gt;
portals differ and a wrong auto-crop is worse than no crop.&lt;/p&gt;
&lt;h2&gt;
  
  
  Getting under a small KB cap without destroying the photo
&lt;/h2&gt;

&lt;p&gt;Portals that ask for 20–50 KB are usually old government systems with a database field&lt;br&gt;
sized decades ago. The trap is that people compress by dragging a quality slider down&lt;br&gt;
until the number fits, then wonder why the face looks like a watercolour.&lt;/p&gt;

&lt;p&gt;The better technique is to treat the cap as a constraint and search for the best quality&lt;br&gt;
that satisfies it, downscaling resolution only when quality alone cannot get there:&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;encodeUnderTarget&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;mime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;targetBytes&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;top&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;toBlob&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;mime&lt;/span&gt;&lt;span class="p"&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="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;top&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;targetBytes&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;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// already fits&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.15&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;best&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;6&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;toBlob&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;mime&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;targetBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt; &lt;span class="o"&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;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="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="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="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;best&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                      &lt;span class="c1"&gt;// null =&amp;gt; downscale and retry&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things I got wrong building this, in case you are writing something similar:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File size scales with the scale factor roughly linearly, not with area.&lt;/strong&gt; I assumed&lt;br&gt;
&lt;code&gt;scale ≈ sqrt(target/actual)&lt;/code&gt; (area model) and it under-shrank every time, so the loop&lt;br&gt;
crept toward the target one round at a time. Measuring it showed 0.423× dimensions →&lt;br&gt;
0.420× bytes, and 0.652× → 0.646×. The exponent is about 1, so the correct step is just&lt;br&gt;
&lt;code&gt;scale ≈ target/actual&lt;/code&gt;. On a 12MP test image that cut the search from 43 encodes to 16.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A binary search never tests its own floor.&lt;/strong&gt; If the lowest quality happens to fit&lt;br&gt;
exactly at the boundary, six probes all miss it and the round is thrown away. Testing&lt;br&gt;
&lt;code&gt;q = 0.15&lt;/code&gt; explicitly after the search fixed a whole wasted round.&lt;/p&gt;

&lt;p&gt;One more thing that matters for ID photos: &lt;strong&gt;transparent PNGs composite onto black in&lt;br&gt;
JPEG&lt;/strong&gt;. You have to paint the canvas white before drawing, or every transparent region&lt;br&gt;
comes out black — which on a passport photo looks like a corrupted upload.&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="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fillStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'#ffffff'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&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;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&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;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&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;h2&gt;
  
  
  A checklist before you upload
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Pixel dimensions match the portal's requirement &lt;strong&gt;exactly&lt;/strong&gt; (not close)&lt;/li&gt;
&lt;li&gt;[ ] Aspect ratio is right — square for the US, 7:9 for India's 630×810&lt;/li&gt;
&lt;li&gt;[ ] File size is under the cap, and the compression ratio is not absurd (US: ≤20:1)&lt;/li&gt;
&lt;li&gt;[ ] Background is plain white or off-white, no shadows&lt;/li&gt;
&lt;li&gt;[ ] Head/facial coverage matches &lt;em&gt;that&lt;/em&gt; country's rule, not another's&lt;/li&gt;
&lt;li&gt;[ ] No glasses (US), no head covering except religious/medical&lt;/li&gt;
&lt;li&gt;[ ] Taken within 6 months&lt;/li&gt;
&lt;li&gt;[ ] You kept the original — do all compression from the original, never from an
  already-compressed copy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last point is worth repeating. Stacking compressions is how a perfectly good photo&lt;br&gt;
becomes unreadable: each pass discards information that was already marginal.&lt;/p&gt;




&lt;p&gt;The compression part of this is &lt;a href="https://imgpress-c90.pages.dev/compress-image-to-20kb/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;br&gt;
if you want the mechanical fix. The framing and dimension parts you have to check against&lt;br&gt;
your own consulate's page — they genuinely differ, and that is not something a tool should&lt;br&gt;
guess at for you.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>productivity</category>
      <category>tutorial</category>
      <category>showdev</category>
    </item>
    <item>
      <title>I built a 176-country trends aggregator that runs entirely on GitHub Actions</title>
      <dc:creator>kiool</dc:creator>
      <pubDate>Wed, 04 Nov 2026 09:59:09 +0000</pubDate>
      <link>https://dev.to/kioolfranrip/i-built-a-176-country-trends-aggregator-that-runs-entirely-on-github-actions-1hha</link>
      <guid>https://dev.to/kioolfranrip/i-built-a-176-country-trends-aggregator-that-runs-entirely-on-github-actions-1hha</guid>
      <description>&lt;p&gt;Every time I wanted to know what people in another country were actually searching for, I ended up opening a local trending page, hitting a region block, or reading a machine-translated news site. So I built a small tool that puts all of it on one map: &lt;strong&gt;176 countries and regions, one row of topics each, every topic linked to the original source.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The interesting part isn't the site. It's the constraint I set myself: &lt;strong&gt;it must not need a server.&lt;/strong&gt; No VPS, no container, no always-on machine. If I have to keep something running, I will eventually stop keeping it running.&lt;/p&gt;

&lt;p&gt;Here are the four things that broke, and what I did about them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The architecture, in one diagram
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GitHub Actions (every 3 hours)
  └─ node tools/crawl.mjs        → 176 requests, ~90 s
       └─ writes static JSON      → 2.7 MB total
            └─ wrangler pages deploy
Cloudflare Pages (static only)
  └─ serves HTML + JSON
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The crawler is plain Node with zero dependencies — no axios, no cheerio, no puppeteer. RSS and JSON get parsed with a handful of regexes, which is fragile in general but fine when you only have four well-known sources.&lt;/p&gt;

&lt;p&gt;The frontend is vanilla JS. The world map is drawn at runtime from a local GeoJSON file into SVG, so there are no map tiles and no CDN.&lt;/p&gt;

&lt;p&gt;Payload is small enough that the "no backend" decision was easy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;board.json        234 KB  →  70 KB gzipped   (every country's #1 topic)
details/US.json    16 KB                     (loaded only when opened)
countries.json     36 KB  →   8 KB gzipped
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Problem 1: Google started handing out captchas
&lt;/h2&gt;

&lt;p&gt;Google Trends publishes a daily trends RSS feed per country. Fetch 176 of them politely and you get JSON-ish XML. Fetch them a little too eagerly and you get a 302 to &lt;code&gt;/sorry/index&lt;/code&gt;, which is Google's polite way of saying "slow down".&lt;/p&gt;

&lt;p&gt;I measured the cliff. Three concurrent requests were enough to trigger it on roughly half the countries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3-way concurrency:  21 countries in 13 s   →  ok 21, failed 0   (fine)
3-way concurrency:  16 countries in 6.6 s  →  ok 8,  failed 8   (bad)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The window is narrow, and once you're in it, everything fails at once. What worked was aggressively boring: a single global throttle shared by all workers, 260–480 ms of jitter between request starts, 3 workers, and a short retry on 302 instead of giving up.&lt;/p&gt;

&lt;p&gt;Result: 176/176 countries, zero failures, ~90 seconds.&lt;/p&gt;

&lt;p&gt;Then I moved the same script to GitHub's runners, expecting to be blocked outright — datacenter IPs, right? &lt;strong&gt;It wasn't.&lt;/strong&gt; Same 122/122 Google coverage as from my residential connection. I'd built the whole proxy fallback for a problem that didn't appear. Worth remembering the next time you assume an API will reject a particular network before you've actually measured it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 2: every URL returned 200
&lt;/h2&gt;

&lt;p&gt;While chasing an unrelated issue I ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-o&lt;/span&gt; /dev/null &lt;span class="nt"&gt;-w&lt;/span&gt; &lt;span class="s2"&gt;"%{http_code}"&lt;/span&gt; https://example.com/no-such-page
&lt;span class="c"&gt;# 200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And &lt;code&gt;curl https://example.com/no-such.js&lt;/code&gt; also returned &lt;code&gt;200&lt;/code&gt;, with 6189 bytes of HTML — the homepage. Cloudflare Pages (or rather, a deployment with an &lt;code&gt;index.html&lt;/code&gt; at the root and no &lt;code&gt;404.html&lt;/code&gt;) was answering every unknown path with the homepage.&lt;/p&gt;

&lt;p&gt;This is a soft 404, and it's worse than a broken link:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google treats it as duplicate content across infinitely many URLs&lt;/li&gt;
&lt;li&gt;It can inflate "discovered" pages while adding zero value&lt;/li&gt;
&lt;li&gt;A missing &lt;code&gt;.js&lt;/code&gt; returning HTML breaks clients in ways that are hard to debug&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fix is one file: a real &lt;code&gt;public/404.html&lt;/code&gt;, which Pages serves with a 404 status. After that, unknown paths correctly 404 while real assets still return 200.&lt;/p&gt;

&lt;p&gt;I only found this because I was checking something else. If you deploy to Pages and never added a 404 page, go check yours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 3: Cloudflare's clean URLs broke site verification
&lt;/h2&gt;

&lt;p&gt;Google Search Console offers two HTML-based verification methods. The file method needs &lt;code&gt;https://your.site/googleXXXX.html&lt;/code&gt; to return 200 with a specific line of text.&lt;/p&gt;

&lt;p&gt;Cloudflare Pages has other ideas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;GET /googleXXXX.html  →  308 Permanent Redirect  →  /googleXXXX
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pages rewrites any &lt;code&gt;.html&lt;/code&gt; URL to its extensionless form. Google's verifier may or may not follow that redirect, but "may or may not" is not a good place to be when the failure message is just &lt;em&gt;"we couldn't find your file at the given location"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I added a rewrite in &lt;code&gt;_redirects&lt;/code&gt; to serve the file at its literal path with a 200:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/google9963f8632195544.html  /google9963f8632195544  200
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That worked. But I ended up switching to the meta tag method anyway — it verifies by reading a &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt; tag from the homepage and doesn't care about redirects, paths, or deploy timing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 4: my own CSP silently blocked analytics
&lt;/h2&gt;

&lt;p&gt;I enabled Cloudflare Web Analytics, deployed, waited, and the dashboard showed nothing. No error in the UI, no hint, just empty charts.&lt;/p&gt;

&lt;p&gt;The cause was a security header I'd written earlier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Content-Security-Policy: script-src 'self'; connect-src 'self'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both directives are reasonable defaults — and both block &lt;code&gt;static.cloudflareinsights.com&lt;/code&gt;. The beacon never loaded, so no data was ever sent, so the dashboard had nothing to show. I had to allow the host in both directives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;script-src  'self' https://static.cloudflareinsights.com;
connect-src 'self' https://cloudflareinsights.com https://static.cloudflareinsights.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lesson: a strict CSP is good, but every third-party you add later has to be re-admitted, and the failure mode is silence rather than an error page.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd do differently
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Search trends and news interest are not the same thing.&lt;/strong&gt; Google only publishes daily trends for 122 of the 176 places I wanted to cover. The rest fall back to Wikipedia pageviews or per-country Bing News. That's not a bug, but it means a card for Fiji and a card for Japan are answering slightly different questions. I label every row with its actual source rather than blending them into one fake ranking — but a reader who ignores the label will draw wrong conclusions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hash routing cost me my content, and I only noticed late.&lt;/strong&gt; I shipped the country view as &lt;code&gt;/#/JP&lt;/code&gt;. That is one indexable URL standing in for 176 countries of content — and because the topics are rendered client-side, a crawler that doesn't run JavaScript sees nothing at all.&lt;/p&gt;

&lt;p&gt;The fix was a build step: the same crawl that writes JSON now also emits plain HTML pages at &lt;code&gt;/country/us/&lt;/code&gt;, &lt;code&gt;/country/jp/&lt;/code&gt; and so on, with the topic list, a real &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt;, and per-source explanations baked into the markup. Twenty of them, not 176 — I want to see whether anyone actually searches for "what's trending in Japan" before generating 156 more pages nobody asked for.&lt;/p&gt;

&lt;p&gt;The general lesson: if a page's value is its content, don't make the crawler execute a program to read it.&lt;/p&gt;

&lt;h2&gt;
  
  
  One thing the data turned up by accident
&lt;/h2&gt;

&lt;p&gt;Once every country had a topic, I clustered them by headline similarity to see whether the same story ever topped more than one country's list. It does, constantly — and the clusters are a better read than the country-by-country list.&lt;/p&gt;

&lt;p&gt;On the day I wrote this, a single NBA game was the #1 trending search in nine countries, none of which were the United States. A football fixture appeared in five West African countries at once. Whatever you think your local feed says about the world, it is mostly saying something about you.&lt;/p&gt;

&lt;p&gt;The clustering itself is deliberately dumb: lowercase, strip punctuation, drop generic tokens like &lt;code&gt;vs&lt;/code&gt; and &lt;code&gt;fc&lt;/code&gt;, then group by token overlap. A semantic model would merge more aggressively, and would also invent matches that aren't there. When the output is going to be published as a claim about the world, I would rather under-merge.&lt;/p&gt;




&lt;p&gt;If you want to look at the result: &lt;strong&gt;&lt;a href="https://trends-world.pages.dev" rel="noopener noreferrer"&gt;https://trends-world.pages.dev&lt;/a&gt;&lt;/strong&gt; — click a country, or just scroll the grid. The crawler and the deploy workflow are on GitHub, linked from my profile; I'm keeping this post to a single link on purpose.&lt;/p&gt;

&lt;p&gt;If you deploy to Cloudflare Pages, the two things worth checking tonight are whether you have a &lt;code&gt;404.html&lt;/code&gt;, and whether your CSP is quietly eating something.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>cloudflare</category>
    </item>
    <item>
      <title>Building an Image Compressor That Hits an Exact File Size</title>
      <dc:creator>kiool</dc:creator>
      <pubDate>Mon, 02 Nov 2026 14:08:08 +0000</pubDate>
      <link>https://dev.to/kioolfranrip/building-an-image-compressor-that-hits-an-exact-file-size-3884</link>
      <guid>https://dev.to/kioolfranrip/building-an-image-compressor-that-hits-an-exact-file-size-3884</guid>
      <description>&lt;p&gt;Every upload form has the same annoying quirk: it rejects on file size, but every compressor gives you a quality slider instead of a size input.&lt;/p&gt;

&lt;p&gt;So you end up in a loop. Compress, check size, compress again. Adjust quality from 80 to 70. Check again. 65. Again.&lt;/p&gt;

&lt;p&gt;I wanted to invert that: give the tool the number, let it figure out the quality.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem with quality-first
&lt;/h2&gt;

&lt;p&gt;JPEG file size doesn't vary linearly with the quality parameter. Going from q=0.9 to q=0.8 might drop the file by 40%, or by 8%, depending on the image content. That's why the manual loop feels so random.&lt;/p&gt;

&lt;p&gt;But the relationship &lt;em&gt;is&lt;/em&gt; monotonic — lower quality always means smaller file. That's enough for a binary search.&lt;/p&gt;

&lt;h2&gt;
  
  
  Binary search over quality
&lt;/h2&gt;

&lt;p&gt;For a fixed image size, find the highest quality whose encoded blob is still under the target:&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;encodeUnderTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;targetBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Check the ceiling first: if even q=0.95 fits, done.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;top&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;toBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mime&lt;/span&gt;&lt;span class="p"&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;targetBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;quality&lt;/span&gt;&lt;span class="p"&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&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="nx"&gt;best&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="c1"&gt;// 6 iterations narrows the range to ~1% — plenty for file size.&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="nx"&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&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="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&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="nx"&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;toBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mid&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="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;targetBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// this works, try higher quality&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// too big, try lower&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="nx"&gt;best&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// null means even q=0.15 is too big&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quality&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBlob&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;mime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quality&lt;/span&gt;&lt;span class="p"&gt;)&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;Six encodes lands within a few percent below the target, instead of the 20-40% below you get from guessing a quality value.&lt;/p&gt;

&lt;h2&gt;
  
  
  When quality isn't enough
&lt;/h2&gt;

&lt;p&gt;Some images can't hit the target at any quality — high-ISO photos, heavy noise, complex texture. So there's a second loop: if even q=0.15 overshoots, scale down and retry.&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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="nx"&gt;attempt&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="nx"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;attempt&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="nx"&gt;canvas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;drawScaled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;srcW&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;srcH&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&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;encodeUnderTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;targetBytes&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="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// 30% smaller each round&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;srcW&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;srcH&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&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;The 0.7 factor matters. At 0.9 you need many more rounds, and each round costs 6 encodes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three things that bit me
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Transparent PNGs turn black in JPEG&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;canvas.toBlob(..., 'image/jpeg')&lt;/code&gt; composites onto black by default. You have to paint white first:&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="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fillStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#ffffff&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drawImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Huge images make the search unusable&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each &lt;code&gt;toBlob()&lt;/code&gt; on a 24MP canvas takes 300-500ms. Six encodes × six scale rounds = 30+ seconds. So downscale anything above ~12MP before searching:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MAX_PIXELS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="nx"&gt;e6&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="nx"&gt;srcW&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;srcH&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;MAX_PIXELS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MAX_PIXELS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;srcW&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;srcH&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;&lt;strong&gt;3. Files already under target shouldn't be re-encoded&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your 20KB file is already under 50KB, re-encoding it loses quality for zero benefit. Pass it through untouched:&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;targetBytes&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;passthrough&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Input&lt;/th&gt;
&lt;th&gt;Target&lt;/th&gt;
&lt;th&gt;Output&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1600×1200 photo, 148 KB&lt;/td&gt;
&lt;td&gt;50 KB&lt;/td&gt;
&lt;td&gt;49 KB&lt;/td&gt;
&lt;td&gt;~1.4s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3000×2000 photo, 2.7 MB&lt;/td&gt;
&lt;td&gt;50 KB&lt;/td&gt;
&lt;td&gt;50 KB&lt;/td&gt;
&lt;td&gt;~7s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1200×900 photo, 88 KB&lt;/td&gt;
&lt;td&gt;20 KB&lt;/td&gt;
&lt;td&gt;20 KB&lt;/td&gt;
&lt;td&gt;~0.7s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;240×180, 14 KB&lt;/td&gt;
&lt;td&gt;50 KB&lt;/td&gt;
&lt;td&gt;unchanged&lt;/td&gt;
&lt;td&gt;~0.1s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What I'd like to know
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Is binary search the right approach?&lt;/strong&gt; libjpeg does something similar internally but I wanted pure JS with no wasm payload. Is there a smarter rate-control strategy?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WebP vs JPEG:&lt;/strong&gt; WebP wins 25-35% at equal quality, and it's my default. But Safari's WebP &lt;em&gt;encode&lt;/em&gt; path was historically slow — anyone have current numbers?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The 12MP cutoff:&lt;/strong&gt; arbitrary. Below it, search feels instant; above it, it drags. Is there a better heuristic?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tool is at &lt;a href="https://imgpress-c90.pages.dev" rel="noopener noreferrer"&gt;imgpress-c90.pages.dev&lt;/a&gt; — no upload endpoint exists, you can go offline after the page loads and it still works. Source is plain JS, no build step.&lt;/p&gt;

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