Home > Backend Development > PHP Tutorial > php collect remote pictures

php collect remote pictures

高洛峰
Release: 2016-11-29 16:03:41
Original
1009 people have browsed it

变量说明:$url 是远程图片的完整url地址,不能为空,$filename 是可选变量,如果为空,本地文件名将基于时间和日期. 
自动生成,代码如下:
function grabimage($url,$filename="") {  
  if($url==""):return false;endif; 
  if($filename=="") {  
    $ext=strrchr($url,".");  
    if($ext!=".gif" && $ext!=".jpg"):return false;endif;  
    $filename=date("dmyhis").$ext;  
  } 
  ob_start();  
  readfile($url);  
  $img = ob_get_contents();  
  ob_end_clean(); //开源代码phpfensi.com 
  $size = strlen($img); 
  $fp2=@fopen($filename, "a");  
  fwrite($fp2,$img);  
  fclose($fp2); 
  return $filename;  
} 
如果可用采集防采集的功能把fopen改成如下:
$ch = curl_init(); 
curl_setopt($ch, curlopt_url, $url);//这是目标地址 
curl_setopt($ch, curlopt_header, 0); 
curl_setopt($ch, curlopt_returntransfer, 1); 
curl_setopt($ch, curlopt_referer,   $referer);//这里伪造 
$data = curl_exec($ch); 
curl_close($ch); 
echo $data;
Copy after login


Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template