gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Disable Image Resizing

Open silaskoehler opened this issue 4 years ago • 2 comments

What problem does this address?

I can't manage to disable image resizing completely for image Blocks? I know that a parent block can provide a context allowResize, but what can i do to disable image resizing completely for my site?

What is your proposed solution?

Image resizing is very powerful but ist not compatible with every block style. Im My case i use :before and :after pseudo-classes to add decoration to my image. If the images don't fill the full width, my :before element is in the wrong place.

silaskoehler avatar Feb 02 '22 16:02 silaskoehler

I don't know of a way currently that you can use to disable image resizing on a site-wide basis. In theory, when the height and width block supports are implemented it may be possible to refactor the Image block to use these, which would add the potential to disable this at the individual theme.json level.

In the meantime, another option would be to include something like the following editor styles in a site theme:

.block-editor-image-size-control,
.wp-block-image .components-resizable-box__container.has-show-handle .components-resizable-box__handle-right,
.wp-block-image .components-resizable-box__container.has-show-handle .components-resizable-box__handle-bottom {
	display: none;
}

glendaviesnz avatar Feb 02 '22 21:02 glendaviesnz

Hey @silaskoehler @glendaviesnz I know this is super old but I ran into the same thing and can't find anywhere else someone is providing a non-CSS solution. The BlockEdit filter can be used to disable all size-related controls for the image block using the context prop like you saw, but may remove more than one would be looking for, such as aspect ratio:


import { createHigherOrderComponent } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';

/**
 * Modify block BlockEdit.
 */
const withBlockEditExtensions = createHigherOrderComponent( ( BlockEdit ) => {
	return ( props ) => {
		// Disable sizing controls.
		if ( 'core/image' === props.name ) {
			props.context.allowResize = false;
		}

		return <BlockEdit key="edit" { ...props } />;
	};
}, 'withBlockEditExtensions' );

addFilter(
	'editor.BlockEdit',
	`my-plugin/core-image-block-edit`,
	withBlockEditExtensions
);

Cheers yo

JordanPak avatar Mar 27 '26 21:03 JordanPak