采集css文件里面的图片采集html里面的图片
<?php
/** http://localhost/test/my/xiuwo/css2img/
* 通过CSS采集图片. 注意:采集下来的图片 保存在和当前目录相同。
$image_path = "//images/a/c.png";
$image_path = str_replace("//","", $image_path);
$image_path = substr($image_path, 0);
$pathinfo = pathinfo($image_path);
$image_dir = $pathinfo ? $pathinfo['dirname'] : '';
echo $image_dir;
//mkdir('1/2/3', 0777,true);
//mkdir($image_dir, 0777,true);
//exit;
exit;
*/
$spider = new spider();
//$spider->time_limiter = 1115500;
set_time_limit(0);
$spider->getAll_one();
class spider{
public $url="http://www.a.com/templets/default/style.css";//css地址或者index.html地址
public $url_img = "http://www.a.com/templets/default/";//源图片网址
//匹配html里面的 img
/*public $reg='/<img.*?src="(.*?)".*?\/?>/i'; //匹配html里面的 img */
/* public $reg='/url\((.*)\)/i'; //匹配html里面的 img */
//匹配css里面的background-image
public $reg="|url\((.*)\)|U"; //匹配css里面的background-image
public $test = 0;//1测试并打印输出 0直接采集
function __construct(){
}
function getAll_one()
{
/**
$value='http://a.a.cn/img/background/line_nav.jpg';
$ary = explode('/', $value);
$num = count($ary);
echo($ary[$num-1]);
*/
$url = $this->url;
$reg = $this->reg;
$data = @file_get_contents($url);
preg_match_all($reg, $data, $out, PREG_PATTERN_ORDER);
$out_1 = $out[1];
foreach($out_1 as $k=>$img_url)
{
$img_url = str_replace("../", "", $img_url);
$img_url = str_replace("'", "", $img_url);
$img_url = str_replace('"', "", $img_url);
$out_1[$k] = $img_url;
}
print_r($out_1);
if($this->test) exit;
$this->download_mp3_five($out_1);
}
function download_mp3_five($data){
//读取 内容,并写入本地。。。
foreach($data as $value)
{
//echo $value.'<br>';
//创建目录
$image_path = $value;
$image_path = str_replace("//","", $image_path);
if(substr($image_path, 0, 1) == '/')
{
$image_path = substr($image_path, 1);
}
$image_path = str_replace(":", "", $image_path);
$pathinfo = pathinfo($image_path);
$image_dir = $pathinfo ? $pathinfo['dirname'] : '';
//echo $image_path.'--';
//echo $image_dir.'<br><br>';exit;
if(!is_dir($image_dir))
{
@mkdir($image_dir, 0777,true);
}
//创建目录 END
//exit;
$value = $this->url_img.$value;
//echo $value;
//echo "\r\n\r\n<br>\r\n";
$file = '';
$file = @file_get_contents($value);
//var_dump($file);exit;
$fp = @fopen($image_path, 'w');
@fwrite($fp, $file);
@fclose($fp);
}
}
function _get_file_name($value)
{
$ary = explode('/', $value);
$num = count($ary);
return $ary[$num-1];
//$reg = "|\/(.*)|U";
//preg_match_all($reg, $value, $out, PREG_PATTERN_ORDER);
//return $out[0];
}
}