php下保存远程图片到本地的函数

<?php
header("content-type:text/html;charset=utf-8");
function GrabImage($url,$filename="") {  
if($url=="") return false;  
 
if($filename=="") {  
$ext=strrchr($url,".");  
###指定远程下载图片的后缀,当然可以加以拓展
if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false; $filename=date("YmdHis").$ext; } //www.bcty365.com 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=GrabImage("http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg","logo.gif"); ### 其中得到本地名称logo.gif,可以指定目录 if($img){ echo '<img src="'.$img.'">'; }else{ echo "false"; } ?>
原文地址:https://www.cnblogs.com/wuheng1991/p/5174649.html