wp_constrain_dimensions

The timeline below displays how wordpress function wp_constrain_dimensions has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.4

/**
 * Calculates the new dimensions for a down-sampled image.
 *
 * If either width or height are empty, no constraint is applied on
 * that dimension.
 *
 * @since 2.5.0
 *
 * @param int $current_width  Current width of the image.
 * @param int $current_height Current height of the image.
 * @param int $max_width      Optional. Max width in pixels to constrain to. Default 0.
 * @param int $max_height     Optional. Max height in pixels to constrain to. Default 0.
 * @return int[] {
 *     An array of width and height values.
 *
 *     @type int $0 The width in pixels.
 *     @type int $1 The height in pixels.
 * }
 */
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);
}

WordPress Version: 5.3

/**
 * Calculates the new dimensions for a down-sampled image.
 *
 * If either width or height are empty, no constraint is applied on
 * that dimension.
 *
 * @since 2.5.0
 *
 * @param int $current_width  Current width of the image.
 * @param int $current_height Current height of the image.
 * @param int $max_width      Optional. Max width in pixels to constrain to. Default 0.
 * @param int $max_height     Optional. Max height in pixels to constrain to. Default 0.
 * @return array First item is the width, the second item is the height.
 */
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 array $dimensions     The image width and height.
     * @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);
}

WordPress Version: 5.1

/**
 * Calculates the new dimensions for a down-sampled image.
 *
 * If either width or height are empty, no constraint is applied on
 * that dimension.
 *
 * @since 2.5.0
 *
 * @param int $current_width  Current width of the image.
 * @param int $current_height Current height of the image.
 * @param int $max_width      Optional. Max width in pixels to constrain to. Default 0.
 * @param int $max_height     Optional. Max height in pixels to constrain to. Default 0.
 * @return array First item is the width, the second item is the height.
 */
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 = $height_ratio = 1.0;
    $did_width = $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 array $dimensions     The image width and height.
     * @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);
}

WordPress Version: 4.6

/**
 * Calculates the new dimensions for a down-sampled image.
 *
 * If either width or height are empty, no constraint is applied on
 * that dimension.
 *
 * @since 2.5.0
 *
 * @param int $current_width  Current width of the image.
 * @param int $current_height Current height of the image.
 * @param int $max_width      Optional. Max width in pixels to constrain to. Default 0.
 * @param int $max_height     Optional. Max height in pixels to constrain to. Default 0.
 * @return array First item is the width, the second item is the height.
 */
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 = $height_ratio = 1.0;
    $did_width = $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 array $dimensions     The image width and height.
     * @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);
}

WordPress Version: 4.2

/**
 * Calculates the new dimensions for a down-sampled image.
 *
 * If either width or height are empty, no constraint is applied on
 * that dimension.
 *
 * @since 2.5.0
 *
 * @param int $current_width  Current width of the image.
 * @param int $current_height Current height of the image.
 * @param int $max_width      Optional. Max width in pixels to constrain to. Default 0.
 * @param int $max_height     Optional. Max height in pixels to constrain to. Default 0.
 * @return array First item is the width, the second item is the height.
 */
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 = $height_ratio = 1.0;
    $did_width = $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
    }
    /**
     * Filter dimensions to constrain down-sampled images to.
     *
     * @since 4.1.0
     *
     * @param array $dimensions     The image width and height.
     * @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);
}

WordPress Version: 4.1

/**
 * Calculates the new dimensions for a downsampled image.
 *
 * If either width or height are empty, no constraint is applied on
 * that dimension.
 *
 * @since 2.5.0
 *
 * @param int $current_width Current width of the image.
 * @param int $current_height Current height of the image.
 * @param int $max_width Optional. Maximum wanted width.
 * @param int $max_height Optional. Maximum wanted height.
 * @return array First item is the width, the second item is the height.
 */
function wp_constrain_dimensions($current_width, $current_height, $max_width = 0, $max_height = 0)
{
    if (!$max_width and !$max_height) {
        return array($current_width, $current_height);
    }
    $width_ratio = $height_ratio = 1.0;
    $did_width = $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
    }
    return apply_filters('wp_constrain_dimensions', array($w, $h), $current_width, $current_height, $max_width, $max_height);
}

WordPress Version: 3.7

/**
 * Calculates the new dimensions for a downsampled image.
 *
 * If either width or height are empty, no constraint is applied on
 * that dimension.
 *
 * @since 2.5.0
 *
 * @param int $current_width Current width of the image.
 * @param int $current_height Current height of the image.
 * @param int $max_width Optional. Maximum wanted width.
 * @param int $max_height Optional. Maximum wanted height.
 * @return array First item is the width, the second item is the height.
 */
function wp_constrain_dimensions($current_width, $current_height, $max_width = 0, $max_height = 0)
{
    if (!$max_width and !$max_height) {
        return array($current_width, $current_height);
    }
    $width_ratio = $height_ratio = 1.0;
    $did_width = $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 (intval($current_width * $larger_ratio) > $max_width || intval($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, intval($current_width * $ratio));
    $h = max(1, intval($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
    if ($did_width && $w == $max_width - 1) {
        $w = $max_width;
    }
    // Round it up
    if ($did_height && $h == $max_height - 1) {
        $h = $max_height;
    }
    // Round it up
    return array($w, $h);
}