You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Match Craft's native transformer for quality, fill, crop and focal points - #218
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
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.
Including PNG output. Craft saves PNGs losslessly, but Cloudflare takes any quality on PNG output as permission to use palette-based PNG8. ↩
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. ↩
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). ↩
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>
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>
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
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.
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
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.
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>
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.
- 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>
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.
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.
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>
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Cloud image transformer translated several Craft transform settings differently from Craft's native transformer (Craft 5.9.17
ImageTransforms::generateTransform()andRaster), so the same transform gave different images on Cloud.qualitynot setdefaultImageQuality(82)qualitysent, falling back todefaultImageQuality1letterbox, nofillbackground=#FFFFFF#FFFFFF2crop,upscale: falsescaleAndCrop()crops smaller images to the target aspect ratiofit=crop, which returns smaller images at their original size and shapefit=aspect-crop3cropmodepositionusedcropmodecropmoderemaindergravity keeps the point at the same relative position in the cropgravity[mode]=box-center4For 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)
padnever enlarges the image, soletterboxwithupscale: truecan't match Craft.gravityonly forcover,crop, andaspect-crop, sopositionon aletterboxtransform is probably ignored.upscale: falseand only one dimension still enlarges on Cloudflare (fit=aspect-crop,width=9000on 6000×4000 gives 9000×6000). That was already true withfit=crop.Related issues
Footnotes
Including PNG output. Craft saves PNGs losslessly, but Cloudflare takes any
qualityon PNG output as permission to use palette-based PNG8. ↩Cloud may serve AVIF or WebP where Craft would write JPEG, so the color has to match Craft's output, not Cloudflare's. A
filloftransparentfollows the same rule. Cloudflare documentsrgba()but not thetransparentkeyword. ↩Including PDFs, and SVGs with a raster
format, which the gateway rasterizes through the Images binding. That path acceptsaspect-cropsince craftcms/cloud-gateway#293 (gateway 1.67.5, in production). ↩box-centeris in workerd'scf.d.tsand Images binding types but not on Cloudflare's docs page. The gateway passesgravity[mode]through unchanged on both thecf.imageand Images binding paths. ↩