diff --git a/projects/plugins/jetpack/changelog/fix-too-large-detected-image-size b/projects/plugins/jetpack/changelog/fix-too-large-detected-image-size new file mode 100644 index 0000000000000..0868b862bfdc6 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-too-large-detected-image-size @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Jetpack Sync: Avoid detecting invalid image size diff --git a/projects/plugins/jetpack/class.jetpack-post-images.php b/projects/plugins/jetpack/class.jetpack-post-images.php index b41f919869149..995272ddc84b2 100644 --- a/projects/plugins/jetpack/class.jetpack-post-images.php +++ b/projects/plugins/jetpack/class.jetpack-post-images.php @@ -519,7 +519,7 @@ public static function from_html( $html_or_id, $width = 200, $height = 200 ) { $width = (int) $image_tag->getAttribute( 'width' ); $height = (int) $image_tag->getAttribute( 'height' ); if ( 0 === $width && 0 === $height ) { - preg_match( '/-([0-9]+)x([0-9]+)\.(?:jpg|jpeg|png|gif|webp)$/i', $img_src, $matches ); + preg_match( '/-([0-9]{1,5})x([0-9]{1,5})\.(?:jpg|jpeg|png|gif|webp)$/i', $img_src, $matches ); if ( ! empty( $matches[1] ) ) { $width = (int) $matches[1]; } diff --git a/projects/plugins/jetpack/tests/php/media/test-class.jetpack-post-images.php b/projects/plugins/jetpack/tests/php/media/test-class.jetpack-post-images.php index 02c6fad97a3ba..01267265ffdf5 100644 --- a/projects/plugins/jetpack/tests/php/media/test-class.jetpack-post-images.php +++ b/projects/plugins/jetpack/tests/php/media/test-class.jetpack-post-images.php @@ -40,6 +40,35 @@ public function test_from_html_double_quotes() { $this->assertEquals( 200, $result[0]['src_height'] ); } + /** + * Test image size extract in src filename + * + * @covers Jetpack_PostImages::from_html + */ + public function test_from_html_size() { + $s = ""; + + $result = Jetpack_PostImages::from_html( $s ); + + $this->assertIsArray( $result ); + $this->assertNotEmpty( $result ); + $this->assertEquals( 2300, $result[0]['src_width'] ); + $this->assertEquals( 1300, $result[0]['src_height'] ); + } + + /** + * Test ignoring unrealistic image sizes from src filename + * + * @covers Jetpack_PostImages::from_html + */ + public function test_from_html_no_size() { + $s = ""; + + $result = Jetpack_PostImages::from_html( $s ); + + $this->assertEquals( array(), $result ); + } + /** * @author scotchfield * @covers Jetpack_PostImages::from_slideshow