国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php將遠(yuǎn)程圖片保存到本地服務(wù)器的實現(xiàn)代碼

2024-05-04 23:38:33
字體:
供稿:網(wǎng)友

有些時候我們想保存一些漂亮圖片,但又不想手動去保存下來,尤其是大批量的存儲,這個時候我們需要寫一段程序來幫助我們完成這個工作,本文介紹了php如何將遠(yuǎn)程圖片本地化,需要的朋友可以參考下

php如何將遠(yuǎn)程圖片本地化,本文分享了實現(xiàn)代碼

 

 
  1. <?php  
  2. //站點根目錄  
  3. $cfg_basedir = dirname(__FILE__);  
  4. //停建目錄屬性  
  5. $cfg_dir_purview ='0755';  
  6. /*Get請求遠(yuǎn)程內(nèi)容函數(shù)*/ 
  7. $cookie_file = dirname(__FILE__).'/cookie.txt'//COOKIE存放地址  
  8. function pget($url,$ref=false,$head=false){  
  9. $curl = curl_init(); // 啟動一個CURL會話  
  10. curl_setopt($curl, CURLOPT_URL, $url); // 要訪問的地址  
  11. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對認(rèn)證證書來源的檢查  
  12. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 從證書中檢查SSL加密算法是否存在  
  13. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模擬用戶使用的瀏覽器  
  14. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉(zhuǎn)  
  15. if($ref){ curl_setopt($curl, CURLOPT_REFERER, $ref);//帶來的Referer  
  16. }else{  
  17. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設(shè)置Referer  
  18. }  
  19. curl_setopt($curl, CURLOPT_HTTPGET, 1); // 發(fā)送一個常規(guī)的Post請求  
  20. curl_setopt($curl, CURLOPT_COOKIEJAR, $GLOBALS['cookie_file']); // 存放Cookie信息的文件名稱  
  21. curl_setopt($curl, CURLOPT_COOKIEFILE,$GLOBALS ['cookie_file']); // 讀取上面所儲存的Cookie信息  
  22. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設(shè)置超時限制防止死循環(huán)  
  23. curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區(qū)域內(nèi)容  
  24. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回  
  25. $tmpInfo = curl_exec($curl); // 執(zhí)行操作  
  26. if (curl_errno($curl)) {  
  27. echo 'Errno'.curl_error($curl);  
  28. }  
  29. if($head){ $data['head']=curl_getinfo($curl);}  
  30. curl_close($curl); // 關(guān)鍵CURL會話  
  31. $data['data']=$tmpInfo;  
  32. return $data// 返回數(shù)據(jù)  
  33. }  
  34.  
  35.  
  36. /**  
  37. * 創(chuàng)建所有目錄  
  38.  
  39. * @param string $truepath 真實地址  
  40. * @param string $mmode 模式  
  41. * @return bool  
  42. */ 
  43. if ( ! function_exists('MkdirAll'))  
  44. {  
  45. function MkdirAll($truepath)  
  46. global $cfg_dir_purview;  
  47. if(!file_exists($truepath))  
  48. {  
  49. mkdir($truepath$cfg_dir_purview);  
  50. chmod($truepath$cfg_dir_purview);  
  51. return true;  
  52. }  
  53. else 
  54. {  
  55. return true;  
  56. }  
  57. }  
  58. }  
  59. /**  
  60. * 獲得文章body里的外部資源  
  61.  
  62. * @access public  
  63. * @param string $body 文檔內(nèi)容  
  64. * @return string  
  65. */ 
  66. function GetCurContent($body)  
  67. {  
  68. global $cfg_multi_site,$cfg_basehost,$cfg_basedir,$cfg_image_dir,$arcID,$cuserLogin,$dsql;  
  69. $cfg_uploaddir = '/c';  
  70. $basehost = "http://".$_SERVER["HTTP_HOST"];  
  71.  
  72. $img_array = array();  
  73. preg_match_all("/src=[/"|'|/s]{0,}(http:////([^>]*)/.(gif|jpg|png))/isU",$body,$img_array);  
  74.  
  75. $img_array = array_unique($img_array[1]);  
  76. $imgUrl = $cfg_uploaddir.'/'.date("ymd", time());  
  77. $imgPath = $cfg_basedir.$imgUrl;  
  78. if(!is_dir($imgPath.'/'))  
  79. {  
  80. MkdirAll($imgPath$GLOBALS['cfg_dir_purview']);  
  81. CloseFtp();  
  82. }  
  83. $milliSecond = date('His',time());  
  84. foreach($img_array as $key=>$value)  
  85. {  
  86. if(preg_match("#".$basehost."#i"$value))  
  87. {  
  88. continue;  
  89. }  
  90. if(preg_match("#".$basehost."#i"$value))  
  91. {  
  92. continue;  
  93. }  
  94. if(!preg_match("#^http:////#i"$value))  
  95. {  
  96. continue;  
  97. }  
  98.  
  99. $http=pget($value,'$value',true);  
  100. $itype=($http['head']['content_type']);  
  101. if(!preg_match("#/.(jpg|gif|png)#i"$itype))  
  102. {  
  103. if($itype=='image/gif')  
  104. {  
  105. $itype = ".gif";  
  106. }  
  107. else if($itype=='image/png')  
  108. {  
  109. $itype = ".png";  
  110. }  
  111. else 
  112. {  
  113. $itype = '.jpg';  
  114. }  
  115. }  
  116. $milliSecondN = rand(1000,9999).rand(1000,9999);  
  117. $value = trim($value);  
  118. $rndFileName = $imgPath.'/'.$milliSecondN.'-'.$key.$itype;  
  119. $fileurl = $imgUrl.'/'.$milliSecondN.'-'.$key.$itype;  
  120.  
  121. $tp = fopen($rndFileName'wb');  
  122. fwrite($tp$http['data']);  
  123. fclose($tp);  
  124.  
  125. if(file_exists($cfg_basedir.$fileurl))  
  126. {  
  127. $info = '';  
  128. $imginfos = GetImageSize($rndFileName$info);  
  129. $fsize = filesize($rndFileName);  
  130. $body = str_replace($value$fileurl$body);  
  131.  
  132. }  
  133. }  
  134. return $body;  
  135. }  
  136.  
  137. //調(diào)用方式  
  138. echo GetCurContent($body);  

以上就是實現(xiàn)遠(yuǎn)程圖片本地化的全部代碼,希望對大家的學(xué)習(xí)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沁水县| 施秉县| 辽阳市| 麻江县| 乌什县| 大同县| 潼关县| 康定县| 建平县| 松滋市| 芮城县| 英山县| 瑞丽市| 静海县| 清苑县| 恭城| 新和县| 财经| 长宁县| 海南省| 赣州市| 梅河口市| 江阴市| 白城市| 铜山县| 来凤县| 中山市| 吉木萨尔县| 永丰县| 铜山县| 霍州市| 威宁| 札达县| 昆明市| 常山县| 高碑店市| 黄骅市| 岱山县| 宁国市| 修文县| 定远县|