gd库图片下载类实现下载网页所有图片的php代码


Posted in PHP onAugust 20, 2012

php代码如下:

<?php 
header("Content-type:text/html ; charset=utf-8"); 
if (!empty($_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_fulluri'=>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]); 
for ($i=0; $i<$count; $i++){ 
/*将所有图片的url转换为小写 
*$matches[1][$i] = strtolower($matches[1][$i]); 
*/ 
//如果图片为相对路径就转化为全路径 
if (!strpos('a'.$matches[1][$i], 'http')){ 
//因为'/'是第0个位置 
if (strpos('a'.$matches[1][$i], '/')){ 
$matches[1][$i] = 'http://'.$main_url.$matches[1][$i]; 
}else{ 
$matches[1][$i] = $base_url.$matches[1][$i]; 
} 
} 
} 
//过滤重复的图片 
$img_arr = array_unique($matches[1]); 
//实例化图片下载类 
$getImg = new DownImage(); 
$url_count = count($img_arr); 
for ($i=0; $i<$url_count; $i++){ 
$getImg->source = $img_arr[$i]; 
$getImg->save_address = './pic/'; 
$file = $getImg->download(); 
} 
echo "下载完成!哈哈,简单吧!"; 
} 
class DownImage{ 
public $source;//远程图片URL 
public $save_address;//保存本地地址 
public $set_extension; //设置图片扩展名 
public $quality; //图片的质量(0~100,100最佳,默认75左右) 
//下载方法(选用GD库图片下载) 
public function download(){ 
//获取远程图片信息 
$info = @getimagesize($this->source); 
//获取图片扩展名 
$mime = $info['mime']; 
$type = substr(strrchr($mime, '/'), 1); 
//不同的图片类型选择不同的图片生成和保存函数 
switch($type){ 
case 'jpeg': 
$img_create_func = 'imagecreatefromjpeg'; 
$img_save_func = 'imagejpeg'; 
$new_img_ext = 'jpg'; 
$image_quality = isset($this->quality) ? $this->quality : 100; 
break; 
case 'png': 
$img_create_func = 'imagecreatefrompng'; 
$img_save_func = 'imagepng'; 
$new_img_ext = 'png'; 
break; 
case 'bmp': 
$img_create_func = 'imagecreatefrombmp'; 
$img_save_func = 'imagebmp'; 
$new_img_ext = 'bmp'; 
break; 
case 'gif': 
$img_create_func = 'imagecreatefromgif'; 
$img_save_func = 'imagegif'; 
$new_img_ext = 'gif'; 
break; 
case 'vnd.wap.wbmp': 
$img_create_func = 'imagecreatefromwbmp'; 
$img_save_func = 'imagewbmp'; 
$new_img_ext = 'bmp'; 
break; 
case 'xbm': 
$img_create_func = 'imagecreatefromxbm'; 
$img_save_func = 'imagexbm'; 
$new_img_ext = 'xbm'; 
break; 
default: 
$img_create_func = 'imagecreatefromjpeg'; 
$img_save_func = 'imagejpeg'; 
$new_img_ext = 'jpg'; 
} 
//根据是否设置扩展名来合成本地文件名 
if (isset($this->set_extension)){ 
$ext = strrchr($this->source,"."); 
$strlen = strlen($ext); 
$newname = basename(substr($this->source,0,-$strlen)).'.'.$new_img_ext; 
}else{ 
$newname = basename($this->source); 
} //生成本地文件路径 
$save_address = $this->save_address.$newname; 
$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); 
} 
return $save_img; 
} 
} 
?> 
<form method="POST" action=""> 
远程url地址:<input type="text" name="url" size=30 /> 
<input type="submit" name="submit" value="下载该页面所有图片" /> 
</form>

运行结果如图:

gd库图片下载类实现下载网页所有图片的php代码下载的图片本例中保存在当前目录的pic文件夹下!

