image_constrain_size_for_editor

函式
image_constrain_size_for_editor ( $width, $height, $size = 'medium', $context = null )
引數
  • (int) $width Width of the image in pixels.
    Required:
  • (int) $height Height of the image in pixels.
    Required:
  • (string|int[]) $size Optional. Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'medium'.
    Required:
    Default: 'medium'
  • (string) $context Optional. Could be 'display' (like in a theme) or 'edit' (like inserting into an editor). Default null.
    Required:
    Default: null
返回值
  • (int[]) { An array of width and height values. @type int $0 The maximum width in pixels. @type int $1 The maximum height in pixels. }
定義位置
相關方法
image_media_send_to_editorget_image_send_to_editormedia_send_to_editorformat_for_editorimage_resize_dimensions
引入
2.5.0
棄用
-

Image_constrain_size_for_editor: 這個函式用來限制在媒體庫中編輯影象時的尺寸。

縮小圖片的預設尺寸。

這是為了使圖片更適合於編輯器和主題。

`$size`引數接受一個陣列或一個字串。支援的字串值是’thumb’或’thumbnail’,用於給定的縮圖尺寸或預設的128寬度和96高度的畫素。支援的字串值還有’medium’、’medium_large’和’full’。實際上不支援’full’,但是除了支援的值之外,任何其他的值都會導致content_width大小,如果沒有設定,則為500。

最後,有一個名為{@see ‘editor_max_image_size’}的過濾器,它將分別對計算出的陣列的寬度和高度進行呼叫。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 );
}
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 ); }
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 );
}

常見問題

FAQs
檢視更多 >