2011-12-20 70 views
0

我正在使用bookmarklet,并且使用HTML DOM解析器(正如前面提出的SO答案)获取任何外部页面的所有照片。我正确地提取照片并在我的书签弹出窗口中显示。但是我遇到了照片相对路径的问题。获取外部网页图像的绝对路径

例如外部页面上的图片来源说http://www.example.com/dir/index.php

  1. 图片来源1:IMG源= '主机名/照片/ photo.jpg' - 获取照片,因为它是绝对

  2. 照片来源2:img source ='/ photos/photo.jpg' - 没有得到,因为它不是绝对的。

我通过当前网址工作我的意思是使用dirname或pathinfo获取当前网址的目录。但导致主机/目录/(主机作为父目录)和主机/目录/ index.php(主机/目录作为父目录是正确的)之间的问题

请帮助我怎样才能得到这些相对照片?

+0

那'link'呢?我的意思是'/photo/xdfa.jpg'只会从域名地址考虑。你也可以尝试'./path/pics.jpg';它已经为我工作 – Kris 2011-12-20 08:53:47

+0

那么问题是什么?如何检测字符串是以'http://'还是'/'开头? – Gordon 2011-12-20 08:56:36

回答

5

FIXED(增加了查询字符串只图像路径的支持)

function make_absolute_path ($baseUrl, $relativePath) { 

    // Parse URLs, return FALSE on failure 
    if ((!$baseParts = parse_url($baseUrl)) || (!$pathParts = parse_url($relativePath))) { 
     return FALSE; 
    } 

    // Work-around for pre- 5.4.7 bug in parse_url() for relative protocols 
    if (empty($baseParts['host']) && !empty($baseParts['path']) && substr($baseParts['path'], 0, 2) === '//') { 
     $parts = explode('/', ltrim($baseParts['path'], '/')); 
     $baseParts['host'] = array_shift($parts); 
     $baseParts['path'] = '/'.implode('/', $parts); 
    } 
    if (empty($pathParts['host']) && !empty($pathParts['path']) && substr($pathParts['path'], 0, 2) === '//') { 
     $parts = explode('/', ltrim($pathParts['path'], '/')); 
     $pathParts['host'] = array_shift($parts); 
     $pathParts['path'] = '/'.implode('/', $parts); 
    } 

    // Relative path has a host component, just return it 
    if (!empty($pathParts['host'])) { 
     return $relativePath; 
    } 

    // Normalise base URL (fill in missing info) 
    // If base URL doesn't have a host component return error 
    if (empty($baseParts['host'])) { 
     return FALSE; 
    } 
    if (empty($baseParts['path'])) { 
     $baseParts['path'] = '/'; 
    } 
    if (empty($baseParts['scheme'])) { 
     $baseParts['scheme'] = 'http'; 
    } 

    // Start constructing return value 
    $result = $baseParts['scheme'].'://'; 

    // Add username/password if any 
    if (!empty($baseParts['user'])) { 
     $result .= $baseParts['user']; 
     if (!empty($baseParts['pass'])) { 
      $result .= ":{$baseParts['pass']}"; 
     } 
     $result .= '@'; 
    } 

    // Add host/port 
    $result .= !empty($baseParts['port']) ? "{$baseParts['host']}:{$baseParts['port']}" : $baseParts['host']; 

    // Inspect relative path path 
    if ($relativePath[0] === '/') { 

     // Leading/means from root 
     $result .= $relativePath; 

    } else if ($relativePath[0] === '?') { 

     // Leading ? means query the existing URL 
     $result .= $baseParts['path'].$relativePath; 

    } else { 

     // Get the current working directory 
     $resultPath = rtrim(substr($baseParts['path'], -1) === '/' ? trim($baseParts['path']) : str_replace('\\', '/', dirname(trim($baseParts['path']))), '/'); 

     // Split the image path into components and loop them 
     foreach (explode('/', $relativePath) as $pathComponent) { 
      switch ($pathComponent) { 
       case '': case '.': 
        // a single dot means "this directory" and can be skipped 
        // an empty space is a mistake on somebodies part, and can also be skipped 
        break; 
       case '..': 
        // a double dot means "up a directory" 
        $resultPath = rtrim(str_replace('\\', '/', dirname($resultPath)), '/'); 
        break; 
       default: 
        // anything else can be added to the path 
        $resultPath .= "/$pathComponent"; 
        break; 
      } 
     } 

     // Add path to result 
     $result .= $resultPath; 

    } 

    return $result; 

} 

测试:

echo make_absolute_path('http://www.example.com/dir/index.php','/photos/photo.jpg')."\n"; 
// Outputs: http://www.example.com/photos/photo.jpg 
echo make_absolute_path('http://www.example.com/dir/index.php','photos/photo.jpg')."\n"; 
// Outputs: http://www.example.com/dir/photos/photo.jpg 
echo make_absolute_path('http://www.example.com/dir/index.php','./photos/photo.jpg')."\n"; 
// Outputs: http://www.example.com/dir/photos/photo.jpg 
echo make_absolute_path('http://www.example.com/dir/index.php','../photos/photo.jpg')."\n"; 
// Outputs: http://www.example.com/photos/photo.jpg 
echo make_absolute_path('http://www.example.com/dir/index.php','http://www.yyy.com/photos/photo.jpg')."\n"; 
// Outputs: http://www.yyy.com/photos/photo.jpg 
echo make_absolute_path('http://www.example.com/dir/index.php','?query=something')."\n"; 
// Outputs: http://www.example.com/dir/index.php?query=something 

我认为应该处理刚才的一切你可能正确遭遇,和应该大致等同于浏览器所使用的逻辑。还应该纠正你在Windows上使用dirname()时可能会出现的杂散斜线。

第一个参数是在哪里找到的<img>(或<a>或其他)的页面,第二个参数的网址是src/href等属性的内容。

如果有人发现一些不起作用的东西(因为我知道你们都会试图破坏它:-D),让我知道,我会尝试修复它。

+1

简单的词:AWESOMEE​​EEEE – Rohit 2011-12-20 09:38:55

+0

@Rohit我刚刚添加了几个小修正:-) – DaveRandom 2011-12-20 09:49:46

0

'/'应该是基本路径。检查你的dom解析器返回的第一个字符,如果它是'/',那么只需在域名前加前缀。

+0

好的,谢谢..请给我介绍一下这个案例,当主网站是像...... www.yahoo.com/news/....这样的子目录时,它会将www.yahoo.com作为域名因此图像路径检测将失败。 – Rohit 2011-12-20 09:09:46

+0

通常,您应该始终使用完整的基本路径+图像路径(如在您提供的#1示例中)。只有在img src以'/'开头的情况下,您是否应该在第一个正斜杠之后使用完整路径减号。所以www.yahoo.com/finance/AAPL => www.yahoo.com然后添加img src:'/photos/photo.jpg'。你的DOM解析器用什么语言编写的? – 2011-12-20 09:15:52