목차
           " >2. PHP图片采集程序--图片采集类               
php教程 PHP源码 PHP图片采集程序--图片采集类

PHP图片采集程序--图片采集类

May 25, 2016 pm 05:09 PM

1.         collection.class.php          

<?php
class Collection{
	protected $url;				//采集地址
	protected $prefix;			//重命名文件前缀
	protected $style;			//需要采集的图片格式,传入一个数组
	const prel = &#39;/(?:http?|https?):\/\/(?:[^\.\/\(\)\?]+)\.(?:[^\.\/]+)\.(?:com|cn|net|org)\/(?:[^\.:\"\&#39;\(\)\?]+)\.(jpg|png|gif)/i&#39;;			//采集规则
	//构造函数
	function __construct($url,$prefix,$style){
		switch($this->checkdata($url,$prefix,$style)){
			case 1:
				echo &#39;<script>alert("采集地址不能为空!")</script>&#39;;
				exit;
				break;
			case 2:
				echo &#39;<script>alert("需要采集的图片格式,应该为数组!")</script>&#39;;
				exit;
				break;
			case 3:
				echo &#39;<script>alert("需要采集的图片格式,不能为空!")</script>&#39;;
				exit;
				break;
			case 4:
				echo &#39;<script>alert("文件名不能含有. / |或用空格开头!")</script>&#39;;
				exit;
		}
		$this->url = $url;
		$this->prefix = $prefix;
		$this->style = $style;
	}
	//开始采集数据
	public function action(){
		$url = $this->checkurl();
		$imgurl = $this->collecturl($url);
		$this->savafile($imgurl);
	}
	//url处理
	protected function checkurl(){
		$munprel = &#39;/\([0-9]+,[0-9]+\)/i&#39;;
		$myurl;
		if(preg_match($munprel,$this->url,$arr)){
			$temp = substr($arr[0],1,strlen($arr[0])-2);
			$mymunber = explode(&#39;,&#39;,$temp);
			$temparr = explode($arr[0],$this->url);
			for($i=$mymunber[0];$i<=$mymunber[1];$i++){
				$myurl[] = $temparr[0].$i.$temparr[1];
			}
		}else{
			$myurl = $this->url;
		}
		return $myurl;
	}
	//文件保存
	protected function savafile($imgurl){
		if(!empty($imgurl)){
			foreach($imgurl[0] as $key=>$value){
				$filename = &#39;&#39;;
				if(in_array($imgurl[1][$key],$this->style)){
				    $size = @getimagesize($value);
					if($size === false){
						continue;
					}
					list($w,$h,$t,$a) = $size;
					if($w<200 || $h<200){
						continue;
					} 
					ob_start();
					readfile($value);
					$obj = ob_get_contents();
					ob_end_clean();
					$dir = &#39;F:/php/&#39;;
					if(!is_dir($dir)){
						mkdir($dir,0777);
					}
					if(!empty($this->prefix)){
						$filename = $dir.$this->prefix.date(&#39;Ymd&#39;).rand(10000,99999).&#39;.&#39;.$imgurl[1][$key];
					}else{
						$filename = $dir.date(&#39;Ymd&#39;).rand(10000,99999).&#39;.&#39;.$imgurl[1][$key];
					}
					$fo = @fopen($filename,&#39;wb&#39;);
					if($fo === false){
						echo &#39;<script>alert("创建文件失败,文件目录不可写!")</script>&#39;;
						exit;
					}
					$fw = fwrite($fo,$obj);
					echo &#39;<p style="width:350px;background:#ddd;">&#39;.$filename.&#39;采集成功</p>&#39;;
				}
			}
		}
	}
	
	//地址采集函数,包括图片后缀名
	protected function collecturl($url){
		set_time_limit(0);
		if(is_array($url)){
			$arr = array();
			$imgkey = array();
			foreach($url as $value){
				$code = file_get_contents($value);
				preg_match_all(self::prel,$code,$arrimg);
				$arr = array_merge($arr,$arrimg[0]);
				$imgkey = array_merge($imgkey,$arrimg[1]);
			}
			return array($arr,$imgkey);
		}else{
			$code = file_get_contents($url);
			preg_match_all(self::prel,$code,$arrimg);
			return $arrimg;
		}
	}
	//检验数据
	private function checkdata($url,$prefix,$style){
		if(empty($url)){
			return 1;
		}elseif(!is_array($style)){
			return 2;
		}elseif(count($style)==0){
			return 3;
		}elseif(stripos($prefix,&#39;.&#39;) !== false || stripos($prefix,&#39;/&#39;) !== false || stripos($prefix,&#39;|&#39;) !== false){
			return 4;
		}
	}
}
	
	
	
	
	
	
	
?>
로그인 후 복사


2. PHP图片采集程序--图片采集类

class Collection{
	protected $url;				//采集地址
	protected $prefix;			//重命名文件前缀
	protected $style;			//需要采集的图片格式,传入一个数组
	const prel = &#39;/(?:http?|https?):\/\/(?:[^\.\/\(\)\?]+)\.(?:[^\.\/]+)\.(?:com|cn|net|org)\/(?:[^\.:\"\&#39;\(\)\?]+)\.(jpg|png|gif)/i&#39;;			//采集规则
	//构造函数
	function __construct($url,$prefix,$style){
		switch($this->checkdata($url,$prefix,$style)){
			case 1:
				echo &#39;<script>alert("采集地址不能为空!")</script>&#39;;
				exit;
				break;
			case 2:
				echo &#39;<script>alert("需要采集的图片格式,应该为数组!")</script>&#39;;
				exit;
				break;
			case 3:
				echo &#39;<script>alert("需要采集的图片格式,不能为空!")</script>&#39;;
				exit;
				break;
			case 4:
				echo &#39;<script>alert("文件名不能含有. / |或用空格开头!")</script>&#39;;
				exit;
		}
		$this->url = $url;
		$this->prefix = $prefix;
		$this->style = $style;
	}
	//开始采集数据
	public function action(){
		$url = $this->checkurl();
		$imgurl = $this->collecturl($url);
		$this->savafile($imgurl);
	}
	//url处理
	protected function checkurl(){
		$munprel = &#39;/\([0-9]+,[0-9]+\)/i&#39;;
		$myurl;
		if(preg_match($munprel,$this->url,$arr)){
			$temp = substr($arr[0],1,strlen($arr[0])-2);
			$mymunber = explode(&#39;,&#39;,$temp);
			$temparr = explode($arr[0],$this->url);
			for($i=$mymunber[0];$i<=$mymunber[1];$i++){
				$myurl[] = $temparr[0].$i.$temparr[1];
			}
		}else{
			$myurl = $this->url;
		}
		return $myurl;
	}
	//文件保存
	protected function savafile($imgurl){
		if(!empty($imgurl)){
			foreach($imgurl[0] as $key=>$value){
				$filename = &#39;&#39;;
				if(in_array($imgurl[1][$key],$this->style)){
				    $size = @getimagesize($value);
					if($size === false){
						continue;
					}
					list($w,$h,$t,$a) = $size;
					if($w<200 || $h<200){
						continue;
					} 
					ob_start();
					readfile($value);
					$obj = ob_get_contents();
					ob_end_clean();
					$dir = &#39;F:/php/&#39;;
					if(!is_dir($dir)){
						mkdir($dir,0777);
					}
					if(!empty($this->prefix)){
						$filename = $dir.$this->prefix.date(&#39;Ymd&#39;).rand(10000,99999).&#39;.&#39;.$imgurl[1][$key];
					}else{
						$filename = $dir.date(&#39;Ymd&#39;).rand(10000,99999).&#39;.&#39;.$imgurl[1][$key];
					}
					$fo = @fopen($filename,&#39;wb&#39;);
					if($fo === false){
						echo &#39;<script>alert("创建文件失败,文件目录不可写!")</script>&#39;;
						exit;
					}
					$fw = fwrite($fo,$obj);
					echo &#39;<p style="width:350px;background:#ddd;">&#39;.$filename.&#39;采集成功</p>&#39;;
				}
			}
		}
	}
	
	//地址采集函数,包括图片后缀名
	protected function collecturl($url){
		set_time_limit(0);
		if(is_array($url)){
			$arr = array();
			$imgkey = array();
			foreach($url as $value){
				$code = file_get_contents($value);
				preg_match_all(self::prel,$code,$arrimg);
				$arr = array_merge($arr,$arrimg[0]);
				$imgkey = array_merge($imgkey,$arrimg[1]);
			}
			return array($arr,$imgkey);
		}else{
			$code = file_get_contents($url);
			preg_match_all(self::prel,$code,$arrimg);
			return $arrimg;
		}
	}
	//检验数据
	private function checkdata($url,$prefix,$style){
		if(empty($url)){
			return 1;
		}elseif(!is_array($style)){
			return 2;
		}elseif(count($style)==0){
			return 3;
		}elseif(stripos($prefix,&#39;.&#39;) !== false || stripos($prefix,&#39;/&#39;) !== false || stripos($prefix,&#39;|&#39;) !== false){
			return 4;
		}
	}
}
로그인 후 복사

                   

                   

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)