根据css文件采集图片

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5 <title>根据css文件采集图片</title>
  6 </head>
  7 <body>
  8 <?php
  9 class get_pic_from_css {
 10 
 11   var $path    = '.';
 12   var $match    = 'background:url\((.*?)\)';
 13   var $url    = 'http://xxxx.com/web/Tpl/Home/Public';
 14   var $flag    = '..';
 15 //   var $absolute_address;    // 绝对地址
 16 //   var $relative_address;    // 相对地址
 17 
 18 //   function __construct(){
 19 //    
 20 //   }
 21 
 22   function work(){
 23     $array = $this->relative_to_absolute();
 24     echo "数组长度".count($array)."<hr>";
 25     foreach($array as $row){
 26       $local = $this->my_substr($row, '/images/', '.');
 27       $ext = substr($row,-4);
 28       $this->down_pic($row, $local.$ext);
 29     }
 30   }
 31 
 32   // 下载图片
 33   function down_pic($url_file, $local_file){
 34     if(file_exists($local_file)){
 35       echo $local_file."存在<br/>";
 36     }else{
 37       $contents = file_get_contents($url_file);
 38       $fp = fopen($local_file,"w+");
 39       fwrite($fp,$contents);
 40       fclose($fp);
 41       echo $url_file.'------------------ok<br/>';
 42     }
 43 
 44     print str_repeat(" ", 4096);
 45     ob_flush();
 46     flush();
 47   }
 48   // 截取字符串
 49   function my_substr($str,$start,$end){
 50     $arr = explode($start,$str);
 51     $arr = explode($end,$arr[1]);
 52     return $arr[0];
 53   }
 54 
 55   // 相对路径变成绝对路径
 56   function relative_to_absolute(){
 57     $array = $this->get_pic_to_array();
 58     $chage = array($this->flag => $this->url);
 59     foreach($array as $row){
 60       $array_new[] = strtr($row,$chage);
 61     }
 62     return $array_new;
 63   }
 64 
 65   // 所有匹配的结果保存到一个数组
 66   function get_pic_to_array(){
 67     $array = array();
 68     $csses = $this->get_css_files($this->path);
 69     foreach($csses as $css){
 70       $matches = $this->get_matches_from_css($css);
 71       $array = array_merge($array, $matches); 
 72     }
 73     // 去掉数组中重复的项目
 74     $array = array_unique($array);
 75     return $array;
 76   }
 77 
 78   // 浏览本文件夹所以css文件保存到数组
 79   function get_css_files($path){
 80     $handle=opendir($path);
 81     while ($file = readdir($handle)) {
 82       if(substr(strrchr($file, "."), 1) == "css"){
 83         $csses[] = $file;
 84       }
 85     }
 86     closedir($handle);
 87     return $csses;
 88   }
 89 
 90   // 浏览单独css文件匹配的背景保存到数组
 91   function get_matches_from_css($file){
 92     $html = file_get_contents($file);
 93     preg_match_all ('/'.$this->match.'/', $html, $matches);
 94     // $matches[0]为匹配到的全部 $matches[1]为匹配到的子项
 95     return $matches[1];
 96   }
 97 
 98 }
 99 
100 set_time_limit(0);
101 $go = new get_pic_from_css();
102 $go->work();
103 ?>
104 </body>
105 </html>

 

posted @ 2013-09-03 16:06  竹三戒  阅读(235)  评论(0编辑  收藏  举报