实现PHP168图片模型的采集、发布、缩略图提取下载

使用火车采集器往PHP168发布数据,总有很多问题,从V5到现在的V6。

由于使用的人数并不多,这里就不多讲问题原因了。

以下是我的解决办法,简单思路为:

  1. 使用HTML代码的形式,发布图片内容,使用正则提取图片地址。
  2. 分析每条图片地址,如果是远程图片,就将图片下载到地。
  3. 检查缩略图是否存在,不存的话,自动从内容中提取缩略图。

以下是代码片段。

[php]

 //过滤火车采集器发布的错误
 preg_match("@\[@", $postdb[picurl]) && $postdb[picurl]=”;
 
 //图片模型的图片处理
 if(isset($post_db[photourl][locoy])){
  
  //获取图片地址
  $post_db[photourl][locoy] = stripslashes($post_db[photourl][locoy]);
  $match = "<img[^>]*?src=[\"’\s]*?([^>\"’]*?)[\"’>]*?[^>]*?>";
  preg_match_all("@$match@is", $post_db[photourl][locoy], $post_db[photourl][img]);
  $post_db[photourl][img] = array_unique($post_db[photourl][img][0]);
  //print_r($post_db[photourl][img]);

  //初始化内容
  $post_db[photourl][name] = $post_db[photourl][url] = array();
  function getInfoFromIMG($img, $key)
  {
   $match = "$key=[\"’\s]*?([^>\"’\s]*?)[\"’>\s]*?[^>]*?>";
   preg_match_all("@$match@isU", $img, $key);
   return trim($key[1][0],",\"\\ /’");
  }
  
  //循环处理图片地址
  foreach ($post_db[photourl][img] as $key=>$img)
  {
   $img_url = getInfoFromIMG($img, ‘src’);
   if($img_url){
    $img_url = preg_match("@://@", $img_url) ? get_outpic($img_url, $fid, 1) : $img_url;
    $post_db[photourl][url][$key] = $img_url;
    $post_db[photourl][name][$key] = getInfoFromIMG($img, ‘alt’);
    (empty($postdb[picurl]) || !isset($postdb[picurl])) && $postdb[picurl] = $post_db[photourl][url][$key];
   }else continue;
  }
  
  preg_match("@://@", $postdb[picurl]) && $postdb[picurl] = get_outpic($postdb[picurl], $fid, 1);
 }

[/php]

发表评论