Home > php教程 > php手册 > body text

css文件背景图片下载器php版

WBOY
Release: 2016-05-26 08:21:06
Original
1046 people have browsed it

下载css文件里面背景是我们这些盗版份子长期搞的事情,下载个css图片下载器各种广告弹窗,扛不住,呵呵,写了个水货的php版本的,可以凑合着用下.

把文件放到php程序目录 dos下面 php.exe cssImages.php 0 http://www.phprm.com/wp-content/themes/itxti/style.css images

先在php程序目录建个images文件夹,呵呵,贴代码:

<?php   
	/**  
	*@link  http://blog.csdn.net/jsbba  
	*@阿唐 2013-4-6 22:19   
	*@呵呵  
	*/   
	set_time_limit(0);   
	error_reporting(E_ERROR);   
	if($argc<4){   
	print_r(&#39;   
	 +-------------------------------------------------+   
	Usage: php &#39;.$argv[0].&#39; css路径类型(0为远程、1为本地) css文件路径 图片保存目录   
	Example:    
	php.exe &#39;.$argv[0].&#39; 0 http://www.xxx.com/index.css images   
	 +-------------------------------------------------+   
	&#39;);   
	exit();   
	}   
	//远程css   
	if($argv[1]==0){   
	    http://pic4.phprm.com/2014/10/22/$host.jpg = getParse($argv[2],&#39;host&#39;);   
	    $savePath = getSavePath($argv[3]);   
	    $images = getCssImagesArray($argv[2]);   
	    //print_r($images);   
	    $imagesurls = getImagesLinks($images,$argv[2]);   
	    imagesDowner($imagesurls);   
	}   
	//本地css开始   
	if($argv[1]==1){   
	    //懒得写了,呵呵,这个没多大意义   
	}   
	/*  
	 * css图片分析函数  
	 * $csspath  css文件路径  
	 */   
	function getCssImagesArray($csspath){   
	    $cssFile = file_get_contents($csspath);   
	    $images = array();   
	    preg_match_all("|url((.+))|i",$cssFile,$images);   
	    $images = $images[1];   
	    return $images;   
	}   
	/*  
	 * css文件相对目录处理函数  
	 * $path 路径  
	 */   
	function getNocssPath($path){   
	    global $host;   
	    $tempLinkmages=&#39;&#39;;   
	    //相当路径获取   
	    $tempPath = explode(&#39;/&#39;,$path);   
	    for($i=1;$i<(count($tempPath)-2);$i++){   
	        $tempLinkmages .= $tempPath[$i].&#39;/&#39;;   
	    }   
	    $xdImage = $host.$tempLinkmages;   
	    return $xdImage;   
	    //相当路径获取   
	}   
	   
	/*  
	 * 图片连接获取函数  
	 * $images array 所有需要获取的images数组  
	 * cssLink css文件链接  
	 */   
	function getImagesLinks($imagesArray,$cssLink){   
	global $host;   
	$urlImages = array();   
	    foreach($imagesArray as $key=>$value){   
	        if(pathCheck($value)){   
	            if((!in_array(($host.$value),$urlImages))){   
	                $urlImages[$key] = $host.$value;   
	            }   
	        }else{   
	            if((!in_array((getNocssPath(getParse($cssLink,&#39;path&#39;)).$value),$urlImages))){   
	                $urlImages[$key] = getNocssPath(getParse($cssLink,&#39;path&#39;)).$value;   
	            }   
	        }   
	    }   
	    return $urlImages;   
	}   
	   
	/*  
	 * 图片获取  
	 * $urlImages需要下载的图片数组  
	 */   
	function imagesDowner($urlImages){   
	//print_r($urlImages);   
	    foreach($urlImages as $key=>$value){   
	        $urlImagesOk[$key] = str_replace(&#39;//&#39;,&#39;/&#39;,$value);   
	        $urlImagesOk[$key] = str_replace(&#39;"&#39;,&#39;&#39;,$urlImagesOk[$key]);   
	        $urlImagesOk[$key] = str_replace("&#39;",&#39;&#39;,$urlImagesOk[$key]);   
	        $urlImagesOk[$key] = &#39;http://&#39;.$urlImagesOk[$key];   
	        if(grabImage($urlImagesOk[$key],basename($urlImagesOk[$key]))){   
	            print_r(   
	basename($urlImagesOk[$key]).&#39; 文件下载成功   
	&#39;);   
	        }else{   
	            print_r(   
	basename($urlImagesOk[$key]).&#39;  下载失败   
	&#39;);   
	        }   
	    }   
	    //print_r($urlImagesOk);   
	}   
	/*  
	 * 相对路径绝对路径判定函数  
	 * $imageUrl 图片链接数组  
	 * true 为绝对路径  
	 * false 为相当路径  
	 */   
	function pathCheck($imageUrl){   
	    if(preg_match(&#39;|^(/)|&#39;,$imageUrl)){   
	        return true;   
	    }else{   
	        return false;   
	    }   
	}   
	   
	/*  
	 * 图片下载函数  
	 * $url 图片链接  
	 * $filename图片名字  
	 */   
	function grabImage($url, $filename){   
	    global $savePath;    
	    if($url == &#39;&#39;) {   
	        return false; //如果 $url 为空则返回 false;   
	           
	    }   
	    $ext_name = strrchr($url, &#39;.&#39;); //获取图片的扩展名   
	    if($ext_name != &#39;.gif&#39; && $ext_name != &#39;.jpg&#39; && $ext_name != &#39;.bmp&#39; && $ext_name != &#39;.png&#39;) {   
	        return false; //格式不在允许的范围   
	           
	    }   
	    if($filename == &#39;&#39;) {   
	        return false; //名称无效   
	           
	    }   
	    //开始捕获   
	    ob_start();   
	    if(readfile($url)){   
	        $img_data = ob_get_contents();   
	        ob_end_clean();   
	        $size = strlen($img_data);   
	    }else{   
	    ob_end_clean();   
	    return false;   
	    }   
	    if(($local_file = fopen($savePath.$filename , &#39;a&#39;))&&(fwrite($local_file, $img_data)))   
	    {   
	        fclose($local_file);   
	        return true;   
	    }   
	}   
	/*  
	 * 保存目录  
	 */   
	function getSavePath($savepath){   
	    $savePath = $savepath;   
	    $savePath = dirname(__FILE__).$savePath;   
	    return $savePath;   
	      //phprm.com 
	}   
	/*  
	 * 解析url  
	 */   
	function getParse($host,$type){   
	    $baseurl = parse_url($host);   
	    return $baseurl[$type].&#39;/&#39;;   
	   
	//echo $baseurl;   
	}   
	
Copy after login

永久地址:

转载随意~请带上教程地址吧^^

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!