wppaste
WordPress

WP_REST_Attachments_Controller::validate_image_dimensions( int $width, int $height, string $image_size, int $attachment_id ): true|WP_Error

Since
7.1.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php:2620
Validates that uploaded image dimensions are appropriate for the specified image size.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$widthint
Uploaded image width.
$heightint
Uploaded image height.
$image_sizestring
The target image size name.
$attachment_idint
The attachment ID.

Return value

true|WP_Error
True if valid, WP_Error if invalid.

Performance profile

How much work a call to WP_REST_Attachments_Controller::validate_image_dimensions() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
13–52

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 130.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()one call below WP_REST_Attachments_Controller::validate_image_dimensions()
  • hookthird-party callbacksapply_filters()one call below WP_REST_Attachments_Controller::validate_image_dimensions()
  • optionoption read or writeget_option()one call below WP_REST_Attachments_Controller::validate_image_dimensions()

Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Attachments_Controller::validate_image_dimensions() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$width && !$height && $image_size !== "original"13–15none
always15–17__()
!$width && !$height && $image_size === "original"18–36wp_get_attachment_metadata()
!$width && !$height && $image_size !== "original" && !$image_size && $image_size !== "animated_video_poster" && !isset($registered_sizes[$image_size])28wp_get_registered_image_subsizes(), __()
!$width && !$height && $image_size !== "original" && !$image_size && $image_size !== "animated_video_poster" && isset($registered_sizes[$image_size]) && !->dimension_exceeds_max()38wp_get_registered_image_subsizes(), ->dimension_exceeds_max(), ->dimension_exceeds_max()
!$width && !$height && $image_size !== "original" && !$image_size && $image_size !== "animated_video_poster" && isset($registered_sizes[$image_size]) && ->dimension_exceeds_max()46wp_get_registered_image_subsizes(), ->dimension_exceeds_max(), __(), sprintf()
!$width && !$height && $image_size === "original" && is_array($metadata) && isset($metadata)49–51wp_get_attachment_metadata(), __(), sprintf()
!$width && !$height && $image_size !== "original" && !$image_size && $image_size !== "animated_video_poster" && isset($registered_sizes[$image_size])52wp_get_registered_image_subsizes(), ->dimension_exceeds_max(), ->dimension_exceeds_max(), __(), sprintf()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 130 instructions, 13–52 executed per call, 15 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 5

Used by · 1

Source code

	private function validate_image_dimensions( int $width, int $height, string $image_size, int $attachment_id ) {		// All image sizes require positive dimensions.		if ( $width <= 0 || $height <= 0 ) {			return new WP_Error(				'rest_upload_invalid_dimensions',				__( 'Uploaded image must have positive dimensions.' ),				array( 'status' => 400 )			);		} 		/*		 * 'original' size: the full-size image that replaces the main file (see		 * sideload_item()/finalize_item()). The endpoint expects any EXIF		 * orientation to be applied to the image already, which can swap width		 * and height, so the dimensions must match the stored dimensions or be		 * their transpose.		 */		if ( 'original' === $image_size ) {			$metadata = wp_get_attachment_metadata( $attachment_id, true );			if ( is_array( $metadata ) && isset( $metadata['width'], $metadata['height'] ) ) {				$expected_width  = (int) $metadata['width'];				$expected_height = (int) $metadata['height']; 				$matches_dimensions    = $width === $expected_width && $height === $expected_height;				$transposes_dimensions = $width === $expected_height && $height === $expected_width; 				if ( ! $matches_dimensions && ! $transposes_dimensions ) {					return new WP_Error(						'rest_upload_dimension_mismatch',						sprintf(							/* translators: 1: Actual width, 2: actual height, 3: expected width, 4: expected height. */							__( 'Uploaded image dimensions (%1$dx%2$d) do not match original image dimensions (%3$dx%4$d).' ),							$width,							$height,							$expected_width,							$expected_height						),						array( 'status' => 400 )					);				}			}			return true;		} 		// 'full' size (PDF thumbnails) and 'scaled': no further constraints.		if ( in_array( $image_size, array( 'full', 'scaled' ), true ) ) {			return true;		} 		/*		 * 'animated_video_poster' companion: a static poster image for the		 * converted video. It is a real image (so it has positive dimensions)		 * but is not a registered sub-size, so it has no dimension constraint.		 */		if ( 'animated_video_poster' === $image_size ) {			return true;		} 		// Regular image sizes: validate against registered size constraints.		$registered_sizes = wp_get_registered_image_subsizes(); 		if ( ! isset( $registered_sizes[ $image_size ] ) ) {			return new WP_Error(				'rest_upload_unknown_size',				__( 'Unknown image size.' ),				array( 'status' => 400 )			);		} 		$size_data  = $registered_sizes[ $image_size ];		$max_width  = (int) $size_data['width'];		$max_height = (int) $size_data['height']; 		// Validate dimensions don't exceed the registered size maximums.		// Allow 1px tolerance for rounding differences.		$tolerance = 1; 		if ( $this->dimension_exceeds_max( $width, $max_width, $tolerance ) ) {			return new WP_Error(				'rest_upload_dimension_mismatch',

Changelog

Introduced in 7.1.0.

Signature, return type and hooks compared across 1 parsed release.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.