Tony网络图片本地化插件

羊倌 2018-10-25 3451

获取:


核心代码:

$letImageLocal = kv_get('letImageLocal');if($letImageLocal['enable']){    $user = user_read($uid);    user_login_check();    $post = post__read($pid);//再次重新读出来    $preg=preg_match_all('/<img.*?src="(.*?)"/',stripslashes($post['message']),$matches);    if($matches && $preg){        //1表示无<img src=.../>标签        foreach($matches[1] as $image_url){            if(empty($image_url)) continue;            if(strpos($image_url,'http') === 0){//是http开头                $site_url = http_url_path();                $pos=strpos($image_url,$site_url);                if($pos===false){//判断是不是本站的图片,不是本站随即采集                    $data = null;                    if(function_exists('curl_init')){                        //可能有https的图片                        $ch = curl_init();                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);                        curl_setopt($ch, CURLOPT_HEADER, false);                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);                        curl_setopt($ch, CURLOPT_URL, $image_url);                        curl_setopt($ch, CURLOPT_REFERER, $image_url);                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);                        $data = curl_exec($ch);                        curl_close($ch);                    }else{                        $data = file_get_contents($image_url);//下载图片                    }                    if(!empty($data)){                        //检查上传权限                        empty($group['allowattach']) AND $gid != 1 AND message(-1, '您无权上传');                        empty($data) AND message(-1, lang('data_is_empty'));                        $size = strlen($data);                        $size > 20480000 AND message(-1, lang('filesize_too_large', array('maxsize'=>'20M', 'size'=>$size)));                        //解析图片                        $img_data = getimagesize('data://image/jpeg;base64,'. base64_encode($data));                        //Array ( [0] => 539 [1] => 316 [2] => 3 [3] => width="539" height="316" [bits] => 8 [mime] => image/png )                        $width = $img_data[0];                        $height = $img_data[1];                        //获取上传目录                        $attach_dir_save_rule = array_value($conf, 'attach_dir_save_rule', 'Ym');                        $day = date($attach_dir_save_rule, time());                        $path = $conf['upload_path'].'attach/'.$day;                        $url = $conf['upload_url'].'attach/'.$day;                        !is_dir($path) AND mkdir($path, 0777, TRUE);                        //按规则生成图片名称并上传                        $ext = file_ext(file_name($image_url));                        if(empty($ext)||strlen($ext) != 3)$ext='png';//给个默认值                        $filetypes = include APP_PATH.'conf/attach.conf.php';                        !in_array($ext, $filetypes['all']) AND $ext = '_'.$ext;                        $filename = $uid.'_'.xn_rand(15).'.'.$ext;                        $destfile = $path.'/'.$filename;                        $desturl = $url.'/'.$filename;                        $filetype = attach_type($filename, $filetypes);                        file_put_contents($destfile, $data) OR message(-1, lang('write_to_file_failed'));                        //开始写入数据库,包含3张表thread,post,attach                        $arr_attach = array(                            'tid'=>$tid,                            'pid'=>$pid,                            'uid'=>$uid,                            'filesize'=>$size,                            'width'=>$img_data[0],                            'height'=>$img_data[1],                            'filename'=>"$day/$filename",                            'orgfilename'=>'image.png',                            'filetype'=>$filetype,                            'create_date'=>time(),                            'comment'=>'',                            'downloads'=>0,                            'isimage'=>1                        );                        attach_create($arr_attach);                        //替换原来的图片路径                        $post['message'] = str_replace($image_url, $desturl,$post['message']);                    }                }            }        }        //更新主题和内容图片数量+1        thread__update($tid,array('images'=>count($matches[1])));        post_message_fmt($post, $gid);//用内置的html格式化函数        post__update($pid,array('images'=>count($matches[1]),'message'=>$post['message'],'message_fmt'=>$post['message_fmt']));    }}



支付宝领红包
为了方便大家交流,以及分享优惠券信息,尝试建了个群,欢迎加入。 京东优惠券
最新回复 (0)
返回
发新帖

关注公众号

访问手机版

360网站安全检测平台