php获取远程图片

jerry thinkphp 2015年11月18日 收藏

用curl的实现了,一下代码我测试成功的哦

  1. function auto_save_image($body, $path='default'){
  2. // $img_array = explode('&',$body);
  3. $img_array = array();
  4. preg_match_all("/(src)=[\"|\'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))[\"|\'| ]{0,}/isU", $body, $img_array);
  5. $img_array = array_unique($img_array[2]); //也可以自动匹配
  6. // leePrint($img_array);
  7. set_time_limit(0);
  8. $imgPath = SITE_PATH.'Public/Uploads/'.$path.'/'.date('Ymd');
  9. $milliSecond = strftime("%H%M%S",time());
  10. if(!is_dir($imgPath)) @mkdir($imgPath,0777);
  11. // echo $imgPath;
  12. // leePrint(is_dir($imgPath));
  13. foreach($img_array as $key =>$url) {
  14. $url = trim($url);
  15. $curl = curl_init(); 
  16. // 设置你需要抓取的URL 
  17. curl_setopt($curl, CURLOPT_URL, $url); 
  18. // 设置header 
  19. curl_setopt($curl, CURLOPT_HEADER, 0); 
  20. // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。 
  21. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  22. // 运行cURL,请求网页 
  23. $file = curl_exec($curl); 
  24. // 关闭URL请求 
  25. curl_close($curl); 
  26. //将文件写入获得的数据 
  27. $filename = $imgPath.'/'.md5_file($url).'.'.substr($url,-3,3);
  28. // leePrint($filename);
  29. $write = @fopen($filename,"w"); 
  30. // leePrint($write);
  31. if($write==false) { 
  32. return false; 
  33. } 
  34. if(fwrite($write,$file)==false) { 
  35. return false; 
  36. } 
  37. if(fclose($write)==false) { 
  38. return false; 
  39. }
  40. // leePrint($filename);
  41. $filename = str_replace(SITE_PATH, '/', $filename);
  42. $body = @ereg_replace($url, $filename, $body);
  43. }
  44. return $body;
  45. }

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

  1. <?php

        $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';

        auto_save_image($str);


        function auto_save_image($body){
            $img_array = explode('&',$body);
            /*$img_array = array();
            preg_match_all("/(src)=[\"|\'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))[\"|\'| ]{0,}/isU", $body, $img_array);
            $img_array = array_unique($img_array[2]);*/ //也可以自动匹配
            set_time_limit(0);
            $imgPath = "uploads/allimg/".date("Ymd")."/";
            $milliSecond = strftime("%H%M%S",time());
            if(!is_dir($imgPath)) @mkdir($imgPath,0777);
            foreach($img_array as $key =>$value)
            {
                    $value = trim($value);
                    $get_file = @file_get_contents($value);
                    $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);
                    if($get_file)
                    {
                            $fp = @fopen($rndFileName,"w");
                            @fwrite($fp,$get_file);
                            @fclose($fp);
                    }
                    $body = @ereg_replace($value, $rndFileName, $body);
            }
         
            return $body;
        }