Commit a2f282a1d31 for woocommerce

commit a2f282a1d3137d899fbc9a36e2540aa1a9c573ad
Author: Allison Levine <1689238+allilevine@users.noreply.github.com>
Date:   Tue Nov 17 16:59:53 2026 -0500

    [Email Editor] Serve server-cropped files for cropped Image blocks (#71064)

    * Serve server-cropped files for cropped Image blocks in email

    * Add changelog entry for the email Image block crop fix

    * Crop email Image blocks only for pixel widths and test the ratio parser

    * Reword the email Image block crop changelog entry

diff --git a/packages/php/email-editor/changelog/fix-nl-1151-image-aspect-ratio-crop b/packages/php/email-editor/changelog/fix-nl-1151-image-aspect-ratio-crop
new file mode 100644
index 00000000000..54a1ecd6d01
--- /dev/null
+++ b/packages/php/email-editor/changelog/fix-nl-1151-image-aspect-ratio-crop
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Add a filter that lets image CDNs serve cropped files for Image blocks in emails.
diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-gallery.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-gallery.php
index 5381ca3b6ce..8d8ef005d8a 100644
--- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-gallery.php
+++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-gallery.php
@@ -103,7 +103,7 @@ class Gallery extends Abstract_Block_Renderer {
 			// Use the per-image override only when it is a valid ratio; a malformed override falls
 			// back to the gallery ratio instead of silently disabling the crop for that image.
 			$image_ratio  = isset( $image_attrs['aspectRatio'] ) && is_string( $image_attrs['aspectRatio'] ) ? $image_attrs['aspectRatio'] : null;
-			$aspect_ratio = ( null !== $image_ratio && null !== $this->parse_aspect_ratio( $image_ratio ) ) ? $image_ratio : $gallery_aspect_ratio;
+			$aspect_ratio = ( null !== $image_ratio && null !== Html_Processing_Helper::parse_aspect_ratio( $image_ratio ) ) ? $image_ratio : $gallery_aspect_ratio;

 			$cell_width      = $this->get_cell_width( $index, $image_count, $columns, $layout_width );
 			$extracted_image = $this->extract_image_from_html( $block['innerHTML'], $aspect_ratio, $cell_width, $image_attrs );
@@ -275,7 +275,7 @@ class Gallery extends Abstract_Block_Renderer {

 		// Only accept simple numeric ratios such as "1", "1.5" or "4/3" to avoid injecting anything unexpected.
 		$aspect_ratio = trim( $aspect_ratio );
-		$ratio_value  = $this->parse_aspect_ratio( $aspect_ratio );
+		$ratio_value  = Html_Processing_Helper::parse_aspect_ratio( $aspect_ratio );
 		if ( null === $ratio_value ) {
 			return $img_html;
 		}
@@ -285,10 +285,8 @@ class Gallery extends Abstract_Block_Renderer {
 			return $img_html;
 		}

-		// Derive the target display dimensions from the cell width and the requested ratio. Clamp the
-		// height to at least 1px so a very wide ratio can't round down to a 0-height (unusable) crop.
 		$width  = $cell_width > 0 ? $cell_width : 0;
-		$height = $width > 0 ? max( 1, (int) round( $width / $ratio_value ) ) : 0;
+		$height = Html_Processing_Helper::get_crop_height( $width, $ratio_value );

 		// get_attribute() can return a string, null (absent), or bool true (valueless attribute);
 		// coerce anything that isn't a real URL string to an empty string.
@@ -312,17 +310,8 @@ class Gallery extends Abstract_Block_Renderer {
 		 */
 		$filtered_url = apply_filters( 'woocommerce_email_editor_gallery_cropped_image_url', $image_url, $aspect_ratio, $width, $height, $image_attrs );

-		// Extensions can return anything (arrays, WP_Error, objects). A crop happened only when an
-		// integration returned a *different*, non-empty string. Compare the raw filter result against
-		// the original src here, BEFORE escaping: get_attribute() returns the entity-decoded src (raw
-		// "&"), while esc_url() re-encodes "&" to "&#038;", so comparing an escaped URL against the
-		// decoded original would misclassify any image whose src has a multi-param query string (e.g.
-		// "?w=600&h=600", common with image CDNs) as server-cropped.
-		$is_server_cropped = is_string( $filtered_url ) && '' !== $image_url && '' !== $filtered_url && $filtered_url !== $image_url;
-
-		// Escape for output only after the decision. If esc_url() reduces a hostile/invalid crop URL
-		// to an empty string, fall through to the CSS branch rather than emit an empty src.
-		$cropped_url = $is_server_cropped ? esc_url( (string) $filtered_url ) : '';
+		// If esc_url() reduces a hostile crop URL to an empty string, this falls through to the CSS branch.
+		$cropped_url = Html_Processing_Helper::get_server_cropped_url( $filtered_url, $image_url );

 		// These crop styles are appended after Html_Processing_Helper::sanitize_image_html() has run
 		// (its style allowlist would otherwise strip object-fit), so they intentionally bypass that
@@ -353,28 +342,6 @@ class Gallery extends Abstract_Block_Renderer {
 		return $html->get_updated_html();
 	}

-	/**
-	 * Parse an aspect ratio attribute value (e.g. "1", "1.5", "4/3") into a numeric width/height ratio.
-	 *
-	 * @param string $aspect_ratio Aspect ratio value.
-	 * @return float|null The ratio (width divided by height), or null when the value is invalid.
-	 */
-	private function parse_aspect_ratio( string $aspect_ratio ): ?float {
-		// Only accept simple numeric ratios to avoid injecting anything unexpected into the markup.
-		if ( ! preg_match( '/^(\d+(?:\.\d+)?)(?:\s*\/\s*(\d+(?:\.\d+)?))?$/', trim( $aspect_ratio ), $matches ) ) {
-			return null;
-		}
-
-		$numerator   = (float) $matches[1];
-		$denominator = isset( $matches[2] ) ? (float) $matches[2] : 1.0;
-
-		if ( $numerator <= 0 || $denominator <= 0 ) {
-			return null;
-		}
-
-		return $numerator / $denominator;
-	}
-
 	/**
 	 * Extract gallery-level caption from the original block content.
 	 *
diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php
index 20665894f83..ebf2e5e5a4a 100644
--- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php
+++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php
@@ -10,6 +10,7 @@ namespace Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks;

 use Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Rendering_Context;
 use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Dom_Document_Helper;
+use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Html_Processing_Helper;
 use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Styles_Helper;
 use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Table_Wrapper_Helper;

@@ -41,6 +42,7 @@ class Image extends Abstract_Block_Renderer {

 		$parsed_block = $this->add_image_size_when_missing( $parsed_block, $image_url );
 		$image        = $this->add_image_dimensions( $image, $parsed_block );
+		$image        = $this->apply_server_crop( $image, $parsed_block );

 		$image_with_wrapper = str_replace(
 			array( '{image_content}', '{caption_content}' ),
@@ -358,6 +360,67 @@ class Image extends Abstract_Block_Renderer {
 		return $block_content;
 	}

+	/**
+	 * Swap a cropped image for a server-cropped file when an integration provides one.
+	 *
+	 * The block's crop is saved as `aspect-ratio` and `object-fit` CSS, which Gmail and Outlook ignore.
+	 * A file that is already cropped renders the same everywhere once it carries the crop's dimensions.
+	 * Without one the image keeps its own ratio, since a crop height on the full file would squash it.
+	 *
+	 * @param string $block_content Image HTML.
+	 * @param array  $parsed_block Parsed block.
+	 */
+	private function apply_server_crop( string $block_content, array $parsed_block ): string {
+		$attrs        = $parsed_block['attrs'] ?? array();
+		$aspect_ratio = isset( $attrs['aspectRatio'] ) && is_string( $attrs['aspectRatio'] ) ? trim( $attrs['aspectRatio'] ) : '';
+		$ratio_value  = Html_Processing_Helper::parse_aspect_ratio( $aspect_ratio );
+		$block_width  = isset( $attrs['width'] ) && is_scalar( $attrs['width'] ) ? trim( (string) $attrs['width'] ) : '';
+
+		// A contain scale letterboxes instead of cropping, and only a pixel width gives a size to crop to.
+		if ( null === $ratio_value || 'cover' !== ( $attrs['scale'] ?? null ) || ! preg_match( '/^\d+(?:\.\d+)?(?:px)?$/', $block_width ) ) {
+			return $block_content;
+		}
+
+		$html = new \WP_HTML_Tag_Processor( $block_content );
+		if ( ! $html->next_tag( array( 'tag_name' => 'img' ) ) ) {
+			return $block_content;
+		}
+
+		$src       = $html->get_attribute( 'src' );
+		$image_url = is_string( $src ) ? $src : '';
+		$width     = (int) round( Styles_Helper::parse_value( $block_width ) );
+		if ( '' === $image_url || $width <= 0 ) {
+			return $block_content;
+		}
+		$height = Html_Processing_Helper::get_crop_height( $width, $ratio_value );
+
+		/**
+		 * Filters the URL of a cropped image block so an integration can serve a server-cropped file.
+		 *
+		 * Takes the same arguments as `woocommerce_email_editor_gallery_cropped_image_url`, so one
+		 * callback can serve both. Return the URL unchanged to keep the uncropped image.
+		 *
+		 * @since 2.21.0
+		 *
+		 * @param string $image_url    The original image URL.
+		 * @param string $aspect_ratio The block's aspect ratio (e.g. "1", "4/3").
+		 * @param int    $width        Display width of the image in px.
+		 * @param int    $height       Display height the crop gives the image in px.
+		 * @param array  $attrs        Parsed attributes of the core/image block (id, sizeSlug, ...).
+		 */
+		$filtered_url = apply_filters( 'woocommerce_email_editor_image_cropped_image_url', $image_url, $aspect_ratio, $width, $height, $attrs );
+
+		$cropped_url = Html_Processing_Helper::get_server_cropped_url( $filtered_url, $image_url );
+		if ( '' === $cropped_url ) {
+			return $block_content;
+		}
+
+		$html->set_attribute( 'src', $cropped_url );
+		$html->set_attribute( 'width', (string) $width );
+		$html->set_attribute( 'height', (string) $height );
+		return $html->get_updated_html();
+	}
+
 	/**
 	 * Scale the image's saved height attribute to the width the email renders it at.
 	 *
diff --git a/packages/php/email-editor/src/Integrations/Utils/class-html-processing-helper.php b/packages/php/email-editor/src/Integrations/Utils/class-html-processing-helper.php
index 10c16c9d0af..d69586fe125 100644
--- a/packages/php/email-editor/src/Integrations/Utils/class-html-processing-helper.php
+++ b/packages/php/email-editor/src/Integrations/Utils/class-html-processing-helper.php
@@ -110,6 +110,57 @@ class Html_Processing_Helper {
 		return false !== strpos( $class_name, '-border-' );
 	}

+	/**
+	 * Parse an aspect ratio attribute value (e.g. "1", "1.5", "4/3") into a numeric width/height ratio.
+	 *
+	 * @param string $aspect_ratio Aspect ratio value.
+	 * @return float|null The ratio (width divided by height), or null when the value is invalid.
+	 */
+	public static function parse_aspect_ratio( string $aspect_ratio ): ?float {
+		// Only accept simple numeric ratios to avoid injecting anything unexpected into the markup.
+		if ( ! preg_match( '/^(\d+(?:\.\d+)?)(?:\s*\/\s*(\d+(?:\.\d+)?))?$/', trim( $aspect_ratio ), $matches ) ) {
+			return null;
+		}
+
+		$numerator   = (float) $matches[1];
+		$denominator = isset( $matches[2] ) ? (float) $matches[2] : 1.0;
+
+		if ( $numerator <= 0 || $denominator <= 0 ) {
+			return null;
+		}
+
+		return $numerator / $denominator;
+	}
+
+	/**
+	 * Get the height an aspect-ratio crop gives an image of the given width.
+	 *
+	 * @param int   $width Display width in px.
+	 * @param float $ratio_value Ratio from {@see self::parse_aspect_ratio()}.
+	 * @return int Height in px, at least 1 so a very wide ratio can't round down to nothing, or 0 when the width or ratio is unusable.
+	 */
+	public static function get_crop_height( int $width, float $ratio_value ): int {
+		return $width > 0 && $ratio_value > 0 ? max( 1, (int) round( $width / $ratio_value ) ) : 0;
+	}
+
+	/**
+	 * Check what a cropped-image-URL filter returned and escape it for output.
+	 *
+	 * A filter can return anything, so the crop counts only when it returned a different, non-empty
+	 * string. The comparison runs before escaping: get_attribute() decodes "&" and esc_url() re-encodes
+	 * it, so an escaped URL would never match a src with a multi-parameter query string.
+	 *
+	 * @param mixed  $filtered_url What the filter returned.
+	 * @param string $image_url The image's original, decoded src.
+	 * @return string The escaped cropped URL, or an empty string when the image wasn't cropped.
+	 */
+	public static function get_server_cropped_url( $filtered_url, string $image_url ): string {
+		if ( ! is_string( $filtered_url ) || '' === $image_url || '' === $filtered_url || $filtered_url === $image_url ) {
+			return '';
+		}
+		return esc_url( $filtered_url );
+	}
+
 	/**
 	 * Sanitize CSS value to prevent injection attacks.
 	 *
diff --git a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Image_Test.php b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Image_Test.php
index 3869a7e6c79..a1c96b01d12 100644
--- a/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Image_Test.php
+++ b/packages/php/email-editor/tests/integration/Integrations/Core/Renderer/Blocks/Image_Test.php
@@ -662,6 +662,166 @@ class Image_Test extends \Email_Editor_Integration_Test_Case {
 		$this->assertNull( $this->get_image_attribute( $rendered, 'height' ) );
 	}

+	/**
+	 * Test a cropped image passes its crop to the filter so an integration can serve a cropped file.
+	 */
+	public function testItPassesTheCropToTheCroppedImageUrlFilter(): void {
+		$parsed_image = $this->get_cropped_image_block( '4/3' );
+
+		$received = array();
+		$filter   = function ( $url, $aspect_ratio, $width, $height, $attrs ) use ( &$received ) {
+			$received = array(
+				'url'          => $url,
+				'aspect_ratio' => $aspect_ratio,
+				'width'        => $width,
+				'height'       => $height,
+				'attrs'        => $attrs,
+			);
+			return $url;
+		};
+		add_filter( 'woocommerce_email_editor_image_cropped_image_url', $filter, 10, 5 );
+		$this->image_renderer->render( $parsed_image['innerHTML'], $parsed_image, $this->rendering_context );
+		remove_filter( 'woocommerce_email_editor_image_cropped_image_url', $filter, 10 );
+
+		$this->assertSame( 'https://test.com/wp-content/uploads/2023/05/image.jpg', $received['url'] );
+		$this->assertSame( '4/3', $received['aspect_ratio'] );
+		$this->assertSame( 580, $received['width'] );
+		$this->assertSame( 435, $received['height'] );
+		$this->assertSame( 1, $received['attrs']['id'] );
+	}
+
+	/**
+	 * Test a server-cropped URL is used with the crop's dimensions.
+	 *
+	 * Gmail and Outlook ignore the saved aspect-ratio CSS, so the cropped file and its height
+	 * attribute are what make the crop show up there.
+	 */
+	public function testItUsesAServerCroppedUrlWithTheCropDimensions(): void {
+		$parsed_image = $this->get_cropped_image_block( '1' );
+
+		$filter = function ( $url, $aspect_ratio, $width, $height ) {
+			return $url . '?resize=' . $width . ',' . $height;
+		};
+		add_filter( 'woocommerce_email_editor_image_cropped_image_url', $filter, 10, 4 );
+		$rendered = $this->image_renderer->render( $parsed_image['innerHTML'], $parsed_image, $this->rendering_context );
+		remove_filter( 'woocommerce_email_editor_image_cropped_image_url', $filter, 10 );
+
+		$this->assertSame( 'https://test.com/wp-content/uploads/2023/05/image.jpg?resize=580,580', $this->get_image_attribute( $rendered, 'src' ) );
+		$this->assertSame( '580', $this->get_image_attribute( $rendered, 'width' ) );
+		$this->assertSame( '580', $this->get_image_attribute( $rendered, 'height' ) );
+	}
+
+	/**
+	 * Test an uncropped URL leaves the image at its own ratio.
+	 *
+	 * Writing the crop height onto the original file would squash it in Outlook.
+	 */
+	public function testItKeepsTheImageRatioWhenNothingCropsTheUrl(): void {
+		$parsed_image = $this->get_cropped_image_block( '1' );
+
+		$rendered = $this->image_renderer->render( $parsed_image['innerHTML'], $parsed_image, $this->rendering_context );
+
+		$this->assertSame( 'https://test.com/wp-content/uploads/2023/05/image.jpg', $this->get_image_attribute( $rendered, 'src' ) );
+		$this->assertSame( '580', $this->get_image_attribute( $rendered, 'width' ) );
+		$this->assertSame( '467', $this->get_image_attribute( $rendered, 'height' ) );
+		$this->assertStringContainsString( 'aspect-ratio:1', (string) $this->get_image_attribute( $rendered, 'style' ) );
+	}
+
+	/**
+	 * Test the filter is skipped when the block doesn't ask for a cover crop.
+	 *
+	 * A contain scale letterboxes rather than crops, and an unparseable ratio has no size to crop to.
+	 *
+	 * @dataProvider uncroppable_attrs_provider
+	 *
+	 * @param array $attrs Block attributes to apply over the cropped block.
+	 */
+	public function testItSkipsTheFilterWhenTheBlockIsNotCroppable( array $attrs ): void {
+		$parsed_image          = $this->get_cropped_image_block( '1' );
+		$parsed_image['attrs'] = array_merge( $parsed_image['attrs'], $attrs );
+
+		$calls  = 0;
+		$filter = function ( $url ) use ( &$calls ) {
+			++$calls;
+			return $url . '?cropped';
+		};
+		add_filter( 'woocommerce_email_editor_image_cropped_image_url', $filter );
+		$rendered = $this->image_renderer->render( $parsed_image['innerHTML'], $parsed_image, $this->rendering_context );
+		remove_filter( 'woocommerce_email_editor_image_cropped_image_url', $filter );
+
+		$this->assertSame( 0, $calls );
+		$this->assertStringNotContainsString( '?cropped', $rendered );
+	}
+
+	/**
+	 * Block attributes that must not trigger a server crop.
+	 *
+	 * @return array<string, array{0: array}>
+	 */
+	public function uncroppable_attrs_provider(): array {
+		return array(
+			'contain scale'    => array( array( 'scale' => 'contain' ) ),
+			'invalid ratio'    => array( array( 'aspectRatio' => '1;color:red' ) ),
+			'non-string ratio' => array( array( 'aspectRatio' => array( '1' ) ) ),
+			'percentage width' => array( array( 'width' => '50%' ) ),
+			'em width'         => array( array( 'width' => '20em' ) ),
+			'rem width'        => array( array( 'width' => '30rem' ) ),
+			'viewport width'   => array( array( 'width' => '50vw' ) ),
+		);
+	}
+
+	/**
+	 * Test a filter returning something other than a URL leaves the image uncropped.
+	 *
+	 * @dataProvider invalid_filter_result_provider
+	 *
+	 * @param mixed $result Value the filter returns.
+	 */
+	public function testItIgnoresAnInvalidCroppedImageUrl( $result ): void {
+		$parsed_image = $this->get_cropped_image_block( '1' );
+
+		$filter = function () use ( $result ) {
+			return $result;
+		};
+		add_filter( 'woocommerce_email_editor_image_cropped_image_url', $filter );
+		$rendered = $this->image_renderer->render( $parsed_image['innerHTML'], $parsed_image, $this->rendering_context );
+		remove_filter( 'woocommerce_email_editor_image_cropped_image_url', $filter );
+
+		$this->assertSame( 'https://test.com/wp-content/uploads/2023/05/image.jpg', $this->get_image_attribute( $rendered, 'src' ) );
+		$this->assertSame( '467', $this->get_image_attribute( $rendered, 'height' ) );
+	}
+
+	/**
+	 * Filter results that are not a usable cropped URL.
+	 *
+	 * @return array<string, array{0: mixed}>
+	 */
+	public function invalid_filter_result_provider(): array {
+		return array(
+			'null'            => array( null ),
+			'empty string'    => array( '' ),
+			'array'           => array( array( 'https://test.com/cropped.jpg' ) ),
+			'unsafe protocol' => array( 'javascript:alert(1)' ),
+		);
+	}
+
+	/**
+	 * Build an image block cropped from a 1003×807 file, the way core saves an Aspect ratio crop.
+	 *
+	 * @param string $aspect_ratio Saved aspect ratio.
+	 */
+	private function get_cropped_image_block( string $aspect_ratio ): array {
+		$parsed_image                         = $this->parsed_image;
+		$parsed_image['attrs']['aspectRatio'] = $aspect_ratio;
+		$parsed_image['attrs']['width']       = '580px';
+		$parsed_image['innerHTML']            = '
+			<figure class="wp-block-image size-full">
+				<img src="https://test.com/wp-content/uploads/2023/05/image.jpg" alt="" width="1003" height="807" style="aspect-ratio:' . $aspect_ratio . ';object-fit:cover"/>
+			</figure>
+		';
+		return $parsed_image;
+	}
+
 	/**
 	 * Read an attribute off the rendered image.
 	 *
diff --git a/packages/php/email-editor/tests/unit/Integrations/Utils/Html_Processing_Helper_Test.php b/packages/php/email-editor/tests/unit/Integrations/Utils/Html_Processing_Helper_Test.php
index c6886529786..14aabd6e7ce 100644
--- a/packages/php/email-editor/tests/unit/Integrations/Utils/Html_Processing_Helper_Test.php
+++ b/packages/php/email-editor/tests/unit/Integrations/Utils/Html_Processing_Helper_Test.php
@@ -818,4 +818,44 @@ class Html_Processing_Helper_Test extends \Email_Editor_Unit_Test {
 			$this->assertEquals( '', $result, 'Failed to reject invalid dimension: ' . $input_description );
 		}
 	}
+
+	/**
+	 * Test parse_aspect_ratio reads the ratio formats core saves.
+	 */
+	public function testParseAspectRatioReadsValidRatios(): void {
+		$this->assertSame( 1.0, Html_Processing_Helper::parse_aspect_ratio( '1' ) );
+		$this->assertSame( 1.5, Html_Processing_Helper::parse_aspect_ratio( '1.5' ) );
+		$this->assertSame( 4 / 3, Html_Processing_Helper::parse_aspect_ratio( '4/3' ) );
+		$this->assertSame( 16 / 9, Html_Processing_Helper::parse_aspect_ratio( ' 16 / 9 ' ) );
+	}
+
+	/**
+	 * Test parse_aspect_ratio rejects values that aren't a simple positive ratio.
+	 *
+	 * The ratio is interpolated into inline CSS, so anything beyond digits and one slash must be rejected.
+	 */
+	public function testParseAspectRatioRejectsInvalidRatios(): void {
+		$invalid = array( '', 'auto', '0', '0/1', '1/0', '-1', '4/3/2', '1;color:red', '1/1 !important', '4:3' );
+		foreach ( $invalid as $ratio ) {
+			$this->assertNull( Html_Processing_Helper::parse_aspect_ratio( $ratio ), "Accepted invalid ratio: {$ratio}" );
+		}
+	}
+
+	/**
+	 * Test get_crop_height returns the height an aspect-ratio crop gives an image.
+	 */
+	public function testGetCropHeightDividesTheWidthByTheRatio(): void {
+		$this->assertSame( 580, Html_Processing_Helper::get_crop_height( 580, 1.0 ) );
+		$this->assertSame( 435, Html_Processing_Helper::get_crop_height( 580, 4 / 3 ) );
+		$this->assertSame( 1, Html_Processing_Helper::get_crop_height( 1, 100.0 ), 'A very wide ratio never rounds down to 0.' );
+	}
+
+	/**
+	 * Test get_crop_height returns 0 instead of dividing by an unusable width or ratio.
+	 */
+	public function testGetCropHeightReturnsZeroForAnUnusableWidthOrRatio(): void {
+		$this->assertSame( 0, Html_Processing_Helper::get_crop_height( 0, 1.0 ) );
+		$this->assertSame( 0, Html_Processing_Helper::get_crop_height( 580, 0.0 ) );
+		$this->assertSame( 0, Html_Processing_Helper::get_crop_height( 580, -1.0 ) );
+	}
 }