image_constrain_size_for_editor

Definition:
function image_constrain_size_for_editor($width, $height, $size = 'medium') {}

Scale down the default size of an image.
This is so that the image is a better fit for the editor and theme.

Parameters

  • int $width: Width of the image
  • int $height: Height of the image
  • string|array $size: Size of what the result image should be.

Return values

returns:Width and height of what the result image should resize to.

Defined filters

  • editor_max_image_size
    apply_filters( 'editor_max_image_size', array( $max_width, $max_height )

Source code

function image_constrain_size_for_editor($width, $height, $size = 'medium') {

	global $content_width, $_wp_additional_image_sizes;



	if ( is_array($size) ) {

		$max_width = $size[0];

		$max_height = $size[1];

	}

	elseif ( $size == 'thumb' || $size == 'thumbnail' ) {

		$max_width = intval(get_option('thumbnail_size_w'));

		$max_height = intval(get_option('thumbnail_size_h'));

		// last chance thumbnail size defaults

		if ( !$max_width && !$max_height ) {

			$max_width = 128;

			$max_height = 96;

		}

	}

	elseif ( $size == 'medium' ) {

		$max_width = intval(get_option('medium_size_w'));

		$max_height = intval(get_option('medium_size_h'));

		// if no width is set, default to the theme content width if available

	}

	elseif ( $size == 'large' ) {

		// 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 = intval(get_option('large_size_w'));

		$max_height = intval(get_option('large_size_h'));

		if ( intval($content_width) > 0 )

			$max_width = min( intval($content_width), $max_width );

	} elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {

		$max_width = intval( $_wp_additional_image_sizes[$size]['width'] );

		$max_height = intval( $_wp_additional_image_sizes[$size]['height'] );

		if ( intval($content_width) > 0 && is_admin() ) // Only in admin. Assume that theme authors know what they're doing.

			$max_width = min( intval($content_width), $max_width );

	}

	// $size == 'full' has no constraint

	else {

		$max_width = $width;

		$max_height = $height;

	}



	list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size );



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

}

1983

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: