Finding absolute image src

I’m using DomDocument to pull HTML elements and attributes from a page and using it in an HTML e-mail. Running into a snag though. Some SRCs may be set as absolute, some may be relative. So I’m trying to find the exact image path (e.g. converting it to an absolute URL) in all cases.

This might be an image path

"../_files/images/NewsStories/imagehere.png"
Which is referenced from this URL

sitehere.com/testingfolder/websitesname/newsarticles/articlehere.php

So as you see it goes up a directory and then starts going to files/images/etc etc

or the full URL which is easy peasy. I had this below script which I thought worked, but apparently doesn’t. Can anyone tell me why? It’s coming up with this sort of URL

http://intranetdirectory.website.com/testingfolder/websitesname/../_files/images/news/imagehere.png

Basically, it has …/ when I don’t need it.

What’s wrong with this script?

$site = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$dir = dirname($site);

  //find first img, add it to $title element
  $leftImage=$cr->getElementsByTagName('img')->item(0)->getAttribute('src');
  $url_info = parse_url($leftImage);
  if (!isset($url_info['host']))
  {
    $path = $url_info['path'];
    if(substr($path,0,1) !== '/')
      $path = '/'.$path;
    $leftImage= $dir.$path;
  }

$leftImage should be the final result. That’s what’s getting put in my e-mails.

1 Like

I’d go for (if it’s not yet an URL)

$url = $dir . realpath(dirname($src));

What does this mean?

1 Like

if your src is already an absolute URL you obviously don’t have to do anything on it, right?

Correct, however I will need to detect it as a full URL. I do not have control over the URLs as absolute/relative.

If it is absolute, then I won’t need to do anything.

I’m really not that good at PHP so I’m unsure of what to do here.

1 Like

to check if it is already an absolute URL you can use filters:

$not_false_on_success = filter_var($src, FILTER_VALIDATE_URL);

parse_url() would also work.

1 Like

Does this look right?

  $leftImage=$cr->getElementsByTagName('img')->item(0)->getAttribute('src');
  $checkSRC = filter_var($leftImage, FILTER_VALIDATE_URL);

  if (!$checkSRC)
  {
    $leftImage= $dir . realpath(dirname($checkSRC));
  }

Alright it appears to work with absolutes but when I go relative it gives me incorrect URLs.

E.g. I have a on my public_html directory, test and test2.php. Test2 is my HTML file I’m pulling from

So on test2.php I have this image src: “/uploads/…/uploads/codefund-16.jpg”;

I did the weird src there just to test. It’s just printing this out as $leftImage using the above code.

http://www.codefundamentals.com/home1/codefund/public_html

Think I got it!

  $leftImage=$cr->getElementsByTagName('img')->item(0)->getAttribute('src');
  $checkSRC = filter_var($leftImage, FILTER_VALIDATE_URL);
  if(!$checkSRC)
  {
    $url_info=parse_url($leftImage);
    $path = $url_info['path'];
    if(substr($path,0,1) !== '/')
      $path = '/'.$path;
    $leftImage= $dir.$path;echo $leftImage;
  }

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.