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

PHP 下载远程网页图片并且保存在本地实例

发布:smiling 来源: PHP粉丝网  添加日期:2014-07-21 11:22:03 浏览: 评论:0 

我们要取远程服务器中网页的图片然后保存到我们本地需要珍到php fopen或curl等等这类的函数,下面我给大家介绍几个常用的实例.

fopen函数实例

ob_start:打开输出缓冲

readfile:读入一个文件并写入到输出缓冲

返回从文件中读入的字节数,如果出错返回 FALSE 并且除非是以 @readfile() 形式调用,否则会显示错误信息.

ob_get_contents : Return the contents of the output buffer(返回输出缓冲的内容)

This will return the contents of the output buffer without clearing it or FALSE, if output buffering isn’t active.(如果输出缓冲没有活动(打开),则返回 FALSE)

ob_end_clean() : Clean (erase) the output buffer and turn off output buffering(清除输出缓冲)

代码如下:

  1. <?php 
  2. //URL是远程的完整图片地址,不能为空, $filename 是另存为的图片名字 
  3. //默认把图片放在以此脚本相同的目录里 
  4. function GrabImage($url,$filename=""){ 
  5.         if($url == ""){ 
  6.             return false; 
  7.         } 
  8.          
  9.         $ext=strrchr($url,"."); 
  10.          
  11.         if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp" && $ext != ".png"){ 
  12.             echo "格式不支持!"; 
  13.             return false; 
  14.         } 
  15.          
  16.         if($filename == ""){ 
  17.             $filename = time()."$ext"; 
  18.         } 
  19.          
  20.         ob_start(); 
  21.         readfile($url); 
  22.         $img=ob_get_contents(); 
  23.         ob_end_clean(); 
  24.         $size=strlen($img); 
  25.         $fp2=fopen($filename,"a"); 
  26.         if(fwrite($fp2,$img) === false){ 
  27.             echo "不能写入文件".$filename; 
  28.             exit(); 
  29.         }else{ 
  30.             echo "保存图片成功!"; 
  31.         } 
  32.         fclose($fp2); 
  33.         return $filename; 
  34.          
  35.     } 
  36. //测试 
  37. GrabImage("/logo.png","as.png"); 
  38. ?> 

php下载远程图片函数,可伪造来路.

$gurl 要下载的图片地址

$rfurl 来路。如果目标图像做了防盗链设置,可以绕过。

$filename 下载图片保存的文件名,相对路径,不要用realpath

$gcookie 调整cookie 伪造的cookie

$JumpCount 跳转计数

$maxtime 最大次数

调用方法:

