php 获取远程图片

<?php
//采集图片
// 判断目录是否存在 不存在的话则创建

function make_dir($path){ 
    if(!file_exists($path)){//不存在则建立 
        $mk=@mkdir($path,0777); //权限 
        @chmod($path,0777); 
    } 
    return true; 
}  
//函数read_filetext()取得图片内容。使用fopen打开图片文件,然后fread读取图片文件内容。

function read_filetext($filepath){ 
    $filepath=trim($filepath); 
    $htmlfp=@fopen($filepath,"r"); 
    //远程 
    if(strstr($filepath,"://")){ 
        while($data=@fread($htmlfp,500000)){ 
            $string.=$data; 
        } 
    } 
    //本地 
    else{ 
        $string=@fread($htmlfp,@filesize($filepath)); 
    } 
    @fclose($htmlfp); 
    return $string; 
} 
//函数write_filetext()写文件,将图片内容fputs写入文件中,即保存图片文件。
function write_filetext($filepath,$string){ 
    //$string=stripSlashes($string); 
    $fp=@fopen($filepath,"w"); 
    @fputs($fp,$string); 
    @fclose($fp); 
} 
//函数get_filename()获取图片名称,也可以自定义要保存的文件名。
function get_filename($filepath){ 
    $fr=explode("/",$filepath); 
    $count=count($fr)-1; 
    return $fr[$count]; 
} 
//组合函数
function save_pic($url,$savepath=''){ 
    //处理地址 
    $url=trim($url); 
    $url=str_replace(" ","%20",$url); 
    //读文件 
    $string=read_filetext($url); 
    if(empty($string)){ 
        echo '读取不了文件';exit; 
    } 
    //文件名 
    $filename = get_filename($url); 
    //存放目录 
    make_dir($savepath); //建立存放目录 
    //文件地址 
    $filepath = $savepath.$filename; 
    //写文件 
    write_filetext($filepath,$string); 
    return $filepath; 
} 

$pic = "http://xxxx.com/img/sns/emotion/0.gif"; 
//保存目录 
$savepath = "q/"; 

//echo save_pic($pic,$savepath); 
for ($i=1;$i<=97;$i++){
	
//	$pic = "http://xxxx.com/img/sns/emotion/$i.gif"; 
	//echo save_pic($pic,$savepath); 
	echo "<br>";
	
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chaojie2009

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值