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下对字符串的递增运算代码
Aug 21 PHP
初品cakephp 入门基础
Feb 16 PHP
Zend Studio 实用快捷键一览表(精心整理)
Aug 10 PHP
PHP实现自动识别Restful API的返回内容类型
Feb 07 PHP
PHP使用CURL模拟登录的方法
Jul 08 PHP
php使用pclzip类实现文件压缩的方法(附pclzip类下载地址)
Apr 30 PHP
PHP实现小偷程序实例
Oct 31 PHP
php面试中关于面向对象的相关问题
Feb 13 PHP
Laravel框架控制器的middleware中间件用法分析
Sep 30 PHP
php实现微信和支付宝支付的示例代码
Aug 11 PHP
PHP7 整型处理机制修改
Mar 09 PHP
PHP实现创建以太坊钱包转账等功能
Apr 21 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安装问题
2006/10/09 PHP
将文件夹压缩成zip文件的php代码
2009/12/14 PHP
PHP实现手机归属地查询API接口实现代码
2012/08/27 PHP
详解WordPress开发中的get_post与get_posts函数使用
2016/01/04 PHP
php魔法函数与魔法常量使用介绍
2017/07/23 PHP
php使用环形链表解决约瑟夫问题完整示例
2018/08/07 PHP
PHP+Apache实现二级域名之间共享cookie的方法
2019/07/24 PHP
Ajax一统天下之Dojo整合篇
2007/03/24 Javascript
一个判断email合法性的函数[非正则]
2008/12/09 Javascript
Js-$.extend扩展方法使方法参数更灵活
2013/01/15 Javascript
js 获取、清空input type=&quot;file&quot;的值示例代码
2014/02/19 Javascript
javascript动画算法实例分析
2015/07/31 Javascript
深入浅析JavaScript中的constructor
2016/04/19 Javascript
jQuery中 bind的用法简单介绍
2017/02/13 Javascript
Bootstrap里的文件分别代表什么意思及其引用方法
2017/05/01 Javascript
Echarts基本用法_动力节点Java学院整理
2017/08/11 Javascript
bootstrap3-dialog-master模态框使用详解
2017/08/22 Javascript
JS实现div模块的截图并下载功能
2017/10/17 Javascript
vue使用mint-ui实现下拉刷新和无限滚动的示例代码
2017/11/06 Javascript
python二叉树遍历的实现方法
2013/11/21 Python
Python基于有道实现英汉字典功能
2015/07/25 Python
总结python实现父类调用两种方法的不同
2017/01/15 Python
Python基于动态规划算法解决01背包问题实例
2017/12/06 Python
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
2019/04/26 Python
Python中调用其他程序的方式详解
2019/08/06 Python
python打印直角三角形与等腰三角形实例代码
2019/10/20 Python
python集合删除多种方法详解
2020/02/10 Python
python简单实现最大似然估计&amp;scipy库的使用详解
2020/04/15 Python
关于python的缩进规则的知识点详解
2020/06/22 Python
Django实现随机图形验证码的示例
2020/10/15 Python
简单介绍CSS3中Media Query的使用
2015/07/07 HTML / CSS
加拿大床上用品、家居装饰、厨房和浴室产品购物网站:Linen Chest
2018/06/05 全球购物
法国最大的在线眼镜店:EasyLunettes
2019/08/26 全球购物
实现向右循环移位
2014/07/31 面试题
J2ee常用的设计模式?说明工厂模式
2015/05/21 面试题
2016元旦主持人经典开场白台词
2015/12/03 职场文书