关闭

百度图片防盗链处理函数

标签: 函数百度图片url
295人阅读 评论(0) 收藏 举报
分类:
//百度图片地址处理
function img($url=''){

        if( $url ){

                $url = base64_decode($url);

                $dir = pathinfo($url);  

                $host = $dir['dirname'];  

                $refer = $host.'/';  

                $ch = curl_init($url);  

                curl_setopt ($ch, CURLOPT_REFERER, $refer);  

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//Activation can modify the page  

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  

                curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);  

                $data = curl_exec($ch);  

                curl_close($ch);  

                $ext = strtolower(substr(strrchr($url,'.'),1,10));  
                $types = array(  
                            'gif'=>'image/gif',  
                            'jpeg'=>'image/jpeg',  
                            'jpg'=>'image/jpeg',  
                            'jpe'=>'image/jpeg',  
                            'png'=>'image/png',  
                );  
                $type = $types[$ext] ? $types[$ext] : 'image/jpeg';  
                header("Content-type: ".$type);  
                echo $data;   

        }
}
//百度文章图片提取
function get_img( $str='' ){

    if( $str ){

        $preg_s = '/(src=\")(.*?)(\")/is';
        $preg_bd = '/(baidu)/is';

        if(preg_match_all($preg_s,$str,$arr) and preg_match_all($preg_bd,$str)){

            foreach($arr[2] as $y){
                if( preg_match_all( $preg_bd,$y ) ){
                    $url = base64_encode($y);
                    $url = str_replace('=','',$url);
                    $str = str_replace($y,site_url("index/img/$url"),$str);
                }

            }
        }
        return $str;
    } 
}
//调用
$r['body'] = $this->hp->get_img( $r['body'] );
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24215次
    • 积分:580
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:21篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论