WP_REST_Attachments_Controller::validate_image_dimensionsprivateWP 7.1.0

Validates that uploaded image dimensions are appropriate for the specified image size.

Method of the class: WP_REST_Attachments_Controller{}

No Hooks.

Returns

true|WP_Error. True if valid, WP_Error if invalid.

Usage

// private - for code of main (parent) class only
$result = $this->validate_image_dimensions( $width, $height, $image_size, $attachment_id );
$width(int) (required)
Uploaded image width.
$height(int) (required)
Uploaded image height.
$image_size(string) (required)
The target image size name.
$attachment_id(int) (required)
The attachment ID.

Changelog

Since 7.1.0 Introduced.

WP_REST_Attachments_Controller::validate_image_dimensions() code WP 7.1

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',
			sprintf(
				/* translators: 1: Image size name, 2: maximum width, 3: actual width. */
				__( 'Uploaded image width (%3$d) exceeds maximum for "%1$s" size (%2$d).' ),
				$image_size,
				$max_width,
				$width
			),
			array( 'status' => 400 )
		);
	}

	if ( $this->dimension_exceeds_max( $height, $max_height, $tolerance ) ) {
		return new WP_Error(
			'rest_upload_dimension_mismatch',
			sprintf(
				/* translators: 1: Image size name, 2: maximum height, 3: actual height. */
				__( 'Uploaded image height (%3$d) exceeds maximum for "%1$s" size (%2$d).' ),
				$image_size,
				$max_height,
				$height
			),
			array( 'status' => 400 )
		);
	}

	return true;
}