Craft CMS image transforms

Published

Reviewedbyfl

Image transforms are behind many 504 errors and memory kills on Craft CMS sites. Generate them in the background with a worker job and keep the source images small.

About image transforms

Craft CMS creates resized and reformatted copies of uploaded images, called transforms. Each transform loads the full source image into memory and resizes it with GD or ImageMagick. A single editor upload of a large photo can trigger many transforms at once: one per size and format in the templates, plus the Control Panel thumbnails. With assets on a remote volume such as S3, Craft also has to download the source image and upload every result.

When that happens in the PHP processes that serve the site, requests run into the timeout and return a 504 error, or the process runs out of memory.

How Craft generates transforms

When a transform does not exist yet, Craft adds a queue job to generate it and renders a temporary URL in its place. Whichever comes first creates the file: the queue job or the first browser request to that URL.

Without a worker, both paths run in the PHP processes of the site: by default, Craft processes the queue through a web request.

Generate transforms in a worker job

Run the Craft queue in a worker job, as described in Craft CMS queue jobs. The worker generates transforms in the background, in its own container, outside the PHP processes that serve the site.

Keep generateTransformsBeforePageLoad at its default false. Set to true, Craft generates all missing transforms while the page renders.

Keep source images small

Scale images down before uploading them. Twice the largest display size is enough for high-density screens. A 6000 pixel camera photo takes about nine times the memory of a 2000 pixel one, since memory grows with the pixel count.

Define fewer transforms. Every size and format combination is another file to generate and store. Page weight covers the sizes and formats worth generating.

Official Craft documentation: image transforms.

Written by humans and AI in collaboration.

AI use & editorial processEdit on GitHub ↗