Serving a 4000px-wide original photo to a 300px thumbnail slot is like mailing a refrigerator to deliver an ice cube — the recipient only needed a fraction of what got sent. Image styles are Drupal's answer: define a target size once, and Drupal generates and caches a properly-sized derivative the first time it's needed, then serves that cached derivative on every subsequent request.
What you'll learn in this lesson
- How an image style's effects pipeline actually works
- Choosing the right effect (scale vs. crop vs. scale-and-crop) for your use case
- Why the first request for a new derivative is always slower
Defining a style
# config/install/image.style.article_thumbnail.yml
name: article_thumbnail
label: 'Article thumbnail'
effects:
crop_scale:
id: image_scale_and_crop
data:
width: 300
height: 200
weight: 0
Effects run in a pipeline, in weight order — you can stack multiple (scale, then sharpen, then convert format) into one style. image_scale_and_crop specifically guarantees the exact output dimensions by cropping any excess after scaling, which matters for layouts that assume a fixed aspect ratio.
Scale vs. crop vs. scale-and-crop
- Scale: resizes proportionally, output dimensions vary with the source aspect ratio — use when layout doesn't require an exact size.
- Crop: cuts to exact dimensions without resizing first — can cut off important content if the source aspect ratio differs significantly.
- Scale and crop: resizes to cover the target dimensions, then crops the excess — the standard choice for consistent grid thumbnails.
public://styles/<style_name>/, and only then returns the image. Every subsequent request for that same derivative is served directly from the already-generated file. This is why a freshly-deployed style, or a newly-uploaded image, can cause a visibly slower first load.Pre-warming derivatives to avoid that first-request cost
# Force-generate derivatives ahead of time rather than waiting for
# the first real visitor to trigger generation.
drush image-derive article_thumbnail public://images/photo.jpg
For a site where the first-visitor slowdown genuinely matters (a high-traffic launch, a bulk content import), pre-generating derivatives via drush (or a custom batch operation looping over existing images) moves that cost to deploy time instead of a real visitor's request.
Quick check: you add a brand-new image style to a site with 10,000 existing images. Does adding the style configuration alone generate 10,000 new derivative files immediately? If you said no — derivatives are generated lazily, one at a time, only when each specific image/style combination is first actually requested — you've understood the lazy-generation model correctly.
Key takeaways
- Image style effects run in a weighted pipeline — stack scale, crop, sharpen, and format-conversion effects as needed.
image_scale_and_cropis the standard choice when a layout needs a guaranteed, consistent output size.- Derivatives generate lazily on first request, not when the style is saved — the first visitor to hit a new combination pays that generation cost.
- Pre-warm derivatives with
drush image-deriveahead of high-traffic events to move that cost off real visitors.
Coming up next
Resizing correctly is half the story — the actual file size at that resolution still matters. Next, we look at compression itself.