手机版

php中使用权志龙库实现下载网页中所有图片

时间:2021-11-06 来源:互联网 编辑:宝哥软件园 浏览:

在前期的服务器端编程语言(专业超文本预处理器的缩写)教程就讲了php gd库可以实现远程图片的下载,但是那只是下载了一张图片,原理是一样的,要想下载一个网页的所有图片只要使用正则表达式进行判断,找出所有的图片全球资源定位器(统一资源定位符)就可以进行循环下载了,我特地参照网络资源编写了权志龙库图片下载类!服务器端编程语言(专业超文本预处理器的缩写)代码如下:

?phpheader(' Content-type : text/html;charset=utf-8 ');if(!空($ _ POST[' submit ']){ $ URL=$ _ POST[' URL '];//为了获取相对路径的图片所做的操作$ URL _ fields=parse _ URL($ URL);$ main _ URL=$ URL _ fields[' host '];$base_url=substr($url,0,strrpos($url,'/')1);//获取网页内容//设置代理服务器$ opts=array(' http '=array(' request _ full uri '=true));$ context=stream _ context _ create($ opts);$ content=file _ get _ contents($ URL,false,$ context);//匹配img标签,将所有匹配字符串保存到数组$matches $reg='/img .*?src=\ '(.*?)\'.*?/I’;preg_match_all($reg,$content,$ matches);$ count=count($ matches[0]);对于($ I=0;一美元计数;$i ){ /*将所有图片的全球资源定位器(统一资源定位符)转换为小写* $ matches[1][$ I]=strtolow($ matches[1][$ I]);*///如果图片为相对路径就转化为全路径if(!strpos('a ' .$匹配[1][$i],' http '){//因为'/'是第0个位置if (strpos('a ').$matches[1][$i],'/'){ $ matches[1][$ I]=' http://' .$main_url .$匹配[1][$ I];} else { $ matches[1][$ I]=$ base _ URL .$匹配[1][$ I];} } } //过滤重复的图片$ img _ arr=array _ unique($ matches[1]);//实例化图片下载类$ GetImg=新的向下图像();$ URL _ count=count($ img _ arr);对于($ I=0;$ I $ URL _ count $ I){ $ GetImg-source=$ img _ arr[$ I];$getImg-save_address=' ./pic/';$ file=$ GetImg-download();}回声下载完成!哈哈,简单吧!}类DownImage { public $ source//远程图片统一资源定位器公共$ save _ address//保存本地地址public $ set _ extension//设置图片扩展名公共$质量;//图片的质量(0~100,100最佳,默认75左右) //下载方法(选用权志龙库图片下载)公共函数下载(){ //获取远程图片信息$ info=@ getimagesize($ this-source);//获取图片扩展名$ mime=$ info[' mime '];$ type=substr(strchr($ mime,'/'),1);//不同的图片类型选择不同的图片生成和保存函数switch($ type){ case ' JPEG ' : $ img _ create _ func=' imagecreatefrom JPEG ';$ img _ save _ func=' image JPEG $ new _ img _ ext=' jpg $ image _ quality=isset($ this-quality)?$这个质量: 100;打破;大小写" png " : $ img _ create _ func=" imagecreatefrompng ";$ img _ save _ func=' image png $ new _ img _ ext=' png打破;案例' BMP ' : $ img _ create _ func=' imagecreatefrom BMP ';$ img _ save _ func=' image BMP $ new _ img _ ext=' BMP打破;案例' gif ' : $ img _ create _ func=' imagecreatefrom gif ';$ img _ save _ func=' imagegif $ new _ img _ ext=' gif打破;案件编号。WAP。wbmp ' : $ img _ create _ func=' imagecreatefrowmp ';$ img _ save _ func=' imagewbmp $ new _ img _ ext=' BMP打破;案例" xbm " : $ img _ create _ func=" imagecreatefromxbm ";$ img _ save _ func=' imagexbm $ new _ img _ ext=' xbm打破;默认值: $ img _ create _ func=' imagecreatefrom JPEG ';$ img _ save _ func=' image JPEG $ new _ img _ ext=' jpg }//根据是否设置扩展名来合成本地文件名if(isset($ this-set _ extension)){ $ ext=strchr($ this-source ' ');$ strlen=strlen($ ext);$ new name=basename(substr($ this-source,0,-$strlen)).$ new _ img _ ext } else { $ new name=base name($ this-source);} //生成本地文件路径$ save _ address=$ this-save _ address .$ new name $ img=@ $ img _ create _ func($ this-source);if(isset($ image _ quality)){ $ save _ img=@ $ img _ save _ func($ img,$save_address,$ image _ quality);} else { $ save _ img=@ $ img _ save _ func($ img,$ save _ address);}返回$ save _ img}}?表单方法='POST '操作=' '远程全球资源定位器(统一资源定位符)地址:输入类型=' text ' name=' URL ' size=30/输入类型='submit '名称='submit '值='下载该页面所有图片//表单运行结果如图:

版权声明:php中使用权志龙库实现下载网页中所有图片是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。