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 相关文章推荐
动易数据转成dedecms的php程序
Apr 07 PHP
php与php MySQL 之间的关系
Jul 17 PHP
php win下Socket方式发邮件类
Aug 21 PHP
PHP 判断变量类型实现代码
Oct 23 PHP
php读取EXCEL文件 php excelreader读取excel文件
Dec 06 PHP
PHP中怎样保持SESSION不过期 原理及方案介绍
Aug 08 PHP
Smarty中调用FCKeditor的方法
Oct 27 PHP
适用于初学者的简易PHP文件上传类
Oct 29 PHP
WordPress中用于创建以及获取侧边栏的PHP函数讲解
Dec 29 PHP
php文件上传 你真的掌握了吗
Nov 28 PHP
PHP实现的字符串匹配算法示例【sunday算法】
Dec 19 PHP
php数据结构之顺序链表与链式线性表示例
Jan 22 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
PHPnow安装服务[apache_pn]失败的问题的解决方法
2010/09/10 PHP
php缩小png图片不损失透明色的解决方法
2013/12/25 PHP
PHP生成各种常见验证码和Ajax验证过程
2016/01/10 PHP
yii2分页之实现跳转到具体某页的实例代码
2016/06/02 PHP
PHP7.1实现的AES与RSA加密操作示例
2018/06/15 PHP
让JavaScript 轻松支持函数重载 (Part 1 - 设计)
2009/08/04 Javascript
jquery ui dialog ie8出现滚动条的解决方法
2010/12/06 Javascript
javascript学习笔记(十六) 系统对话框(alert、confirm、prompt)
2012/06/20 Javascript
jQuery 回车事件enter使用示例
2014/02/18 Javascript
angularjs客户端实现压缩图片文件并上传实例
2015/07/06 Javascript
Jquery Easyui自定义下拉框组件使用详解(21)
2020/12/31 Javascript
Javascript 高性能之递归,迭代,查表法详解及实例
2017/01/08 Javascript
Vuejs入门教程之Vue生命周期,数据,手动挂载,指令,过滤器
2017/04/19 Javascript
angular2+node.js express打包部署的实战
2017/07/27 Javascript
对Angular中单向数据流的深入理解
2018/03/31 Javascript
使用vuepress搭建静态博客的示例代码
2019/02/14 Javascript
vue实现div可拖动位置也可改变盒子大小的原理
2020/09/16 Javascript
Python的ORM框架中SQLAlchemy库的查询操作的教程
2015/04/25 Python
python操作mongodb根据_id查询数据的实现方法
2015/05/20 Python
对于Python装饰器使用的一些建议
2015/06/03 Python
Python tkinter实现的图片移动碰撞动画效果【附源码下载】
2018/01/04 Python
pycharm的console输入实现换行的方法
2019/01/16 Python
在python image 中安装中文字体的实现方法
2019/08/22 Python
Python shelve模块实现解析
2019/08/28 Python
python 元组和列表的区别
2020/12/30 Python
用python爬虫批量下载pdf的实现
2020/12/01 Python
详解python的变量缓存机制
2021/01/24 Python
python中用ggplot绘制画图实例讲解
2021/01/26 Python
尤妮佳moony海外旗舰店:日本殿堂级纸尿裤品牌
2018/02/23 全球购物
掌上明珠Java程序员面试总结
2016/02/23 面试题
机械制造专业毕业生求职信
2014/03/02 职场文书
公司演讲稿开场白
2014/08/25 职场文书
党的群众路线教育实践活动整改方案
2014/10/28 职场文书
2014年信贷员工作总结
2014/11/18 职场文书
个人先进事迹材料范文
2014/12/29 职场文书
pytorch 权重weight 与 梯度grad 可视化操作
2021/06/05 Python