Pattern: Photo and video ratios, cropping and placeholders
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion. The sizes and ratios below are example values.
- Core ideas: accept a limited range of ratios and crop outside it at upload time, generate a few sizes once, reserve the box before the image loads, and strip location metadata.
- Related: feeds and pagination, file upload, share links.
1. Problem
Owners ask for “photos that look right” without stating ratios. Without rules, feed cards jump while loading, grids show random crops, huge originals waste bandwidth, and photos leak the location where they were taken.
2. Options
| Topic | Option | Upside | Downside |
|---|---|---|---|
| Ratios | Free ratio everywhere | No cropping | Cards of every height; messy grids; extreme panoramas |
| Ratios | Limited range plus crop step (recommended) | Predictable layout | Users must accept a crop for extreme images |
| Ratios | One fixed ratio | Simplest | Loses the composition of many photos |
| Format | WebP variants | Smaller files than PNG or JPEG at similar quality (Google WebP documentation) | Very old clients; keep a fallback if you need it |
| Placeholder | Blurred tiny preview (BlurHash-style) | Box looks filled before the image arrives | Extra field per image |
| Placeholder | Solid colour | Cheapest | Less pleasant |
3. Recommended design
- Allowed ratios (feed): the card keeps the content’s own ratio within a limited range from landscape to portrait (example: between 4:3 and 3:4). Wider or taller content is brought into the range with a crop step during posting. Without crop display, fill remaining space with a fixed background colour.
- Grid (profile): fixed cell ratio (example: 4:5), fixed column count (example: 3), thin gaps; cells show a cropped thumbnail.
- Sizes: compress on the server once and produce a few variants (example: large 2048 px, medium 1080 px, small 400 px, WebP). The client asks for the variant matching its screen; for web pages use
srcset/sizesand<picture>as in MDN’s responsive images guide. - Placeholder: store a very small blurred preview string per image (the BlurHash project describes the idea) so the box looks full before the image arrives.
- No layout shift: know the card height from the ratio before loading. On the web set
widthandheightor CSSaspect-ratio(web.dev: images without dimensions are a main source of layout shift; MDN documentsaspect-ratio). Measure with Cumulative Layout Shift (CLS). - Video: duration limits (example: 5 seconds to 1 minute), size limit derived from the duration, a poster frame, and prepare only the one video that is most visible on screen.
- Privacy: remove location and other metadata (EXIF) at upload. OWASP’s File Upload sheet says to decode and re-encode images to an allowed format, explicitly removing unnecessary metadata, while noting that rewriting does not guarantee all malicious content is gone. Do not write other people’s media to the phone’s gallery without a user action.
- Platform ratios: platforms publish recommended ratios, but this guide did not verify them against primary pages; read the current official guidance for the platforms you target before fixing numbers.
4. Scenarios and edge cases
- Panorama or very tall screenshot: the crop step offers a framing; the original is not stored beyond what the product needs.
- Photo without metadata or with rotated orientation: normalise orientation before generating variants.
- Slow network: small variant plus placeholder arrive first; large variant replaces it.
- Image missing or deleted: the reserved box shows a neutral empty state, not a collapsed layout.
5. Common mistakes
- Cards without a known height, so lists jump.
- Serving the 2048 px original in a 400 px grid cell.
- Keeping EXIF location in published images.
- Computing crops differently on server and client.
- Preparing every video in the feed at once.
6. How to audit
- What: layout does not shift while images load.
- How to check: throttle the network and watch the list; run a layout-shift measurement.
- Evidence: CLS value or a screen recording.
- What: correct size variant is requested per screen.
- How to check: read the network requests in a grid and a detail view.
- Evidence: request URLs and sizes.
- What: published images carry no location metadata.
- How to check: upload a geotagged photo, download the published file, inspect its metadata.
- Evidence: metadata dump with no location.
- What: out-of-range ratios are cropped.
- How to check: upload a 21:9 image and a 1:3 image.
- Evidence: resulting ratios.
Sources
- Cumulative Layout Shift (CLS) — web.dev (Google)
- Optimize Cumulative Layout Shift — web.dev (Google)
- aspect-ratio CSS property — MDN Web Docs
- Using responsive images in HTML — MDN Web Docs
- The picture element — MDN Web Docs
- WebP: An image format for the Web — Google for Developers
- BlurHash — BlurHash project
- File Upload Cheat Sheet — OWASP Cheat Sheet Series
- User Privacy Protection Cheat Sheet — OWASP Cheat Sheet Series