diff --git src/wp-includes/media.php src/wp-includes/media.php
index 9f35f79..8d23f93 100644
--- src/wp-includes/media.php
+++ src/wp-includes/media.php
@@ -1035,6 +1035,8 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac
 		$image_baseurl = set_url_scheme( $image_baseurl, 'https' );
 	}
 
+	$image_basepath = parse_url( $image_baseurl, PHP_URL_PATH );
+
 	/*
 	 * Images that have been edited in WordPress after being uploaded will
 	 * contain a unique hash. Look for that hash and use it later to filter
@@ -1075,7 +1077,7 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac
 		}
 
 		// If the file name is part of the `src`, we've confirmed a match.
-		if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
+		if ( ! $src_matched && false !== strpos( $image_src, $image_basepath . $image['file'] ) ) {
 			$src_matched = $is_src = true;
 		}
 