DownImageKeep(“http://www.baidu.com/img/baidu_jgylogo2.gif”,”http://baidu.com”,”a.gif”,”",0,10);

代码如下:

  1. function DownImageKeep($gurl, $rfurl, $filename, $gcookie=”", $JumpCount=0, $maxtime=30) 
  2. { 
  3. $urlinfos = GetHostInfo($gurl); 
  4. $ghost = trim($urlinfos['host']); 
  5. if($ghost==”) 
  6. { 
  7. return FALSE; 
  8. } 
  9. $gquery = $urlinfos['query']; 
  10. if($gcookie==”" && !emptyempty($rfurl)) 
  11. { 
  12. $gcookie = RefurlCookie($rfurl); 
  13. } 
  14. $sessionQuery = “GET $gquery HTTP/1.1rn”; 
  15. $sessionQuery .= “Host: $ghostrn”; 
  16. $sessionQuery .= “Referer: $rfurlrn”; 
  17. $sessionQuery .= “Accept: */*rn”; 
  18. $sessionQuery .= “User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)rn”; 
  19. if($gcookie!=”" && !preg_match(“/[rn]/”, $gcookie)) 
  20. { 
  21. $sessionQuery .= $gcookie.”rn”; 
  22. } 
  23. $sessionQuery .= “Connection: Keep-Alivernrn”; 
  24. $errno = “”; 
  25. $errstr = “”; 
  26. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10); 
  27. fwrite($m_fp,$sessionQuery); 
  28. $lnum = 0; 
  29.  
  30. //获取应答头 
  31. $m_httphead = Array(); 
  32. $httpstas = explode(” “,fgets($m_fp,256)); 
  33. $m_httphead["http-edition"] = trim($httpstas[0]); 
  34. $m_httphead["http-state"] = trim($httpstas[1]); 
  35. while(!feof($m_fp)) 
  36. { 
  37. $line = trim(fgets($m_fp,256)); 
  38. if($line == “” || $lnum>100) 
  39. { 
  40. break; 
  41. } 
  42. $hkey = “”; 
  43. $hvalue = “”; 
  44. $v = 0; 
  45. for($i=0; $i { 
  46. if($v==1) 
  47. { 
  48. $hvalue .= $line[$i]; 
  49. } 
  50. if($line[$i]==”:”) 
  51. { 
  52. $v = 1; 
  53. } 
  54. if($v==0) 
  55. { 
  56. $hkey .= $line[$i]; 
  57. } 
  58. } 
  59. $hkey = trim($hkey); 
  60. if($hkey!=”") 
  61. { 
  62. $m_httphead[strtolower($hkey)] = trim($hvalue); 
  63. } 
  64. } 
  65.  
  66. if(preg_match(“/^3/”, $m_httphead["http-state"])) 
  67. { 
  68. if(isset($m_httphead["location"]) && $JumpCount<3) { $JumpCount++; DownImageKeep($gurl,$rfurl,$filename,$gcookie,$JumpCount); } else { return FALSE; } } if(!preg_match(“/^2/”, $m_httphead["http-state"])) { return FALSE; } if(!isset($m_httphead)) { return FALSE; } $contentLength = $m_httphead['content-length']; //保存图片 $fp = fopen($filename,”w”) or die(“写入文件:{$filename} 失败!”); $i=0; $okdata = “”; $starttime = time(); while(!feof($m_fp)) { $okdata .= fgetc($m_fp); $i++; //超时退出 if(time()-$starttime>$maxtime) 
  69. { 
  70. break; 
  71. } 
  72.  
  73. //到达指定大小结束 
  74. if($i >= $contentLength) 
  75. { 
  76. break; 
  77. } 
  78. } 
  79. if($okdata!=”") 
  80. { 
  81. fwrite($fp,$okdata); 
  82. } 
  83. fclose($fp); 
  84. if($okdata==”") 
  85. { 
  86. @unlink($filename); 
  87. fclose($m_fp); 
  88. return FALSE; 
  89. } 
  90. fclose($m_fp); 
  91. return TRUE; 
  92. } 
  93. //获得网址的host和query部份 
  94. function GetHostInfo($gurl) 
  95. { 
  96. $gurl = preg_replace(“/^http:///i”, “”, trim($gurl)); 
  97. $garr['host'] = preg_replace(“//(.*)$/i”, “”, $gurl); 
  98. $garr['query'] = “/”.preg_replace(“/^([^/]*)//i”, “”, $gurl); 
  99. return $garr; 
  100. } 
  101. //获得页面返回的Cookie信息 
  102. function RefurlCookie($gurl) 
  103. { 
  104. global $gcookie,$lastRfurl; 
  105. $gurl = trim($gurl); 
  106. if(!empty($gcookie) && $lastRfurl==$gurl) 
  107. { 
  108. return $gcookie; 
  109. } 
  110. else 
  111. { 
  112. $lastRfurl=$gurl; 
  113. } 
  114. if(trim($gurl)==”) 
  115. { 
  116. return ”; 
  117. } 
  118. $urlinfos = GetHostInfo($gurl); 
  119. $ghost = $urlinfos['host']; 
  120. $gquery = $urlinfos['query']; 
  121. $sessionQuery = “GET $gquery HTTP/1.1rn”; 
  122. $sessionQuery .= “Host: $ghostrn”; 
  123. $sessionQuery .= “Accept: */*rn”; 
  124. $sessionQuery .= “User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)rn”; 
  125. $sessionQuery .= “Connection: Closernrn”; 
  126. $errno = “”; 
  127. $errstr = “”; 
  128. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) or die($ghost.’ 
  129. ‘); 
  130. fwrite($m_fp,$sessionQuery); 
  131. $lnum = 0; 
  132.  
  133. //获取详细应答头 
  134. $gcookie = “”; 
  135. while(!feof($m_fp)) 
  136. { 
  137. $line = trim(fgets($m_fp,256)); 
  138. if($line == “” || $lnum>100) 
  139. { 
  140. break; 
  141. } 
  142. else 
  143. { 
  144. if(preg_match(“/^cookie/i”, $line)) 
  145. { 
  146. $gcookie = $line; 
  147. break; 
  148. } 
  149. } 
  150. } 
  151. fclose($m_fp); 
  152. return $gcookie; 
  153. } 

Tags: 下载远程网页图片 保存本地

分享到: