php读远程图片和PHP保存远程图片到本地


php读远程图片

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

PHP保存远程图片到本地

方法一:
print_r stream_get_wrappers();
查看是否wrapper http,若包含http,则:
copy("http://www.baidu.com/img/baidu_logo.gif","baidu.jpg");
即可。
方法二:
$img=file_get_contents(" http://www.baidu.com/img/baidu_logo.gif");
亦可。

============================================

另附:
<?php   

//   
// Function: 获取远程图片并把它保存到本地
//   
//   
//     确定您有把文件写入本地服务器的权限   
//     
// 变量说明:   
// $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();   
$size = strlen($img);   

$fp2=@fopen($filename, "a");   
fwrite($fp2,$img);   
fclose($fp2);   

return $filename;   
 


$img=GrabImage("http://www.phpzy.com/login.gif","");   
if($img):echo '<pre><img src="'.$img.'"></pre>';else:echo "false";endif;       




?>

dedecms中的:                 

if(!emptyempty($saveremoteimg))
{
$body = stripslashes($body);
$img_array = array();
preg_match_all("/(src|SRC)=["|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array);
$img_array = array_unique($img_array[2]);
set_time_limit(0);
$imgUrl = $img_dir."/".strftime("%Y%m%d",time());
$imgPath = $base_dir.$imgUrl;
$milliSecond = strftime("%H%M%S",time());
if(!is_dir($imgPath)) @mkdir($imgPath,0777);
foreach($img_array as $key =>$value)
{
$value = trim($value);
$get_file = @file_get_contents($value);
$rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);
$fileurl = $imgUrl."/".$milliSecond.$key.".".substr($value,-3,3);
if($get_file)
{
$fp = @fopen($rndFileName,"w");
@fwrite($fp,$get_file);
@fclose($fp);
}
$body = ereg_replace($value,$fileurl,$body);
}
$body = addslashes($body);
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值