php 封存远程图片到本地

WBOY
풀어 주다: 2016-06-13 13:08:40
원래의
797명이 탐색했습니다.

php 保存远程图片到本地

显示远程图片:


?
header('Content-Type:image/jpg');    
echo file_get_contents("http://www.baidu.com/img/baidu_logo.gif");    
?>  
로그인 후 복사

?

第一种: 精确型

?

<?php     
    
// 变量说明:     
// $url 是远程图片的完整URL地址,不能为空。    
// $filename 是可选变量: 如果为空,本地文件名将基于时间和日期     
// 自动生成.     
    
function get_photo($url,$filename='',$savefile='test/')   
{     
    $imgArr = array('gif','bmp','png','ico','jpg','jepg');  
  
    if(!$url) return false;  
    
    if(!$filename) {     
      $ext=strtolower(end(explode('.',$url)));     
      if(!in_array($ext,$imgArr)) return false;  
      $filename=date("dMYHis").'.'.$ext;     
    }     
  
    if(!is_dir($savefile)) mkdir($savefile, 0777);  
    if(!is_readable($savefile)) chmod($savefile, 0777);  
      
    $filename = $savefile.$filename;  
  
    ob_start();     
    readfile($url);     
    $img = ob_get_contents();     
    ob_end_clean();     
    $size = strlen($img);     
    
    $fp2=@fopen($filename, "a");     
    fwrite($fp2,$img);     
    fclose($fp2);     
    
    return $filename;     
 }     
   
  
 $img=get_photo("http://www.baidu.com/img/baidu_logo.gif");     
 echo $img ? '<pre class="brush:php;toolbar:false"><img  src="'.$img.'" alt=" php 封存远程图片到本地 " >
로그인 후 복사
' : "false";

?

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

function getImg($str){  
    $str = stripslashes($str);  
    $pattern = "/<img [^ alt=" php 封存远程图片到本地 " >]*src\=\"(([^>]*)(jpg|gif|png|bmp|jpeg))\"/i";   //获取所有图片标签的全部信息  
    preg_match_all($pattern, $str, $matches);  
        
    return $matches[1];   //$matches[1]中就是所想匹配的结果,结果为数组  
}  
  
    $str = <<<EOT  
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.  
  
Must Read  
SonySony Playstation Phone Rumors Heat Up  
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  
<img    style="max-width:90%"  style="max-width:90%" alt="" src="http://img.ibtimes.com/www/site/us/images/1px.gif" sized="yes">  
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.  
<img alt="Sony" src="http://img.ibtimes.com/www/thumb/mainpage/13463-12079-sony.jpg" sized="yes">  
This article was originally published on Benzinga, and is republished here with permission.   
EOT;  
  
  
foreach( getImg($str) as $url)  
{  
    get_photo($url);  
}  
로그인 후 복사
?
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!