发帖时远程图片本地化教程

魔王  (UID: 1) 11月前 [复制链接]
帖子链接已复制到剪贴板

808 0

在hook文件夹新建一个

model_thread_create_end.php

<?php
    $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)
    创建新帖