How to get list of paths (not urls) for an image

I can get a list of urls for an image like this:

$sizes = array_merge(array('full'),get_intermediate_image_sizes());
foreach ($sizes as $imageSize) {
    $image_attributes = wp_get_attachment_image_src( $post->ID , $imageSize );
    echo   $imageSize . ':<br>' .$image_attributes[0].'<br>';
}

which gives me

full:
http://domain.com/wp-content/_uploads/2014/01/my_image.png
thumbnail:
http://domain.com/wp-content/_uploads/2014/01/my_image-150x150.png
medium:
http://domain.com/wp-content/_uploads/2014/01/my_image-300x116.png 

What I want is

full:
/path/to/public_html/wp-content/_uploads/2014/01/my_image.png 
thumbnail:
/path/to/public_html/wp-content/_uploads/2014/01/my_image-150x150.png 
medium:
/path/to/public_html/wp-content/_uploads/2014/01/my_image-300x116.png 

1 Answer
1

This should achieve what you want, however note that there will be backslashes AND forward slashes in the resulting path name. If this causes a problem on your web server you may need to do a further str_replace to forward slashes with backslashes

See comments in code for more info

$sizes = array_merge(array('full'),get_intermediate_image_sizes());
$uploads = wp_upload_dir();
foreach ($sizes as $imageSize) {
    // Get the image object
    $image_object = wp_get_attachment_image_src($post->ID,$imageSize );
    // Isolate the url
    $image_url = $image_object[0];
    // Using the wp_upload_dir replace the baseurl with the basedir
    $image_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $image_url );
    // echo it out
    echo   $imageSize . ':<br>' .$image_path.'<br>';
}

Leave a Comment