Get attachment URL from attachment Link in WordPress

There’s a couple functions to get different attachment parameters based on one another, but a cursory look doesn’t indicate there’s one to get one based on the Attachment Page URL.

You could try and get the Post ID based on the Name (or Title) using the $wpdb class, and then drop that ID into the wp_get_attachment_image_src() function. Something like this would get you started:

function get_attachment_id_by_url( $url ){
    global $wpdb;

    $url   = trim( $url, "https://wordpress.stackexchange.com/" ); // Trim whitespace and trailing slash
    $parts = explode( "https://wordpress.stackexchange.com/", $url ); 
    $name  = end( $parts ); // We only want the last part

    if( ! $result = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name ) ) ) // potentially replace post_name with post_title
        return false;

    return $result->ID;
}

This would let you do something like this:

// Attachment URL we want to utilize
$image_url="https://example.com/attachment/name_of_photo/";

// Get the ID using our function above
$image_id  = get_attachment_id_by_url( $image_url );

// Get the SRC parameters from that ID (use whatever `$size` args you want)
$image_src = wp_get_attachment_image_src( $image_url, 'full' );

/**
 * var_dump($image_src) Output:
 * 
 * array(4) {
 *    [0]=> string(48) "https://example.com/attachment/name_of_photo.png"
 *    [1]=> int(640)
 *    [2]=> int(480)
 *    [3]=> bool(true)
 * }
 */