Title: wp_constrain_dimensions
Published: April 25, 2014
Last modified: May 20, 2026

---

# wp_constrain_dimensions( int $current_width, int $current_height, int $max_width, int $max_height ): int[]

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#wp--skip-link--target)

Calculates the new dimensions for a down-sampled image.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#description)󠁿

If either width or height are empty, no constraint is applied on that dimension.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#parameters)󠁿

 `$current_width`intrequired

Current width of the image.

`$current_height`intrequired

Current height of the image.

`$max_width`intoptional

Max width in pixels to constrain to. Default 0.

`$max_height`intoptional

Max height in pixels to constrain to. Default 0.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#return)󠁿

 int[] An array of width and height values.

 * `0` int
 * The width in pixels.
 * `1` int
 * The height in pixels.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#source)󠁿

    ```php
    function wp_constrain_dimensions( $current_width, $current_height, $max_width = 0, $max_height = 0 ) {
    	if ( ! $max_width && ! $max_height ) {
    		return array( $current_width, $current_height );
    	}

    	$width_ratio  = 1.0;
    	$height_ratio = 1.0;
    	$did_width    = false;
    	$did_height   = false;

    	if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
    		$width_ratio = $max_width / $current_width;
    		$did_width   = true;
    	}

    	if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) {
    		$height_ratio = $max_height / $current_height;
    		$did_height   = true;
    	}

    	// Calculate the larger/smaller ratios.
    	$smaller_ratio = min( $width_ratio, $height_ratio );
    	$larger_ratio  = max( $width_ratio, $height_ratio );

    	if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
    		// The larger ratio is too big. It would result in an overflow.
    		$ratio = $smaller_ratio;
    	} else {
    		// The larger ratio fits, and is likely to be a more "snug" fit.
    		$ratio = $larger_ratio;
    	}

    	// Very small dimensions may result in 0, 1 should be the minimum.
    	$w = max( 1, (int) round( $current_width * $ratio ) );
    	$h = max( 1, (int) round( $current_height * $ratio ) );

    	/*
    	 * Sometimes, due to rounding, we'll end up with a result like this:
    	 * 465x700 in a 177x177 box is 117x176... a pixel short.
    	 * We also have issues with recursive calls resulting in an ever-changing result.
    	 * Constraining to the result of a constraint should yield the original result.
    	 * Thus we look for dimensions that are one pixel shy of the max value and bump them up.
    	 */

    	// Note: $did_width means it is possible $smaller_ratio == $width_ratio.
    	if ( $did_width && $w === $max_width - 1 ) {
    		$w = $max_width; // Round it up.
    	}

    	// Note: $did_height means it is possible $smaller_ratio == $height_ratio.
    	if ( $did_height && $h === $max_height - 1 ) {
    		$h = $max_height; // Round it up.
    	}

    	/**
    	 * Filters dimensions to constrain down-sampled images to.
    	 *
    	 * @since 4.1.0
    	 *
    	 * @param int[] $dimensions     {
    	 *     An array of width and height values.
    	 *
    	 *     @type int $0 The width in pixels.
    	 *     @type int $1 The height in pixels.
    	 * }
    	 * @param int   $current_width  The current width of the image.
    	 * @param int   $current_height The current height of the image.
    	 * @param int   $max_width      The maximum width permitted.
    	 * @param int   $max_height     The maximum height permitted.
    	 */
    	return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/media.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/media.php#L448)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/media.php#L448-L519)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_constrain_dimensions’, int[] $dimensions, int $current_width, int $current_height, int $max_width, int $max_height )](https://developer.wordpress.org/reference/hooks/wp_constrain_dimensions/)

Filters dimensions to constrain down-sampled images to.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#related)󠁿

| Uses | Description | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [wp_image_matches_ratio()](https://developer.wordpress.org/reference/functions/wp_image_matches_ratio/)`wp-includes/media.php` |

Helper function to test if aspect ratios for two images match.

  | 
| [wp_shrink_dimensions()](https://developer.wordpress.org/reference/functions/wp_shrink_dimensions/)`wp-admin/includes/deprecated.php` |

Calculates the new dimensions for a downsampled image.

  | 
| [get_udims()](https://developer.wordpress.org/reference/functions/get_udims/)`wp-admin/includes/deprecated.php` |

Calculates the new dimensions for a downsampled image.

  | 
| [wp_image_editor()](https://developer.wordpress.org/reference/functions/wp_image_editor/)`wp-admin/includes/image-edit.php` |

Loads the WP image-editing interface.

  | 
| [wp_expand_dimensions()](https://developer.wordpress.org/reference/functions/wp_expand_dimensions/)`wp-includes/media.php` |

Based on a supplied width/height example, returns the biggest possible dimensions based on the max width/height.

  | 
| [image_resize_dimensions()](https://developer.wordpress.org/reference/functions/image_resize_dimensions/)`wp-includes/media.php` |

Retrieves calculated resize dimensions for use in [WP_Image_Editor](https://developer.wordpress.org/reference/classes/wp_image_editor/).

  | 
| [image_constrain_size_for_editor()](https://developer.wordpress.org/reference/functions/image_constrain_size_for_editor/)`wp-includes/media.php` |

Scales down the default size of an image.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_constrain_dimensions/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_constrain_dimensions%2F)
before being able to contribute a note or feedback.