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(2)
Oct 09 PHP
php 无法加载mysql的module的时候的配置的解决方案引发的思考
Jan 27 PHP
PHP中图片等比缩放的实例
Mar 24 PHP
PHP笔记之:基于面向对象设计的详解
May 14 PHP
PHP循环函数使用介绍之PHP基础入门教程
Sep 21 PHP
CI(CodeIgniter)框架介绍
Jun 09 PHP
使用symfony命令创建项目的方法
Mar 17 PHP
php中文字符串截取多种方法汇总
Oct 06 PHP
php使用crypt()函数进行加密
Jun 08 PHP
PHP微信公众号开发之微信红包实现方法分析
Jul 14 PHP
PHP封装的数据库模型Model类完整示例【基于PDO】
Mar 14 PHP
php+js实现的无刷新下载文件功能示例
Aug 23 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/29 PHP
PHP5.6新增加的可变函数参数用法分析
2017/08/25 PHP
laravel框架查询数据集转为数组的两种方法
2019/10/10 PHP
javascript面向对象入门基础详细介绍
2012/09/05 Javascript
js中for in的用法示例解析
2013/12/25 Javascript
js Calender控件使用详解
2015/01/05 Javascript
jquery 插件实现多行文本框[textarea]自动高度
2015/03/04 Javascript
JS替换字符串中空格方法
2015/04/17 Javascript
JavaScript编程学习技巧汇总
2016/02/21 Javascript
JavaScript模仿Pinterest实现图片预加载功能
2016/10/25 Javascript
简单实现jQuery多选框功能
2017/01/09 Javascript
jQuery实现动态删除LI的方法
2017/05/30 jQuery
jQuery选择器之属性过滤选择器详解
2017/09/28 jQuery
bootstrap fileinput插件实现预览上传照片功能
2018/01/23 Javascript
使用Javascript简单计算器
2018/11/17 Javascript
js验证身份证号码记录的方法
2019/04/26 Javascript
解决使用layui的时候form表单中的select等不能渲染的问题
2019/09/18 Javascript
Vue项目中数据的深度监听或对象属性的监听实例
2020/07/17 Javascript
详解vue-cli项目在IE浏览器打开报错解决方法
2020/12/10 Vue.js
Python使用Mechanize模块编写爬虫的要点解析
2016/03/31 Python
解决Python中字符串和数字拼接报错的方法
2016/10/23 Python
python 计算文件的md5值实例
2017/01/13 Python
Apache如何部署django项目
2017/05/21 Python
TensorFlow在MAC环境下的安装及环境搭建
2017/11/14 Python
python字符串下标与切片及使用方法
2020/02/13 Python
PyCharm 2020 激活到 2100 年的教程
2020/03/25 Python
Python之Matplotlib文字与注释的使用方法
2020/06/18 Python
美国生日蛋糕店:Bake Me A Wish!
2017/02/08 全球购物
英国皇家邮政海外旗舰店:Royal Mail
2018/02/21 全球购物
优秀团队获奖感言
2014/02/19 职场文书
工作求职自荐信
2014/06/13 职场文书
2016春节家属慰问信
2015/03/25 职场文书
农村党支部承诺书
2015/04/30 职场文书
Nginx的rewrite模块详解
2021/03/31 Servers
Python中的变量与常量
2021/11/11 Python
vue中的可拖拽宽度div的实现示例
2022/04/08 Vue.js