-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fill image transform setting #12214
Fill image transform setting #12214
Conversation
DEV-239 `fill` image transform setting
In these cases, if Possible values could be:
This will be useful for cases where you need something that looks like |
$useFill = $this->request->getBodyParam('useFill'); | ||
if ($useFill && $transform->mode === 'fit') { | ||
$transform->fill = $transform->fill ? ColorValidator::normalizeColor($transform->fill) : 'transparent'; | ||
} else { | ||
$transform->fill = null; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not wild about this, but I couldn't think of a better way to allow users to specify if they wanted the current behavior (image is sized proportionately to fit within a window) or the new behavior (image is placed within an area specified by the transform).
In code, the fact that fill
exists tells Craft which behavior to us, but in the UI a fill is always returned, and currently there's not a difference between no fill and a transparent one.
Adds an additional `fill` parameter to image transforms. The value of `fill` must be a value that the `ColorValidator` class accepts as valid (right now that means either 3 or 6 digit hex The fill is only applied when it is valid, the mode is set to `fit` and the upscale images setting is set to `false` OR when the ratio of the transform differs from the ratio of the image itself. In that scenario, blank pixels between the size of the source image and the specified transform will be filled with the color specified (using the image’s color space or palette). Indexes in the `imagetransformindex` table will now have the fill appended to the end of the string. Generated folders will also have the fill color appended (minus the `#` character).
When applying the fill and a position is provided it will be used to place the image within the transform size specified
Allows image to be scaled down to fit a ratio not equal to its intrinsic ration, but still apply the fill.
b81d5f8
to
232b30a
Compare
src/base/Image.php
Outdated
abstract public function scaleToFit(?int $targetWidth, ?int $targetHeight, bool $scaleIfSmaller = true): self; | ||
abstract public function scaleToFit(?int $targetWidth, ?int $targetHeight, bool $scaleIfSmaller = true, string $position = 'center-center'): self; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing @param
for $position
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also if the fill stuff is exclusive to Raster
, it feels weird to have $position
defined here in the base class. Maybe Raster
should just be defining its own scaleToFitAndFill()
method that combines scaleToFit()
and setFill()
?
src/image/Raster.php
Outdated
public function scaleToFit(?int $targetWidth, ?int $targetHeight, bool $scaleIfSmaller = true): self | ||
public function scaleToFit(?int $targetWidth, ?int $targetHeight, bool $scaleIfSmaller = true, array|string $position = 'center-center'): self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$position
has a different type declaration here than in the base class (array|string
vs string
).
….com:craftcms/cms into feature/dev-239-fill-image-transform-setting
# Conflicts: # src/helpers/ImageTransforms.php # src/imagetransforms/ImageTransformer.php
@brianjhanson This doesn’t seem to be working for me. I created a transform with the following settings:
However when I output a 1200x394 image using this transform, the result is 300x99 without any red pixels. I would have expected it to end up as 300x200, with ~50 red pixels above and below the original image. ( And separately, looks like there’s a bug when Fill Color is left blank: when you go back to the transform edit page, the input value is set to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed,
- Let’s introduce a new dedicated “Letterbox” transform mode for this, with “Fill Color” and “Gravity” settings. (Gravity should default to
center-center
.) - All transforms should get a new “Upscale” setting, and the
upscaleImages
config setting should be used by default.
Just pushed some code to address the first half of this and wanted to jot down some notes before I forget. PositioningI went with "Position" instead of "Gravity" because that felt more straightforward. In Cloudinary world, Gravity is how they refer to a focal point, but we have established language for that. We could keep it as "Focal Point" here, but given how it behaves, I think "position" is a better description (and aligns with how to determine it programmatically). I duplicated the field rather than just messing with it via JS because JS introduced some complications (translations, accessibility, etc.). It felt like just duplicating the field was better all around. UI UpdateFocal PointsIf an image has a focal point set, we will do our best to put that point into the center. Some screenshots of how this behaves are below. |
…ll-image-transform-setting
Use `_ns` to denote transforms that don't allow upscaling. The abscense of `_ns` means upscaling is allowed
[ci skip]
…//github.com/craftcms/cms into feature/dev-239-fill-image-transform-setting
Description
Adds an additional
fill
parameter to image transforms.The value of
fill
must be either a hex value (3 or 6 digits) or the stringtransparent
.When the transform mode is set to
fit
and a fill color is provided, the fill color will be used to fill any empty pixels surrounding the image so the final image size is always equal to the transform sized passed in.You can also use
position
in order to control where within the transform space the image should appear.Within the UI there's a new "Fill empty pixels" checkbox that will show or hide the color field for the fill.
CleanShot.2022-10-27.at.14.01.57.mp4
Related issues
#8848
To-Do