Skip to content

Match Craft's native transformer for quality, fill, crop and focal points - #218

Merged
timkelty merged 7 commits into
3.xfrom
tim/image-transform-craft-parity
Nov 26, 2026
Merged

timkelty merged 7 commits into
3.xfrom
tim/image-transform-craft-parity

Conversation

@timkelty

@timkelty timkelty commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The Cloud image transformer translated several Craft transform settings differently from Craft's native transformer (Craft 5.9.17 ImageTransforms::generateTransform() and Raster), so the same transform gave different images on Cloud.

Setting Craft Cloud before Cloud now
quality not set defaultImageQuality (82) nothing sent, Cloudflare uses 85 quality sent, falling back to defaultImageQuality1
letterbox, no fill transparent white; JPEG output flattens it to opaque white background=#FFFFFF transparent when Craft's output format has alpha (png, webp, gif, avif), otherwise #FFFFFF2
crop, upscale: false scaleAndCrop() crops smaller images to the target aspect ratio fit=crop, which returns smaller images at their original size and shape fit=aspect-crop3
focal point outside crop mode ignored, position used focal point used as gravity in every mode focal point used only in crop mode
focal point in crop mode crop centered on the focal point, shifted to stay inside the image Cloudflare's default remainder gravity keeps the point at the same relative position in the crop gravity[mode]=box-center4

For example, a 612×613 image with { width: 1920, height: 1120, mode: 'crop', upscale: false } now comes back as 612×357 instead of 612×613.

Known gaps (not addressed here)

  • Cloudflare's pad never enlarges the image, so letterbox with upscale: true can't match Craft.
  • Cloudflare documents gravity only for cover, crop, and aspect-crop, so position on a letterbox transform is probably ignored.
  • A crop with upscale: false and only one dimension still enlarges on Cloudflare (fit=aspect-crop,width=9000 on 6000×4000 gives 9000×6000). That was already true with fit=crop.

Related issues

Footnotes

  1. Including PNG output. Craft saves PNGs losslessly, but Cloudflare takes any quality on PNG output as permission to use palette-based PNG8. ↩

  2. Cloud may serve AVIF or WebP where Craft would write JPEG, so the color has to match Craft's output, not Cloudflare's. A fill of transparent follows the same rule. Cloudflare documents rgba() but not the transparent keyword. ↩

  3. Including PDFs, and SVGs with a raster format, which the gateway rasterizes through the Images binding. That path accepts aspect-crop since craftcms/cloud-gateway#293 (gateway 1.67.5, in production). ↩

  4. box-center is in workerd's cf.d.ts and Images binding types but not on Cloudflare's docs page. The gateway passes gravity[mode] through unchanged on both the cf.image and Images binding paths. ↩

- Send quality, falling back to defaultImageQuality like Craft.
- Letterbox without fill uses a transparent background, like Raster::setFill(null).
- Crop without upscaling maps to aspect-crop, which crops smaller images to the target aspect ratio.
- Only crop mode uses the asset focal point; other modes use position.

Amp-Thread-ID: https://ampcode.com/threads/T-817bab5a-1757-4c07-ac06-409657afd942
Co-authored-by: Amp <amp@ampcode.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 01:56
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T01:59:36.269189Z 9aaf3ca PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block approval of the image-transform behavior updates.

No open issues 🎉

What changed in this PR

This PR brings the Cloud image transformer closer to Craft’s native behavior for image quality, letterbox backgrounds, cropping, and focal points.

Changes:

  • Fall back to defaultImageQuality and use transparent white for letterbox transforms without a fill or with transparent.
  • Use aspect-crop when upscaling is disabled and apply asset focal points only in crop mode.
  • Add regression tests for all four adjustments and update existing quality expectations.
File Description
tests/​unit/​ImageTransformTest.php Tests the four behavior changes and updates expected quality values.
src/​imagetransforms/​ImageTransformer.php Restricts asset focal-point gravity to crop mode.
src/​imagetransforms/​ImageTransformBehavior.php Updates derived quality, letterbox background, and crop-fit options.

Review effort: Balanced

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9aaf3cae68

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Cloudflare treats any quality on PNG output as permission to use palette-based PNG8, while Craft saves PNGs losslessly and uses quality only as a compression level.

Amp-Thread-ID: https://ampcode.com/threads/T-817bab5a-1757-4c07-ac06-409657afd942
Co-authored-by: Amp <amp@ampcode.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 02:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

