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设计模式小结
Feb 15 PHP
php连接与操作PostgreSQL数据库的方法
Dec 25 PHP
php将文本文件转换csv输出的方法
Dec 31 PHP
PHP IDE PHPStorm配置支持友好Laravel代码提示方法
May 12 PHP
ThinkPHP开发框架函数详解:C方法
Aug 14 PHP
php 使用curl模拟登录人人(校内)网的简单实例
Jun 06 PHP
Laravel如何使用数据库事务及捕获事务失败后的异常详解
Oct 23 PHP
PHP实现链式操作的三种方法详解
Nov 16 PHP
PHP性能分析工具xhprof的安装使用与注意事项
Dec 19 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
Feb 11 PHP
实例介绍PHP删除数组中的重复元素
Mar 03 PHP
Laravel自动生成UUID,从建表到使用详解
Oct 24 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
给海燕B411配件机起死回生配上件
2021/03/02 无线电
拼音码表的生成
2006/10/09 PHP
PHP 转义使用详解
2013/07/15 PHP
PHP面向对象教程之自定义类
2014/06/10 PHP
ThinkPHP的截取字符串函数无法显示省略号的解决方法
2014/06/25 PHP
PHP给前端返回一个JSON对象的实例讲解
2018/05/31 PHP
浅析jQuery1.8的几个小变化
2013/12/10 Javascript
JavaScript不刷新实现浏览器的前进后退功能
2014/11/05 Javascript
鼠标经过子元素触发mouseout,mouseover事件的解决方案
2015/07/26 Javascript
JS设置下拉列表框当前所选值的方法
2015/12/22 Javascript
基于axios 解决跨域cookie丢失的问题
2018/09/26 Javascript
express框架下使用session的方法
2019/07/31 Javascript
vue实现简单跑马灯效果
2020/05/25 Javascript
浅谈element中InfiniteScroll按需引入的一点注意事项
2020/06/05 Javascript
Element Rate 评分的使用方法
2020/07/27 Javascript
JS实现小米轮播图
2020/09/21 Javascript
[01:03:51]2018DOTA2亚洲邀请赛 4.7 淘汰赛 VP vs LGD 第三场
2018/04/09 DOTA
Python中类型关系和继承关系实例详解
2015/05/25 Python
Python基于pygame实现的font游戏字体(附源码)
2015/11/11 Python
简介Python设计模式中的代理模式与模板方法模式编程
2016/02/02 Python
Python端口扫描简单程序
2016/11/10 Python
Python子类继承父类构造函数详解
2019/02/19 Python
Python实现钉钉发送报警消息的方法
2019/02/20 Python
python图像和办公文档处理总结
2019/05/28 Python
flask框架json数据的拿取和返回操作示例
2019/11/28 Python
python GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的详细方法与实例
2020/03/09 Python
sublime3之内网安装python插件Anaconda的流程
2020/11/10 Python
Python+MySQL随机试卷及答案生成程序的示例代码
2021/02/01 Python
师范生实习个人的自我评价
2013/09/28 职场文书
个人求职简历中英文自我评价
2013/12/16 职场文书
直接有效的自我评价
2014/01/11 职场文书
北体毕业生求职信
2014/02/28 职场文书
三严三实·严以用权心得体会
2016/01/12 职场文书
2016年青少年禁毒宣传教育活动总结(学校)
2016/04/05 职场文书
导游词之开封禹王台风景区
2019/12/02 职场文书
python井字棋游戏实现人机对战
2022/04/28 Python