最新消息: 新版网站上线了!!!

thinkphp 获取远程图片

  1. 后台添加文章时,直接下载远程图片

  2.  

  3.     $str = 'http://www.thinkphp.cn/Uploads/info/2013-07-24/51ef32f4490e3_100_100.jpg&http://www.thinkphp.cn/Uploads/ad/2013-05-09/518b1b272d448.jpg&http://www.thinkphp.cn/Uploads/ad/2013-07-02/51d24ce472da0.jpg&http://www.thinkphp.cn/Uploads/ad/2013-07-04/51d4c7c4aa490.jpg';

  4.  

  5.     auto_save_image($str);

  6.  

  7.  

  8.     function auto_save_image($body){

  9.         $img_array = explode('&',$body);

  10.         /*$img_array = array();

  11.         preg_match_all("/(src)=[\"|\'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))[\"|\'| ]{0,}/isU", $body, $img_array);

  12.         $img_array = array_unique($img_array[2]);*/ //也可以自动匹配

  13.         set_time_limit(0);

  14.         $imgPath = "uploads/allimg/".date("Ymd")."/";

  15.         $milliSecond = strftime("%H%M%S",time());

  16.         if(!is_dir($imgPath)) @mkdir($imgPath,0777);

  17.         foreach($img_array as $key =>$value)

  18.         {

  19.                 $value = trim($value);

  20.                 $get_file = @file_get_contents($value);

  21.                 $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);

  22.                 if($get_file)

  23.                 {

  24.                         $fp = @fopen($rndFileName,"w");

  25.                         @fwrite($fp,$get_file);

  26.                         @fclose($fp);

  27.                 }

  28.                 $body = @ereg_replace($value, $rndFileName, $body);

  29.         }

  30.      

  31.         return $body;

  32.     }

 

 

 

  1.    单图片采集

  2.  

  3.     $str = 'http://www.thinkphp.cn/Uploads/info/2013-07-24/51ef32f4490e3_100_100.jpg';

  4.  

  5.     auto_save_image($str);

  6.  

  7.  

  8.     

     

  9. function auto_save_image($imgurl){

  10.         set_time_limit(0);

  11.         $imgPath = "./Uploads/Picture/".date("Y-m-d");

  12.         if(!is_dir($imgPath)) @mkdir($imgPath, 0777);

  13.  

  14.         $get_file = @file_get_contents($imgurl);

  15.         $rndFileName = $imgPath."/".time().".jpg";

  16.         if($get_file)

  17.         {

  18.                 $fp = @fopen($rndFileName, "w");

  19.                 @fwrite($fp, $get_file);

  20.                 @fclose($fp);

  21.         }

  22.         return $rndFileName;

  23. }

转载请注明:谷谷点程序 » thinkphp 获取远程图片