示例#1
0
 public function testRelativeUrl()
 {
     $base = 'http://shimizuyu.jp/index.html#facilities';
     $target = 'images/btn_s01_f2.jpg';
     $url = Url::parseRelative($base, $target);
     $this->assertEquals('http://shimizuyu.jp/images/btn_s01_f2.jpg', $url->url);
     $this->assertEquals('images/shimizuyu.jp/' . $target, Service::kindlePath($url));
     $target = './images/btn_s01_f2.jpg';
     $url = Url::parseRelative($base, $target);
     $this->assertEquals('http://shimizuyu.jp/images/btn_s01_f2.jpg', $url->url);
     $this->assertEquals('images/shimizuyu.jp/images/btn_s01_f2.jpg', Service::kindlePath($url));
 }
 public function exec()
 {
     $imgs = $this->domElement->getElementsByTagName('img');
     foreach ($imgs as $img) {
         $src = $img->getAttribute('src');
         $imgUrl = Url::parseRelative($this->url->url, $src);
         try {
             $imgFile = file_get_contents($imgUrl->url);
             $this->dirBuilder->putImage(Service::kindlePath($imgUrl), $imgFile);
         } catch (Exception $e) {
             // 無視
             d($e);
         }
     }
 }
 private function normalize()
 {
     // a hrefを絶対パスに変更
     $u = Url::parse($this->url);
     foreach ($this->contentNode->getElementsByTagName('a') as $element) {
         $href = $element->getAttribute('href');
         if (strpos($href, 'http') === 0 || strpos($href, '#') === 0) {
             continue;
         }
         if (strpos($href, '/') === 0) {
             $element->setAttribute('href', $u->scheme . $u->host . $href);
         } else {
             if (strpos($href, './') === 0) {
                 $element->setAttribute('href', $u->scheme . $u->host . $u->path . substr($href, 2));
             }
         }
     }
     // img srcをdomain/相対パスに変更 hoge.domain.com/s/r/c.png
     foreach ($this->contentNode->getElementsByTagName('img') as $element) {
         $src = $element->getAttribute('src');
         $srcUrl = Url::parseRelative($this->url, $src);
         $element->setAttribute('src', Service::kindlePath($srcUrl));
     }
 }