函数文档

image_constrain_size_for_editor()

💡 云策文档标注

概述

image_constrain_size_for_editor() 函数用于根据指定尺寸和上下文,计算图像在编辑器或主题中显示时的约束尺寸。它支持多种图像尺寸参数,并可通过 'editor_max_image_size' 过滤器进行自定义。

关键要点

  • 函数接受宽度、高度、尺寸和上下文参数,返回一个包含最大宽度和高度的数组。
  • 尺寸参数可以是数组(直接指定宽高)或字符串(如 'thumb'、'medium'、'medium_large'、'large' 或注册的自定义尺寸)。
  • 上下文参数默认为 'edit'(在管理界面)或 'display'(在主题中),影响尺寸计算逻辑。
  • 函数内部使用 wp_constrain_dimensions() 计算最终尺寸,并应用 'editor_max_image_size' 过滤器。

代码示例

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' );
        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 ) {
        $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'];
        if ( (int) $content_width > 0 && 'edit' === $context ) {
            $max_width = min( (int) $content_width, $max_width );
        }
    } else {
        $max_width  = $width;
        $max_height = $height;
    }
    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 );
}

注意事项

  • 当尺寸为 'full' 时,函数不会约束图像尺寸,直接返回原始宽高。
  • 对于 'medium_large' 和 'large' 尺寸,如果设置了 $content_width,会将其作为最大宽度限制。
  • 自定义图像尺寸仅在 'edit' 上下文中考虑 $content_width 限制。

📄 原文内容

Scales down the default size of an image.

Description

This is so that the image is a better fit for the editor and theme.

The $size parameter accepts either an array or a string. The supported string values are ‘thumb’ or ‘thumbnail’ for the given thumbnail size or defaults at 128 width and 96 height in pixels. Also supported for the string value is ‘medium’, ‘medium_large’ and ‘full’. The ‘full’ isn’t actually supported, but any value other than the supported will result in the content_width size or 500 if that is not set.

Finally, there is a filter named ‘editor_max_image_size’, that will be called on the calculated array for width and height, respectively.

Parameters

$widthintrequired
Width of the image in pixels.
$heightintrequired
Height of the image in pixels.
$sizestring|int[]optional
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'medium'.
$contextstringoptional
Could be 'display' (like in a theme) or 'edit' (like inserting into an editor).

Default:null

Return

int[] An array of width and height values.

  • 0 int
    The maximum width in pixels.
  • 1 int
    The maximum height in pixels.

Source

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 );
}

Hooks

apply_filters( ‘editor_max_image_size’, int[] $max_image_size, string|int[] $size, string $context )

Filters the maximum image size dimensions for the editor.

Changelog

Version Description
2.5.0 Introduced.