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 strstr查找字符串中是否包含某些字符的查找函数
Jun 03 PHP
PHP中的cookie不用刷新就生效的方法
Feb 04 PHP
Thinkphp使用mongodb数据库实现多条件查询方法
Jun 26 PHP
php中单个数据库字段多列显示(单字段分页、横向输出)
Jul 28 PHP
php通过asort()给关联数组按照值排序的方法
Mar 18 PHP
php中关于socket的系列函数总结
May 18 PHP
windows下apache搭建php开发环境
Aug 27 PHP
深入理解PHP原理之执行周期分析
Jun 01 PHP
对比PHP对MySQL的缓冲查询和无缓冲查询
Jul 01 PHP
Yii使用smsto短信接口的函数demo示例
Jul 13 PHP
Laravel框架中VerifyCsrfToken报错问题的解决
Aug 30 PHP
PHP7.1实现的AES与RSA加密操作示例
Jun 15 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图片等比例缩放
2015/07/28 PHP
php微信公众号开发模式详解
2016/11/28 PHP
laravel返回统一格式错误码问题
2019/11/04 PHP
javascript mouseover、mouseout停止事件冒泡的解决方案
2009/04/07 Javascript
js中settimeout方法加参数的使用实例
2014/02/27 Javascript
JQuery记住用户名和密码的具体实现
2014/04/04 Javascript
jQuery操作表格(table)的常用方法、技巧汇总
2014/04/12 Javascript
JavaScript的jQuery库中function的存在和参数问题
2015/08/13 Javascript
jQuery利用sort对DOM元素进行排序操作
2016/11/07 Javascript
详解Nodejs 通过 fs.createWriteStream 保存文件
2017/10/10 NodeJs
微信小程使用swiper组件实现图片轮播切换显示功能【附源码下载】
2017/12/12 Javascript
浅谈Webpack下多环境配置的思路
2018/06/27 Javascript
解决axios发送post请求返回400状态码的问题
2018/08/11 Javascript
详解Vue 项目中的几个实用组件(ts)
2019/10/29 Javascript
[55:32]2018DOTA2亚洲邀请赛 4.4 淘汰赛 EG vs LGD 第二场
2018/04/05 DOTA
python和bash统计CPU利用率的方法
2015/07/10 Python
python数据类型_元组、字典常用操作方法(介绍)
2017/05/30 Python
Python获取指定文件夹下的文件名的方法
2018/02/06 Python
分析python动态规划的递归、非递归实现
2018/03/04 Python
Python实战之制作天气查询软件
2019/05/14 Python
在主流系统之上安装Pygame的方法
2020/05/20 Python
解决Alexnet训练模型在每个epoch中准确率和loss都会一升一降问题
2020/06/17 Python
Python如何读取、写入JSON数据
2020/07/28 Python
python获取本周、上周、本月、上月及本季的时间代码实例
2020/09/08 Python
python em算法的实现
2020/10/03 Python
中国排名第一的外贸销售网站:LightInTheBox.com(兰亭集势)
2016/10/28 全球购物
英国蛋糕装饰用品一站式商店:Craft Company
2019/03/18 全球购物
内部类的定义、种类以及优点
2013/10/16 面试题
大型晚会策划方案
2014/02/06 职场文书
《花木兰》教学反思
2014/04/09 职场文书
庐山导游词
2015/02/03 职场文书
青岛海底世界导游词
2015/02/11 职场文书
开除员工通知
2015/04/22 职场文书
债务纠纷起诉书
2015/05/20 职场文书
工作建议书范文
2019/07/08 职场文书
Vertica集成Apache Hudi重磅使用指南
2022/03/31 Servers