PNG sources with format: 'auto' retain the quality parameter, allowing palette-based PNG8 output when negotiation falls back to PNG instead of preserving lossless output.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Omit quality for PNG sources using auto format

src/​imagetransforms/​ImageTransformer.php:70

With a PNG source and format: 'auto', this check leaves quality in the URL because it compares auto rather than the source extension. If format negotiation falls back to PNG, that quality value permits palette-based PNG8 output instead of preserving lossless PNG. Account for this fallback when deciding whether to send quality, and add a PNG-with-auto regression test.

Review effort: Balanced

Set the filename on the TransformUrlAsset test fixture so the real getExtension() and getFilename() work.

Amp-Thread-ID: https://ampcode.com/threads/T-817bab5a-1757-4c07-ac06-409657afd942
Co-authored-by: Amp <amp@ampcode.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 00:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

PNG assets using format: 'auto' bypass the quality exemption, allowing PNG fallback to produce palette-based PNG8 and violate the lossless-PNG guarantee.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Preserve lossless PNG quality when auto format falls back to PNG

src/​imagetransforms/​ImageTransformer.php:71

The PNG exemption is bypassed when a PNG asset requests format: 'auto': the comparison sees auto, so the newly defaulted quality is still sent. If negotiation falls back to PNG (for example, a client that does not accept WebP/AVIF), Cloudflare can consequently produce palette-based PNG8, defeating the lossless-PNG guarantee. Account for PNG fallback in automatic format selection and add a PNG-with-format: 'auto' regression case, while preserving quality for explicitly requested lossy formats.

Review effort: Balanced

Craft centers the crop on the focal point and keeps it inside the image. Cloudflare's default remainder mode keeps the point at the same relative position in the crop instead.

Also return no gravity when an asset reports a focal point but getFocalPoint() returns null, which can happen for PDFs.

Amp-Thread-ID: https://ampcode.com/threads/T-817bab5a-1757-4c07-ac06-409657afd942
Co-authored-by: Amp <amp@ampcode.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 00:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Local tests verify only serialized options, leaving aspect-crop dimensions, box-center focal placement, and lossless PNG handling unvalidated against the separately deployed gateway.

No open issues 🎉

Review effort: Balanced

- Move every Craft-to-Cloudflare decision into ImageTransformBehavior::toOptions(), which now takes the asset. Gravity precedence (explicit, focal point in crop mode, position) is now decided in one place.
- Keep fit=crop for PDFs and SVGs rasterized to another format. The gateway sends them through the Images binding, which rejects aspect-crop.
- Letterbox without a fill is transparent only when Craft's output format has alpha. JPEG output gets opaque white, as Craft's does.
- Drop quality only for an explicit png format. Without a format, the gateway negotiates AVIF or WebP, where quality applies.
- Test through toOptions() and getTransformUrl() instead of test-only subclasses.

Amp-Thread-ID: https://ampcode.com/threads/T-817bab5a-1757-4c07-ac06-409657afd942
Co-authored-by: Amp <amp@ampcode.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 00:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The separately deployed gateway is unavailable for review, so option-serialization tests cannot establish rendered-image parity for aspect-crop, box-center gravity, or SVG/PDF fitting.

No open issues 🎉

Review effort: Balanced

The gateway knows the final output format, including PNG chosen after
content negotiation or for rasterized PDFs, and drops quality there.

Amp-Thread-ID: https://ampcode.com/threads/T-ca8df760-164d-4331-a8d2-70b6bf00d90c
Co-authored-by: Amp <amp@ampcode.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 02:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

PNG losslessness depends on an uninspected, separately deployed cloud-gateway change, so rollout order and rendered PNG/PDF/SVG behavior require human verification beyond local option and URL assertions.

No open issues 🎉

Review effort: Balanced

The gateway decides which assets go through the Images binding, so it now handles aspect-crop there. The plugin always sends aspect-crop for crop mode without upscaling.

Amp-Thread-ID: https://ampcode.com/threads/T-817bab5a-1757-4c07-ac06-409657afd942
Co-authored-by: Amp <amp@ampcode.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 03:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The required cloud-gateway #293 production deployment could not be verified because the linked PR returned 404, leaving PDF/SVG aspect-crop requests at risk of HTTP 400 errors.

No open issues 🎉

Review effort: Balanced

@timkelty
timkelty merged commit f44d044 into 3.x Nov 26, 2026
10 checks passed
@linear-code

linear-code Bot commented Nov 26, 2026

Copy link
Copy Markdown

CLOUD-782

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Focal points produce different crops on Craft Cloud

2 participants