php中使用gd库实现下载网页中所有图片


Posted in PHP onMay 12, 2015

在前期的php教程就讲了php gd库可以实现远程图片的下载,但是那只是下载了一张图片,原理是一样的,要想下载一个网页的所有图片只要使用正则表达式进行判断,找出所有的图片url就可以进行循环下载了,我特地参照网络资源编写了gd库图片下载类!
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>

运行结果如图:

php中使用gd库实现下载网页中所有图片

PHP 相关文章推荐
连接到txt文本的超链接,不直接打开而是点击后下载的处理方法
Jul 01 PHP
获取PHP警告错误信息的解决方法
Jun 03 PHP
使用Appcan客户端自动更新PHP版本号(全)
Jul 31 PHP
浅析php设计模式之数据对象映射模式
Mar 03 PHP
PHP目录与文件操作技巧总结(创建,删除,遍历,读写,修改等)
Sep 11 PHP
PHP验证终端类型是否为手机的简单实例
Feb 07 PHP
php中序列化与反序列化详解
Feb 13 PHP
thinkphp 验证码 的使用小结
May 07 PHP
PHP简单实现遍历目录下特定文件的方法小结
May 22 PHP
thinkPHP中钩子的使用方法实例分析
Nov 16 PHP
PHP simplexml_load_string()函数实例讲解
Feb 03 PHP
PHP抽象类与接口的区别详解
Mar 21 PHP
php中使用gd库实现远程图片下载实例
May 12 #PHP
PHP输入输出流学习笔记
May 12 #PHP
PHP SPL标准库之SplFixedArray使用实例
May 12 #PHP
php中get_defined_constants函数用法实例分析
May 12 #PHP
PHP SPL标准库之数据结构栈(SplStack)介绍
May 12 #PHP
php遍历类中包含的所有元素的方法
May 12 #PHP
PHP 双链表(SplDoublyLinkedList)简介和使用实例
May 12 #PHP
You might like
打造计数器DIY三步曲(下)
2006/10/09 PHP
Banner程序
2006/10/09 PHP
在数据量大(超过10万)的情况下
2007/01/15 PHP
php中session_unset与session_destroy的区别分析
2011/06/16 PHP
php命令行使用方法和命令行参数说明
2014/04/08 PHP
PHP Curl出现403错误的解决办法
2014/05/29 PHP
PHP对象克隆clone用法示例
2016/09/28 PHP
php修改数组键名的方法示例
2017/04/15 PHP
PHP实现RSA签名生成订单功能【支付宝示例】
2017/06/06 PHP
JS构建页面的DOM节点结构的实现代码
2011/12/09 Javascript
40个有创意的jQuery图片、内容滑动及弹出插件收藏集之一
2011/12/31 Javascript
jQuery制作可自定义大小的拼图游戏
2015/03/30 Javascript
JS仿百度自动下拉框模糊匹配提示
2016/07/25 Javascript
JS实现复制内容到剪贴板功能
2017/02/05 Javascript
jquery仿苹果的时间/日期选择效果
2017/03/08 Javascript
浅析JS中常用类型转换及运算符表达式
2017/07/23 Javascript
JS/HTML5游戏常用算法之碰撞检测 包围盒检测算法详解【凹多边形的分离轴检测算法】
2018/12/13 Javascript
详解VScode编辑器vue环境搭建所遇问题解决方案
2019/04/26 Javascript
vue父子组件通信的高级用法示例
2019/08/29 Javascript
Vue中使用Lodop插件实现打印功能的简单方法
2019/12/19 Javascript
JS实现省市县三级下拉联动
2020/04/10 Javascript
原生js实现移动小球(碰撞检测)
2020/12/17 Javascript
Python 读取某个目录下所有的文件实例
2018/06/23 Python
Python 移动光标位置的方法
2019/01/20 Python
Python安装与基本数据类型教程详解
2019/05/29 Python
详解Python 切片语法
2019/06/10 Python
python实现将文件夹内的每张图片批量分割成多张
2019/07/22 Python
CSS3 选择器 基本选择器介绍
2012/01/21 HTML / CSS
韩国三星旗下的一家超市连锁店:Home Plus
2016/07/30 全球购物
意大利在线大学图书馆:Libreria universitaria
2019/07/16 全球购物
工艺工程师工作职责
2013/11/23 职场文书
护林防火标语
2014/06/27 职场文书
2014年大学宣传部工作总结
2014/12/19 职场文书
2015年乡镇残联工作总结
2015/05/13 职场文书
2019优秀干部竞聘演讲稿范文!
2019/07/02 职场文书
golang实现浏览器导出excel文件功能
2022/03/25 Golang