{"success":true,"name":"resize-and-compress-images-at-upload-time-never-serve-a-5mb-","description":"App accepts user photos and later displays them in lists, cards, or avatars","category":"ai-brimind","path":"ai-brimind/resize-and-compress-images-at-upload-time-never-serve-a-5mb-/SKILL.md","file":"SKILL.md","content":"---\nname: resize-and-compress-images-at-upload-time-never-serve-a-5mb-\ndescription: \"App accepts user photos and later displays them in lists, cards, or avatars\"\n---\n\n# Resize and Compress Images at Upload Time: Never Serve a 5MB Photo Into a 100px Avatar\n\n## When to use\nThe app stores user-uploaded images (avatars, club logos, product photos) and serves them back into the UI as-is — original camera-sized files flowing into thumbnails and lists — with no resizing, compression, or size-variant step anywhere in the pipeline.\n\n## Example request\nA member list shows 50 avatars, each the original 4MB phone photo scaled down by CSS → the page silently downloads 200MB; the screen needed 50 images of a few KB each, and the gap between those numbers is pure waste the user pays for in seconds and data. Photos uploaded straight from modern phones are 4000+ pixels wide → no screen in the app displays more than 800; resizing once at upload time makes every future view cheap forever, which beats resizing on every request. One stored size serves the avatar (40px), the profile page (300px), and a zoom view (1200px) → generating 2-3 named variants at upload (thumb, medium, large) lets each context load only what it shows; this is the standard 'image variants' pattern, and libraries like sharp exist precisely for it. Next.js already ships next/image → it handles lazy loading, sizing hints, and modern formats for display — the framework's built-in is the display half of the answer; the upload-time processing is yours.\n\n## Instructions\nAct as a developer who reads every image on a page as a bill the user pays — in load seconds, in mobile data, in server bandwidth — and who knows the single most wasteful line item in typical apps is original-resolution photos jammed into small boxes, because CSS scaling changes what the eye sees but not one byte of what the network carried. For any feature storing and displaying user images, do this: (1) Process at upload time, once, so every later view is cheap: after your upload-validation gate accepts the file, run it through an image library (sharp is the Node standard — this is a solved category, never hand-roll pixel work) to resize down to the largest dimension the app will ever display, re-encode with sensible compression (and a modern format like WebP where you choose to), and strip metadata — which also quietly removes GPS EXIF data users never meant to publish, a privacy win riding along free. (2) Decide the variant set from the actual UI, not from habit: list where images appear (avatar 40px, card 300px, detail 1200px?) and generate the 2-3 named sizes those contexts need, storing each per your file-naming scheme with the variant in the name or path; the DB record keeps the base reference. Each display context then requests ITS size — the 50-avatar list costs kilobytes, as it always should have. (3) On the display side, use the framework's machinery instead of raw <img>: next/image gives lazy loading, dimension-based layout stability, and responsive selection — pair it with your variants rather than treating it as a substitute for them (it optimizes serving; it does not shrink what you stored). (4) Keep the operational disciplines attached: processing is CPU work, so for bulk or heavy cases it belongs in your background-work pattern rather than blocking the request; failures mid-processing must not leave half-saved variant sets (your all-or-nothing thinking applies); and the size limit at the border still guards the door — resizing huge files is fine, accepting infinite ones is not. (5) Verify with the only meter that counts: open the network tab on the heaviest image page and read the transferred bytes — before, a list page moving tens of megabytes; after, the same page in the hundreds of kilobytes. Only call image handling done when no display context downloads meaningfully more pixels than it renders — 'the pictures show up' was never the bar; what they cost is.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #233\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}