views:

254

answers:

3

Hi,

I'm having a problem fetching the absolute URL of a remote image. What I'm doing right now is this:

foreach($html->find('img') as $e){
$path = parse_url($e->src, PHP_URL_PATH);
$absolute = realpath($path);
    if($e->src==$path){
    echo '<img class="pic" src=' . $absolute . '/><br/>';
    }

}

This code has been written using the simple html dom project. The ideea is that I can't display images with a relative url in the original coding because they are not located on my server.

So does anyone know how to resolve the absolute path of a remote image?

+1  A: 

You'll need to grab the base domain, along with the relative url root.

So, if the image was found on the page, yoursite.com/slideshow/4/view, but was relatively pointing to ./../../images/435.jpg, you would just combine the paths.

Otherwise, if the image pointed to the root of the domains (you can use regex to find if the first character is a /), then you can get use just yoursite.com and add that path to the string.

Look at http://php.net/manual/en/function.parse-url.php to parse the url into the site host, the relative page to that host.

CodeJoust
A: 

If you know the address of where you are filtering these images, why not just add that to what the src property returns?

AntonioCS
A: 

If you are doing this a lot (like an entire site) you might look at the power that behind wget.

One of the options is to rebuild urls, IIRC.

-k --convert-links

Might be worth your time taking a look at wget.

Cups