Skip to content

feat(manganis): add with_width/with_height for auto aspect-ratio image sizing#5606

Open
theonlyhennygod wants to merge 1 commit into
DioxusLabs:mainfrom
theonlyhennygod:feat/auto-width-height-image
Open

feat(manganis): add with_width/with_height for auto aspect-ratio image sizing#5606
theonlyhennygod wants to merge 1 commit into
DioxusLabs:mainfrom
theonlyhennygod:feat/auto-width-height-image

Conversation

@theonlyhennygod

Copy link
Copy Markdown

Summary

Closes #5458

Adds the ability to specify only one dimension (with_width or with_height) when resizing image assets, and have the other dimension automatically derived from the image's aspect ratio at build time.

Changes

packages/manganis/manganis-core/src/images.rs

  • Added ImageSize::WidthOnly { width } and ImageSize::HeightOnly { height } variants to the ImageSize enum
  • Added with_width(width: u32) and with_height(height: u32) const methods to AssetOptionsBuilder<ImageAssetOptions>

packages/cli/src/opt/image.rs

  • Updated process_image to handle the new ImageSize variants by reading the decoded image's actual dimensions and computing the missing dimension from the aspect ratio

Usage

// Resize to a specific width, height derived from aspect ratio
pub const RESIZED_PNG_ASSET: Asset =
    asset!("/assets/image.png", AssetOptions::image().with_width(52));

// Resize to a specific height, width derived from aspect ratio
pub const RESIZED_PNG_ASSET: Asset =
    asset!("/assets/image.png", AssetOptions::image().with_height(52));

// Existing API still works — both dimensions explicit
pub const RESIZED_PNG_ASSET: Asset =
    asset!("/assets/image.png", AssetOptions::image().with_size(ImageSize::Manual { width: 512, height: 512 }));

Implementation Notes

  • The aspect ratio is computed from the decoded image at build time (in the CLI processor), so the original image must be readable when dx runs
  • Uses resize_exact (same as Manual) for consistency
  • The new ImageSize variants are #[repr(C, u8)] compatible with the existing serialization infrastructure
  • All methods are const fn to match the existing API convention

…e sizing

Add WidthOnly and HeightOnly variants to ImageSize enum, enabling
users to specify only one dimension and have the other derived from
the image's aspect ratio at build time.

Closes DioxusLabs#5458
@theonlyhennygod theonlyhennygod requested a review from a team as a code owner June 2, 2026 03:41

@ealmloff ealmloff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Adding an enum variant is the right change for a non-breaking release, but since we have already cut over to 0.8 alphas, we can simplify this api

height: u32,
},
/// Only the width is specified; the height will be derived from the image's aspect ratio
WidthOnly {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since we now support any combination, the struct can be width: Option, height: Option now with methods to create different variants

if let Ok(image) = &mut image {
if let ImageSize::Manual { width, height } = image_options.size() {
*image = image.resize_exact(width, height, image::imageops::FilterType::Lanczos3);
match image_options.size() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

changing to optional fields simplifies this logic to manual_dim.unwrap_or_else(existing_dim)

@ealmloff ealmloff added enhancement New feature or request manganis Related to the manganis crate labels Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request manganis Related to the manganis crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto width/height for manganis images

2 participants