Skip to content

Commit

Permalink
Jetpack Sync: Avoid detecting invalid image size (#35532)
Browse files Browse the repository at this point in the history
Ignore image size inferred from media URL if it's longer than 5 characters.
  • Loading branch information
trakos authored Feb 9, 2024
1 parent e0462a5 commit 1e225a3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Jetpack Sync: Avoid detecting invalid image size
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/class.jetpack-post-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<img src='img-2300x1300.jpg' />";

$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 = "<img src='img-851958915511220x220.jpg' />";

$result = Jetpack_PostImages::from_html( $s );

$this->assertEquals( array(), $result );
}

/**
* @author scotchfield
* @covers Jetpack_PostImages::from_slideshow
Expand Down

0 comments on commit 1e225a3

Please sign in to comment.