Skip to content
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

Try core/image aspect ratio #51544

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre
- **Name:** core/image
- **Category:** media
- **Supports:** anchor, behaviors (lightbox), color (~~background~~, ~~text~~), filter (duotone)
- **Attributes:** align, alt, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, sizeSlug, title, url, width
- **Attributes:** align, alt, aspectRatio, caption, height, href, id, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width

## Latest Comments

Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@
"source": "attribute",
"selector": "figure > a",
"attribute": "target"
},
"aspectRatio": {
"type": "string"
},
"scale": {
"type": "string",
"default": "cover"
}
},
"supports": {
Expand Down
10 changes: 9 additions & 1 deletion packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export function ImageEdit( {
width,
height,
sizeSlug,
aspectRatio,
scale,
} = attributes;
const [ temporaryURL, setTemporaryURL ] = useState();

Expand Down Expand Up @@ -326,6 +328,10 @@ export function ImageEdit( {
const blockProps = useBlockProps( {
ref,
className: classes,
style: {
objectFit: aspectRatio ? scale : 'auto',
aspectRatio,
},
} );

// Much of this description is duplicated from MediaPlaceholder.
Expand All @@ -342,7 +348,9 @@ export function ImageEdit( {
instructions={ __(
'Upload an image file, pick one from your media library, or add one with a URL.'
) }
style={ isSelected ? undefined : borderProps.style }
style={ {
...borderProps.style,
} }
>
{ content }
</Placeholder>
Expand Down
33 changes: 31 additions & 2 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TextareaControl,
TextControl,
ToolbarButton,
SelectControl,
} from '@wordpress/components';
import { useViewportMatch, usePrevious } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -91,6 +92,8 @@ export default function Image( {
height,
linkTarget,
sizeSlug,
aspectRatio,
scale,
} = attributes;
const imageRef = useRef();
const prevCaption = usePrevious( caption );
Expand Down Expand Up @@ -260,6 +263,10 @@ export default function Image( {
setAttributes( { alt: newAlt } );
}

function updateAspectRatio( newAspectRatio ) {
setAttributes( { aspectRatio: newAspectRatio } );
}

function updateImage( newSizeSlug ) {
const newUrl = image?.media_details?.sizes?.[ newSizeSlug ]?.source_url;
if ( ! newUrl ) {
Expand Down Expand Up @@ -419,6 +426,22 @@ export default function Image( {
}
/>
) }
<SelectControl
label={ __( 'Aspect ratio' ) }
options={ [
{ label: __( 'Original' ), value: 'auto' },
{ label: __( 'Square - 1:1' ), value: '1' },
{ label: __( 'Classic - 3:2' ), value: '1.5' },
{ label: __( 'Standard - 4:3' ), value: '1.33' },
{ label: __( 'Portrait - 3:4' ), value: '0.75' },
{ label: __( 'Wide - 16:9' ), value: '1.77' },
{ label: __( 'Tall - 9:16' ), value: '0.562' },
{ label: __( 'Custom' ), value: 'custom' },
] }
value={ aspectRatio }
onChange={ updateAspectRatio }
size="__unstable-large"
/>
<ImageSizeControl
onChangeImage={ updateImage }
onChange={ ( value ) => setAttributes( value ) }
Expand Down Expand Up @@ -478,6 +501,11 @@ export default function Image( {
const hasCustomBorder =
!! borderProps.className ||
( borderProps.style && Object.keys( borderProps.style ).length > 0 );
const imageStyles = {
...borderProps.style,
aspectRatio,
objectFit: !! ( height || aspectRatio ) && scale,
};

let img = (
// Disable reason: Image itself is not meant to be interactive, but
Expand All @@ -496,7 +524,7 @@ export default function Image( {
} }
ref={ imageRef }
className={ borderProps.className }
style={ borderProps.style }
style={ imageStyles }
/>
{ temporaryURL && <Spinner /> }
</>
Expand Down Expand Up @@ -593,7 +621,7 @@ export default function Image( {
img = (
<ResizableBox
size={ {
width: width ?? 'auto',
width: 'auto',
height: height && ! hasCustomBorder ? height : 'auto',
} }
showHandle={ isSelected }
Expand All @@ -617,6 +645,7 @@ export default function Image( {
} );
} }
resizeRatio={ align === 'center' ? 2 : 1 }
styles={ { aspectRatio } }
>
{ img }
</ResizableBox>
Expand Down