# image_constrain_size_for_editor()

URL: https://chugunov.pro/api-wordpress/functions/image_constrain_size_for_editor/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.5.0.

## Сигнатура

```php
image_constrain_size_for_editor( int $width, int $height, string|int[] $size = 'medium', string $context = null ): int[]
```

## Описание

Это нужно, чтобы изображение лучше вписывалось в редактор и тему.
Параметр $size принимает либо массив, либо строку. Поддерживаемые строковые значения — 'thumb' или 'thumbnail' для заданного размера миниатюры или значения по умолчанию 128 пикселей в ширину и 96 в высоту. Также поддерживаются строковые значения 'medium', 'medium_large' и 'full'. Значение 'full' на самом деле не поддерживается, но любое значение, кроме поддерживаемых, приведёт к использованию размера content_width или 500, если он не задан.
Наконец, есть фильтр 'editor_max_image_size', который вызывается для вычисленного массива ширины и высоты соответственно.

## Параметры

- `$width` `int` — обязательный. Ширина изображения в пикселях.
- `$height` `int` — обязательный. Высота изображения в пикселях.
- `$size` `string|int[]` — необязательный, по умолчанию `'medium'`. Размер изображения. Принимает имя любого зарегистрированного размера изображения или массив значений ширины и высоты в пикселях (именно в таком порядке). Значение по умолчанию 'medium'.
- `$context` `string` — необязательный, по умолчанию `null`. Может быть 'display' (как в теме) или 'edit' (как при вставке в редактор).

## Возвращаемое значение

`int[]` — 0 intМаксимальная ширина в пикселях. 1 intМаксимальная высота в пикселях.

## Исходный код

Файл: `wp-includes/media.php:65`

```php
function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) {
	global $content_width;

	$_wp_additional_image_sizes = wp_get_additional_image_sizes();

	if ( ! $context ) {
		$context = is_admin() ? 'edit' : 'display';
	}

	if ( is_array( $size ) ) {
		$max_width  = $size[0];
		$max_height = $size[1];
	} elseif ( 'thumb' === $size || 'thumbnail' === $size ) {
		$max_width  = (int) get_option( 'thumbnail_size_w' );
		$max_height = (int) get_option( 'thumbnail_size_h' );
		// Last chance thumbnail size defaults.
		if ( ! $max_width && ! $max_height ) {
			$max_width  = 128;
			$max_height = 96;
		}
	} elseif ( 'medium' === $size ) {
		$max_width  = (int) get_option( 'medium_size_w' );
		$max_height = (int) get_option( 'medium_size_h' );

	} elseif ( 'medium_large' === $size ) {
		$max_width  = (int) get_option( 'medium_large_size_w' );
		$max_height = (int) get_option( 'medium_large_size_h' );

		if ( (int) $content_width > 0 ) {
			$max_width = min( (int) $content_width, $max_width );
		}
	} elseif ( 'large' === $size ) {
		/*
		 * We're inserting a large size image into the editor. If it's a really
		 * big image we'll scale it down to fit reasonably within the editor
		 * itself, and within the theme's content width if it's known. The user
		 * can resize it in the editor if they wish.
		 */
		$max_width  = (int) get_option( 'large_size_w' );
		$max_height = (int) get_option( 'large_size_h' );

		if ( (int) $content_width > 0 ) {
			$max_width = min( (int) $content_width, $max_width );
		}
	} elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) {
		$max_width  = (int) $_wp_additional_image_sizes[ $size ]['width'];
		$max_height = (int) $_wp_additional_image_sizes[ $size ]['height'];
		// Only in admin. Assume that theme authors know what they're doing.
		if ( (int) $content_width > 0 && 'edit' === $context ) {
			$max_width = min( (int) $content_width, $max_width );
		}
	} else { // $size === 'full' has no constraint.
		$max_width  = $width;
		$max_height = $height;
	}

	/**
	 * Filters the maximum image size dimensions for the editor.
	 *
	 * @since 2.5.0
	 *
	 * @param int[]        $max_image_size {
	 *     An array of width and height values.
	 *
	 *     @type int $0 The maximum width in pixels.
	 *     @type int $1 The maximum height in pixels.
	 * }
	 * @param string|int[] $size     Requested image size. Can be any registered image size name, or
	 *                               an array of width and height values in pixels (in that order).
	 * @param string       $context  The context the image is being resized for.
	 *                               Possible values are 'display' (like in a theme)
	 *                               or 'edit' (like inserting into an editor).
	 */
	list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context );

	return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
}
```

## История изменений

- 2.5.0 — Introduced.

## Связанные

Использует: [`wp_get_additional_image_sizes`](https://chugunov.pro/api-wordpress/functions/wp_get_additional_image_sizes/), [`wp_constrain_dimensions`](https://chugunov.pro/api-wordpress/functions/wp_constrain_dimensions/), [`is_admin`](https://chugunov.pro/api-wordpress/functions/is_admin/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`get_option`](https://chugunov.pro/api-wordpress/functions/get_option/).
Используется в: [`wp_prepare_attachment_for_js`](https://chugunov.pro/api-wordpress/functions/wp_prepare_attachment_for_js/), [`image_get_intermediate_size`](https://chugunov.pro/api-wordpress/functions/image_get_intermediate_size/), [`image_downsize`](https://chugunov.pro/api-wordpress/functions/image_downsize/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/image_constrain_size_for_editor/