PHP 相关文章推荐
PHP的开合式多级菜单程序
Oct 09 PHP
用Php实现链结人气统计
Oct 09 PHP
关于文本留言本的分页代码
Oct 09 PHP
PHP 数组遍历方法大全(foreach,list,each)
Jun 30 PHP
过滤掉PHP数组中的重复值的实现代码
Jul 17 PHP
PHP采用自定义函数实现遍历目录下所有文件的方法
Aug 19 PHP
PHP中提问频率最高的11个面试题和答案
Sep 02 PHP
PHP正则替换函数preg_replace和preg_replace_callback使用总结
Sep 22 PHP
支持中文的PHP按字符串长度分割成数组代码
May 17 PHP
PHP中的命名空间详细介绍
Jul 02 PHP
PHP数组的定义、初始化和数组元素的显示实现代码
Nov 05 PHP
laravel框架学习笔记之组件化开发实现方法
Feb 01 PHP
自己在做项目过程中学到的PHP知识收集
Aug 20 #PHP
用PHP+MySQL搭建聊天室功能实例代码
Aug 20 #PHP
PHP系列学习之日期函数使用介绍
Aug 18 #PHP
PHP中extract()函数的定义和用法
Aug 17 #PHP
Linux下实现PHP多进程的方法分享
Aug 16 #PHP
PHP基础知识回顾
Aug 16 #PHP
php开发文档 会员收费1期
Aug 14 #PHP
You might like
php的4种常见运行方式
2015/03/20 PHP
php动态生成缩略图并输出显示的方法
2015/04/20 PHP
PHP在线书签系统分享
2016/01/04 PHP
PHP批斗大会之缺失的异常详解
2019/07/09 PHP
php文件上传原理与实现方法详解
2019/12/20 PHP
Jquery实现图片放大镜效果的思路及代码(自写)
2013/10/18 Javascript
jquery获取checkbox的值并post提交
2015/01/14 Javascript
jQuery实现带水平滑杆的焦点图动画插件
2016/03/08 Javascript
jQuery弹出窗口打开链接的实现代码
2016/12/24 Javascript
jQuery使用siblings获取某元素所有同辈(兄弟姐妹)元素用法示例
2017/01/30 Javascript
JS控件bootstrap datepicker使用方法详解
2017/03/25 Javascript
Mobile Web开发基础之四--处理手机设备的横竖屏问题
2017/08/11 Javascript
vue和webpack安装命令详解
2018/06/15 Javascript
详解webpack import()动态加载模块踩坑
2018/07/17 Javascript
vue 框架下自定义滚动条(easyscroll)实现方法
2019/08/29 Javascript
JS桶排序的简单理解与实现方法示例
2019/11/25 Javascript
用yum安装MySQLdb模块的步骤方法
2016/12/15 Python
浅谈python中copy和deepcopy中的区别
2017/10/23 Python
Python实现的选择排序算法原理与用法实例分析
2017/11/22 Python
python中文编码与json中文输出问题详解
2018/08/24 Python
python之yield和Generator深入解析
2019/09/18 Python
Python使用requests模块爬取百度翻译
2020/08/25 Python
GitHub上值得推荐的8个python 项目
2020/10/30 Python
举例详解HTML5中使用JSON格式提交表单
2015/06/16 HTML / CSS
Europcar德国:全球汽车租赁领域的领导者
2018/08/15 全球购物
非功能性需求都包括哪些方面
2013/10/29 面试题
电气工程及其自动化自我评价四篇
2013/09/24 职场文书
《蜗牛》教学反思
2014/02/18 职场文书
2014全国两会学习心得体会1000字
2014/03/10 职场文书
元旦寄语大全
2014/04/10 职场文书
入党积极分子评语
2014/05/04 职场文书
品牌推广策划方案
2014/05/28 职场文书
幼儿园课题方案
2014/06/09 职场文书
2015世界地球日活动总结
2015/02/09 职场文书
SpringBoot整合MongoDB的实现步骤
2021/06/23 MongoDB
基于Python实现流星雨效果的绘制
2022/03/18 Python