Home Backend Development PHP Tutorial php 封存远程图片到本地

php 封存远程图片到本地

Jun 13, 2016 pm 01:08 PM
img the url

php 保存远程图片到本地

显示远程图片:


?
header('Content-Type:image/jpg');    
echo file_get_contents("http://www.baidu.com/img/baidu_logo.gif");    
?>  
Copy after login

?

第一种: 精确型

?

<?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="/static/imghw/default1.png"  data-src="'.$img.'"  class="lazy".$img.'" alt=" php 封存远程图片到本地 " >
Copy after login
' : "false";

?

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

function getImg($str){  
    $str = stripslashes($str);  
    $pattern = "/<img  src="/static/imghw/default1.png"  data-src="http://img.ibtimes.com/www/site/us/images/1px.gif"  class="lazy" [^ 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 src="/static/imghw/default1.png"  data-src="http://img.ibtimes.com/www/site/us/images/1px.gif"  class="lazy"     style="max-width:90%"  style="max-width:90%" alt="" 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 src="/static/imghw/default1.png"  data-src="http://img.ibtimes.com/www/thumb/mainpage/13463-12079-sony.jpg"  class="lazy"  alt="Sony" sized="yes">  
This article was originally published on Benzinga, and is republished here with permission.   
EOT;  
  
  
foreach( getImg($str) as $url)  
{  
    get_photo($url);  
}  
Copy after login
?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to open img file How to open img file Sep 18, 2023 am 09:40 AM

How to open img file

How to open img file How to open img file Jul 06, 2023 pm 04:17 PM

How to open img file

PHP function introduction—get_headers(): Get the response header information of the URL PHP function introduction—get_headers(): Get the response header information of the URL Jul 25, 2023 am 09:05 AM

PHP function introduction—get_headers(): Get the response header information of the URL

How to get your Steam ID in a few steps? How to get your Steam ID in a few steps? May 08, 2023 pm 11:43 PM

How to get your Steam ID in a few steps?

Why NameResolutionError(self.host, self, e) from e and how to solve it Why NameResolutionError(self.host, self, e) from e and how to solve it Mar 01, 2024 pm 01:20 PM

Why NameResolutionError(self.host, self, e) from e and how to solve it

How to use URL encoding and decoding in Java How to use URL encoding and decoding in Java May 08, 2023 pm 05:46 PM

How to use URL encoding and decoding in Java

After 2 months, the humanoid robot Walker S can fold clothes After 2 months, the humanoid robot Walker S can fold clothes Apr 03, 2024 am 08:01 AM

After 2 months, the humanoid robot Walker S can fold clothes

What is the format of img? What is the format of img? Mar 17, 2023 am 10:33 AM

What is the format of img?

See all articles