当前位置:首页 > PHP教程 > php图像处理 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-26 16:28:15 浏览: 评论:0 

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

php代码如下:

  1. <?php 
  2. header("Content-type:text/html ; charset=utf-8"); 
  3. if (!emptyempty($_POST['submit'])){ 
  4.  $url = $_POST['url']; 
  5.  //为了获取相对路径的图片所做的操作 
  6.  $url_fields = parse_url($url); 
  7.  $main_url = $url_fields['host']; 
  8.  $base_url = substr($url,0,strrpos($url, '/')+1); 
  9.  //获取网页内容 
  10.  //设置代理服务器 
  11.  $opts = array('http'=>array('request_fulluri'=>true)); 
  12.  $context = stream_context_create($opts); 
  13.  $content = file_get_contents($url,false,$context); 
  14.  //匹配img标签,将所有匹配字符串保存到数组$matches 
  15.  $reg = "/<img.*?src=\"(.*?)\".*?>/i"; 
  16.  preg_match_all($reg, $content, $matches); 
  17.  $count = count($matches[0]); 
  18.  for ($i=0; $i<$count; $i++){ 
  19.  /*将所有图片的url转换为小写 
  20.   *$matches[1][$i] = strtolower($matches[1][$i]); 
  21.  */ 
  22.  //如果图片为相对路径就转化为全路径 
  23.  if (!strpos('a'.$matches[1][$i], 'http')){ 
  24.   //因为'/'是第0个位置 
  25.   if (strpos('a'.$matches[1][$i], '/')){ 
  26.   $matches[1][$i] = 'http://'.$main_url.$matches[1][$i]; 
  27.   }else{ 
  28.   $matches[1][$i] = $base_url.$matches[1][$i]; 
  29.   } 
  30.  } 
  31.  } 
  32.  //过滤重复的图片 
  33.  $img_arr = array_unique($matches[1]); 
  34.  //实例化图片下载类 
  35.  $getImg = new DownImage(); 
  36.  $url_count = count($img_arr); 
  37.  for ($i=0; $i<$url_count; $i++){ 
  38.  $getImg->source = $img_arr[$i]; 
  39.  $getImg->save_address = './pic/'; 
  40.  $file = $getImg->download(); 
  41.  } 
  42.  echo "下载完成!哈哈,简单吧!"; 
  43. } 
  44. class DownImage{ 
  45.  public $source;//远程图片URL 
  46.  public $save_address;//保存本地地址 
  47.  public $set_extension; //设置图片扩展名 
  48.  public $quality; //图片的质量(0~100,100最佳,默认75左右) 
  49.  //下载方法(选用GD库图片下载) 
  50.  public function download(){ 
  51.  //获取远程图片信息 
  52.  $info = @getimagesize($this->source); 
  53.  //获取图片扩展名 
  54.  $mime = $info['mime']; 
  55.  $type = substr(strrchr($mime, '/'), 1); 
  56.  //不同的图片类型选择不同的图片生成和保存函数 
  57.  switch($type){ 
  58.   case 'jpeg': 
  59.   $img_create_func = 'imagecreatefromjpeg'; 
  60.   $img_save_func = 'imagejpeg'; 
  61.   $new_img_ext = 'jpg'; 
  62.   $image_quality = isset($this->quality) ? $this->quality : 100; 
  63.   break; 
  64.   case 'png': 
  65.   $img_create_func = 'imagecreatefrompng'; 
  66.   $img_save_func = 'imagepng'; 
  67.   $new_img_ext = 'png'; 
  68.   break; 
  69.   case 'bmp': 
  70.   $img_create_func = 'imagecreatefrombmp'; 
  71.   $img_save_func = 'imagebmp'; 
  72.   $new_img_ext = 'bmp'; 
  73.   break; 
  74.   case 'gif': 
  75.   $img_create_func = 'imagecreatefromgif'; 
  76.   $img_save_func = 'imagegif'; 
  77.   $new_img_ext = 'gif'; 
  78.   break; 
  79.   case 'vnd.wap.wbmp': 
  80.   $img_create_func = 'imagecreatefromwbmp'; 
  81.   $img_save_func = 'imagewbmp'; 
  82.   $new_img_ext = 'bmp'; 
  83.   break; 
  84.   case 'xbm': 
  85.   $img_create_func = 'imagecreatefromxbm'; 
  86.   $img_save_func = 'imagexbm'; 
  87.   $new_img_ext = 'xbm'; 
  88.   break; 
  89.   default: 
  90.   $img_create_func = 'imagecreatefromjpeg'; 
  91.   $img_save_func = 'imagejpeg'; 
  92.   $new_img_ext = 'jpg'; 
  93.  } 
  94.  //根据是否设置扩展名来合成本地文件名 
  95.  if (isset($this->set_extension)){ 
  96.   $ext = strrchr($this->source,"."); 
  97.   $strlen = strlen($ext); 
  98.   $newname = basename(substr($this->source,0,-$strlen)).'.'.$new_img_ext; 
  99.  }else{ 
  100.   $newname = basename($this->source); 
  101.  } 
  102.    
  103.  //生成本地文件路径 
  104.  $save_address = $this->save_address.$newname; 
  105.  $img = @$img_create_func($this->source); 
  106.  if (isset($image_quality)){ 
  107.   $save_img = @$img_save_func($img,$save_address,$image_quality); 
  108.  }else{ 
  109.   $save_img = @$img_save_func($img,$save_address); 
  110.  } 
  111.  return $save_img;  
  112.  } 
  113. } 
  114. ?> 
  115. <form method="POST" action=""> 
  116. 远程url地址:<input type="text" name="url" size=30 /> 
  117. <input type="submit" name="submit" value="下载该页面所有图片" /> 
  118. </form>

Tags: gd库下载图片

分享到: