| | 1501 | * Determines if the image meta data is for the image source file. |
| | 1502 | * |
| | 1503 | * The image meta data is retrieved by attachment post ID. In some cases the post IDs may change. |
| | 1504 | * For example when the website is exported and imported at another website. Then the |
| | 1505 | * attachment post IDs that are in post_content for the exported website may not match |
| | 1506 | * the same attachments at the new website. |
| | 1507 | * |
| | 1508 | * @since 5.5.0 |
| | 1509 | * |
| | 1510 | * @param string $image_location The full path or URI to the image file. |
| | 1511 | * @param array $image_meta The attachment meta data as returned by 'wp_get_attachment_metadata()'. |
| | 1512 | * @return bool Whether the image meta is for this image file. |
| | 1513 | */ |
| | 1514 | function wp_image_file_matches_image_meta( $image_location, $image_meta ) { |
| | 1515 | $match = false; |
| | 1516 | |
| | 1517 | // Ensure the $image_meta is valid. |
| | 1518 | if ( isset( $image_meta['file'] ) && strlen( $image_meta['file'] ) > 4 ) { |
| | 1519 | // Remove quiery args if image URI. |
| | 1520 | list( $image_location ) = explode( '?', $image_location ); |
| | 1521 | |
| | 1522 | // Check if the relative image path from the image meta is at the end of $image_location. |
| | 1523 | if ( strrpos( $image_location, $image_meta['file'] ) === strlen( $image_location ) - strlen( $image_meta['file'] ) ) { |
| | 1524 | $match = true; |
| | 1525 | } |
| | 1526 | |
| | 1527 | if ( ! empty( $image_meta['sizes'] ) ) { |
| | 1528 | // Retrieve the uploads sub-directory from the full size image. |
| | 1529 | $dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); |
| | 1530 | |
| | 1531 | if ( $dirname ) { |
| | 1532 | $dirname = trailingslashit( $dirname ); |
| | 1533 | } |
| | 1534 | |
| | 1535 | foreach ( $image_meta['sizes'] as $image_size_data ) { |
| | 1536 | $relative_path = $dirname . $image_size_data['file']; |
| | 1537 | |
| | 1538 | if ( strrpos( $image_location, $relative_path ) === strlen( $image_location ) - strlen( $relative_path ) ) { |
| | 1539 | $match = true; |
| | 1540 | break; |
| | 1541 | } |
| | 1542 | } |
| | 1543 | } |
| | 1544 | } |
| | 1545 | |
| | 1546 | /** |
| | 1547 | * Filter whether an image path or URI matches image meta. |
| | 1548 | * |
| | 1549 | * @since 5.5.0 |
| | 1550 | * |
| | 1551 | * @param bool $match Whether the image relative path from the image meta |
| | 1552 | * matches the end of the URI or path to the image file. |
| | 1553 | * @param string $image_location Full path or URI to the tested image file. |
| | 1554 | * @param array $image_meta The image meta data being tested. |
| | 1555 | */ |
| | 1556 | return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta ); |
| | 1557 | } |
| | 1558 | |
| | 1559 | /** |
| 1520 | | foreach ( $image_meta['sizes'] as $image_size_data ) { |
| 1521 | | if ( $image_filename === $image_size_data['file'] ) { |
| 1522 | | return array( |
| 1523 | | (int) $image_size_data['width'], |
| 1524 | | (int) $image_size_data['height'], |
| 1525 | | ); |
| | 1582 | if ( ! empty( $image_meta['sizes'] ) ) { |
| | 1583 | $src_filename = wp_basename( $image_src ); |
| | 1584 | |
| | 1585 | foreach ( $image_meta['sizes'] as $image_size_data ) { |
| | 1586 | if ( $src_filename === $image_size_data['file'] ) { |
| | 1587 | return array( |
| | 1588 | (int) $image_size_data['width'], |
| | 1589 | (int) $image_size_data['height'], |
| | 1590 | ); |
| | 1591 | } |