Sub topic 12
Generating the Image Widths You Need
This chapter assumes you know how srcset with w descriptors and sizes work, and that you can run commands in a terminal. Here you will produce the files that those attributes point to.
Why variants need a plan
A srcset list is only as useful as the files behind it. If you list photo-400.jpg, photo-800.jpg, and photo-1600.jpg, each file must exist and its real pixel width must match the number in the w descriptor. A mismatch makes the browser's size calculation wrong, and it may download more data than it needs or show a blurry image.
Start from the largest source image you have. Generate smaller copies from it. Never enlarge a small image to make a bigger variant, because that adds file size without adding detail.
Choosing the widths
Pick widths from how large the image can appear, not from device names. A practical method:
- Find the largest width the image is displayed at, in CSS pixels. Multiply it by 2 to cover 2x screens.
- Find the smallest width, usually a narrow phone layout.
- Add a few steps in between. Steps about 1.5 to 2 times apart keep the number of files small and the size jump between files reasonable.
For an image shown at up to 800 CSS pixels wide, the widths 400, 800, and 1600 cover 1x and 2x screens at both small and large sizes.
Resizing with ImageMagick
This ImageMagick 7 loop creates one file per width. The "${w}x" geometry sets the width and keeps the aspect ratio.
for w in 400 800 1600; do
magick photo.jpg -resize "${w}x" "photo-${w}.jpg"
doneTo avoid enlarging an image that is already smaller than the target, add > to the geometry. Quote the geometry so the shell does not treat > as a redirect.
for w in 400 800 1600; do
magick photo.jpg -resize "${w}x>" "photo-${w}.jpg"
doneWith >, a 1200-pixel source produces a 1200-pixel file for the 1600 step. Its real width is then 1200, so the 1600w descriptor would be wrong. When that happens, drop that width from the srcset or use the true width in the descriptor.
Checking the results
Confirm the actual widths before you write the HTML:
for f in photo-*.jpg; do
magick identify -format "%f %w x %h\n" "$f"
doneThe output should list each file with the width in its name. Then write the matching markup:
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(min-width: 40em) 50vw, 100vw"
width="1600"
height="1200"
alt="A lighthouse on a rocky coast at sunset">The workflow at a glance
Common errors
- Descriptor does not match the file. A file labeled
800wthat is really 640 pixels wide leads to poor choices. Check withidentify. - Too many variants. Ten widths for one image add storage and cache entries for little gain. Three to five is usually enough.
- Upscaling. Resizing a small source to a large width increases bytes but not sharpness.
- Wrong aspect ratio across files. With
srcsetandwdescriptors, all candidates must have the same aspect ratio. If you need a different crop, usepicturewithmedia, as covered in the art direction chapter. - Compression ignored. Resizing does not choose quality settings for you. Add
-quality 80to a JPEG command, then compare file sizes and look at the result.
Exercise
Exercise
Your master image is 2400 pixels wide and is displayed at up to 600 CSS pixels wide. Which widths would you generate, and what command creates them?
Show answer
Cover 1x and 2x at the largest size, plus a smaller step for narrow layouts. For example 300, 600, and 1200 (600 x 2 = 1200). The command is: for w in 300 600 1200; do magick photo.jpg -resize "${w}x" "photo-${w}.jpg"; done. The 2400-pixel width is not needed because no screen will display the image above 1200 device pixels.