php 保存远程图片到本地

来源:互联网 发布:英语动画配音软件 编辑:程序博客网 时间:2024/06/05 06:19

http://justcoding.iteye.com/blog/856425#bc2358473

http://www.521admin.com/bbs/thread-92-1.html

php 保存远程图片到本地

    博客分类:
  • Php / Pear / Mysql / Node.js
PHPEXTUP 

显示远程图片:

 

Php代码  收藏代码
  1. <?php    
  2. header('Content-Type:image/jpg');    
  3. echo file_get_contents("http://www.baidu.com/img/baidu_logo.gif");    
  4. ?>  
 

 

第一种: 精确型

 

Php代码  收藏代码
  1. <?php     
  2.     
  3. // 变量说明:     
  4. // $url 是远程图片的完整URL地址,不能为空。    
  5. // $filename 是可选变量: 如果为空,本地文件名将基于时间和日期     
  6. // 自动生成.     
  7.     
  8. function get_photo($url,$filename='',$savefile='test/')   
  9. {     
  10.     $imgArr = array('gif','bmp','png','ico','jpg','jepg');  
  11.   
  12.     if(!$urlreturn false;  
  13.     
  14.     if(!$filename) {     
  15.       $ext=strtolower(end(explode('.',$url)));     
  16.       if(!in_array($ext,$imgArr)) return false;  
  17.       $filename=date("dMYHis").'.'.$ext;     
  18.     }     
  19.   
  20.     if(!is_dir($savefile)) mkdir($savefile, 0777);  
  21.     if(!is_readable($savefile)) chmod($savefile, 0777);  
  22.       
  23.     $filename = $savefile.$filename;  
  24.   
  25.     ob_start();     
  26.     readfile($url);     
  27.     $img = ob_get_contents();     
  28.     ob_end_clean();     
  29.     $size = strlen($img);     
  30.     
  31.     $fp2=@fopen($filename"a");     
  32.     fwrite($fp2,$img);     
  33.     fclose($fp2);     
  34.     
  35.     return $filename;     
  36.  }     
  37.    
  38.   
  39.  $img=get_photo("http://www.baidu.com/img/baidu_logo.gif");     
  40.  echo $img ? '<pre><img src="'.$img.'"></pre>' : "false";  
 

第二种:从文章中提取图片,并保存至本地

 

Php代码  收藏代码
  1. function getImg($str){  
  2.     $str = stripslashes($str);  
  3.     $pattern = "/<img[^>]*src\=\"(([^>]*)(jpg|gif|png|bmp|jpeg))\"/i";   //获取所有图片标签的全部信息  
  4.     preg_match_all($pattern$str$matches);  
  5.         
  6.     return $matches[1];   //$matches[1]中就是所想匹配的结果,结果为数组  
  7. }  
  8.   
  9.     $str = <<<EOT  
  10. Money has been moving into Brazilian stocks over the last couple of days, despite mostly flat trading activity in the U.S. equity markets. During Thursday's session, the iShares MSCI Brazil Index ETF (NYSE: EWZ) has risen 1.12% after outperforming the U.S. indices yesterday as well.  
  11.   
  12. Must Read  
  13. SonySony Playstation Phone Rumors Heat Up  
  14. A South Korean Army soldier walks up steps of a guard post near the demilitarised zone separating the two Koreas.S. Korea waves olive branch  
  15. <img width="1" height="1" alt="" src="http://img.ibtimes.com/www/site/us/images/1px.gif" sized="yes">  
  16. Two giant resource companies make up a big chunk of the Brazilian Bovespa. They are PetroBras (NYSE: PBR), with a market cap of $163.44 billion, and miner Vale (NYSE: VALE), which has a market cap of $178.95 billion. PBR shares have risen 1.47% thus far today and VALE is trading 0.77% higher at $34.03.  
  17. <img alt="Sony" src="http://img.ibtimes.com/www/thumb/mainpage/13463-12079-sony.jpg" sized="yes">  
  18. This article was originally published on Benzinga, and is republished here with permission.   
  19. EOT;  
  20.   
  21.   
  22. foreach( getImg($stras $url)  
  23. {  
  24.     get_photo($url);  



php下保存远程图片到本地的办法

  1. function GrabImage($url,$filename="") {
  2. if($url=="") return false;

  3. if($filename=="") {
  4. $ext=strrchr($url,".");
  5. if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false;
  6. $filename=date("YmdHis").$ext;
  7. }

  8. ob_start();
  9. readfile($url);
  10. $img = ob_get_contents();
  11. ob_end_clean();
  12. $size = strlen($img);

  13. $fp2=@fopen($filename, "a");
  14. fwrite($fp2,$img);
  15. fclose($fp2);

  16. return $filename;
  17. }

获取一张图片的代码:
  1. $img=GrabImage("http://www.baidu.com/img/baidu_logo.gif","logo.gif");
  2. if($img){
  3. echo '<img src="'.$img.'">';
  4. }else{
  5. echo "false";
  6. }


这是保存google的logo的例子,获取到的图片保存在同级目录下面。


获取一系列的有规律的图片(例如:以数字1-100命名的100张图片):
  1. for ($i=1;$i<=100;$i++){
  2. $img=GrabImage("http://www.yourimagesite.com/images/$i.gif","images/$i.gif");
  3. }




 
0 0
原创粉丝点击