xiebruce / PicUploader

一个还不错的图床工具,支持Mac/Win/Linux服务器、支持压缩后上传、添加图片或文字水印、多文件同时上传、同时上传到多个云、右击任意文件上传、快捷键上传剪贴板截图、Web版上传、支持作为Mweb/Typora发布图片接口、作为PicGo/ShareX/uPic等的自定义图床,支持在服务器上部署作为图床接口,支持上传任意格式文件。
https://www.xiebruce.top/17.html
MIT License
1.19k stars 169 forks source link

请问作者此项目能不能和ueditor配合使用直接对接到WEB后台 #49

Open sw586 opened 4 years ago

sw586 commented 4 years ago

ueditor通过PicUploader上传图片到github

xiebruce commented 4 years ago

可以的,但需要修改ueditor的上传代码,我下载个ueditor测试一下吧,然后更新一下文档你再看就行。

sw586 commented 4 years ago

太好了 真想亲你一口

sw586 commented 4 years ago

或者应该怎么联系上您,如果在自己搞不定情况下 能不能付费请您处理,我的小站是用的ueditor,很想结合使用本项目

xiebruce commented 4 years ago

@sw586 你先试试能不能解决,如果需要我帮忙直接在这里回复就行,解决后给你个打赏码。

sw586 commented 4 years ago

好的,您能大概写思路么?就是假如本地有个ueditor和PicUploader两个站点,应该怎么来调用PicUploader。实现在ueditor上传图片到github

xiebruce commented 4 years ago

ueditor和picuploader在同一台机器上时,可通过命令来调用PicUploader上传:

/path/to/php /path/to/PicUploader/index.php --type=typora /path/to/test.jpg

你需要在ueditor的php上传方法里修改一下,用php的exec()去执行上边的命令就行,它会返回一个上传后的链接(纯链接,非markdown格式)。


当ueditor与PicUploader不在同一台机器上时,就需要使用curl来post上传这个文件,PicUploader那边就是常规的php接收文件方式,即用$_FILES来接收上传文件的,你需要组装curl上传参数,让PicUploader能接收到这个$_FILES['ueditor']这个变量的值就行了(注意无需修改PicUploader,要修改的是ueditor的这个文件ueditor-utf8-php/php/Uploader.class.php)。

sw586 commented 4 years ago

遗憾,,没搞定,能搞个demo么

sw586 commented 4 years ago

ueditor和picuploader在同一台机器上时,需要在同一个站点下吗

xiebruce commented 4 years ago

不需要在同一站点下的。

uploader.class.php中找到这个方法private function upFile(),在这个方法最后有一个if else,注释掉else里原来的那句,添加一句调用自定义方法$this->uploadByPicUploader();

//移动文件
if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败
    $this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");
} else { //移动成功
    // 注释掉
    // $this->stateInfo = $this->stateMap[0];
    // 添加
    $this->uploadByPicUploader();
}

这是自定义方法,你粘贴到uploader.class.php文件里(可以放在upFile()方法后面),注意要把$phpPath$picUploaderPath两个变量的值换成你服务器里的(用绝对路径)

/**
 * 自定义上传函数(调用PicUploader上传)
 */
private function uploadByPicUploader($type='local'){
    if($type == 'local'){
        $phpPath = '/usr/local/opt/php@7.3/bin/php';
        $picUploaderPath = '/Users/bruce/www/personal/PicUploader/index.php';
        $option = '--type=typora';
        $cmd = $phpPath . ' ' . $picUploaderPath . ' ' . $option . ' ' . $this->filePath;
        exec($cmd, $output);
        if(isset($output[0])){
            $this->fullName = $output[0];
            $this->stateInfo = $this->stateMap[0];
        }
    }else{
        //处理ueditor与PicUploader不在同一台机器上的问题
    }
}

另外要注意php.ini中的disable_functions不要有exec,否则这函数会被被禁用,也就无法执行。

sw586 commented 4 years ago

太牛了 居然成功了!!

sw586 commented 4 years ago

大佬帮看一下下面这种上传方法应该怎么来修改应用:

    /**
     * 文件上传
     */
    public function upload() {

        // 验证上传权限
        $this->_check_upload_auth();
        $p = $this->_get_upload_params();
        $rt = \Phpcmf\Service::L('upload')->upload_file([
            'path' => '',
            'form_name' => 'file_data',
            'file_exts' => @explode(',', $p['exts']),
            'file_size' => (int)$p['size'] * 1024 * 1024,
            'attachment' => \Phpcmf\Service::M('Attachment')->get_attach_info((int)$p['attachment'], (int)$p['image_reduce']),
        ]);
        if (!$rt['code']) {
            exit(dr_array2string($rt));
        }

        // 附件归档
        $data = \Phpcmf\Service::M('Attachment')->save_data($rt['data']);
        if (!$data['code']) {
            exit(dr_array2string($data));
        }

        // 上传成功
        if (IS_API_HTTP) {
            $data['data'] = [
                'id' => $data['code'],
                'url' => $rt['data']['url'],
            ];
            exit(dr_array2string($data));
        } else {
            exit(dr_array2string(['code' => 1, 'msg' => dr_lang('上传成功'), 'id' => $data['code'], 'info' => $rt['data']]));
        }

    }

完整文件:[https://github.com/sw586/uploadfile/blob/master/File.php] @xiebruce

xiebruce commented 4 years ago

现在才看到,你这代码发上来全乱了,你直接把你二次开发过的ueditor压缩包发我邮箱吧:xiediebruce@hotmail.com,另外,感谢vx打赏!

sw586 commented 4 years ago

感谢大佬!已经打包发到您邮箱,最近不景气。。打赏莫嫌少就好!!我会再次打赏您的,不会白占用您的时间,感谢您这么热心的回复及帮助!

xiebruce commented 4 years ago

研究了一下,这个比之前直接用的原版ueditor麻烦,主要是研究过程麻烦,我得安装你的cms,又得研究二次开发后的ueditor上传图片逻辑。

private function upFile()方法的最后几行注释掉并添加一行调用自定义函数

// $this->fileUrl = $this->attachment_info['url'].$this->fullName;
// $this->stateInfo = $this->stateMap[0];

// 存储附件
// $this->save_attach($rt);
//添加这行(调用自定义函数)
$this->uploadByPicUploader($rt);

这是自定义函数(注意要把$phpPath和$picUploaderPath两个变量的值换成你服务器里的(用绝对路径))

/**
 * 自定义上传函数(调用PicUploader上传)
 */
private function uploadByPicUploader($rt, $type='local'){
    $filePath = ROOTPATH . 'uploadfile/' . $this->fullName;
    if($type == 'local'){
        $phpPath = '/usr/local/opt/php@7.3/bin/php';
        $picUploaderPath = '/Users/bruce/www/personal/PicUploader/index.php';
        $option = '--type=typora';
        $cmd = $phpPath . ' ' . $picUploaderPath . ' ' . $option . ' ' . $filePath;
        exec($cmd, $output);
        if(isset($output[0])){
            $this->fileUrl = $output[0];
            $this->stateInfo = $this->stateMap[0];
            // 存储附件
            $this->save_attach($rt);
        }
    }else{
        //处理ueditor与PicUploader不在同一台机器上的问题
    }
}

另外要注意php.ini中的disable_functions不要有exec,否则这函数会被被禁用,也就无法执行。

sw586 commented 4 years ago

多谢搞定了

sw586 commented 4 years ago

/**

} 大佬能付幫看下這個上傳方法應該怎麽改造

------------------ 原始邮件 ------------------ 发件人: "xiebruce/PicUploader" <notifications@github.com>; 发送时间: 2020年7月12日(星期天) 凌晨3:00 收件人: "xiebruce/PicUploader"<PicUploader@noreply.github.com>; 抄送: "sw586"<sw586@126.com>;"Mention"<mention@noreply.github.com>; 主题: Re: [xiebruce/PicUploader] 请问作者此项目能不能和ueditor配合使用直接对接到WEB后台 (#49)

研究了一下,这个比之前直接用的原版ueditor麻烦,主要是研究过程麻烦,我得安装你的cms,又得研究二次开发后的ueditor上传图片逻辑。

把private function upFile()方法的最后几行注释掉并添加一行调用自定义函数 // $this->fileUrl = $this->attachment_info['url'].$this->fullName; // $this->stateInfo = $this->stateMap[0]; // 存储附件 // $this->save_attach($rt); //添加这行(调用自定义函数) $this->uploadByPicUploader($rt);

这是自定义函数 /* 自定义上传函数(调用PicUploader上传) */ private function uploadByPicUploader($rt, $type='local'){ $filePath = ROOTPATH . 'uploadfile/' . $this->fullName; if($type == 'local'){ $phpPath = '/usr/local/opt/php@7.3/bin/php'; $picUploaderPath = '/Users/bruce/www/personal/PicUploader/index.php'; $option = '--type=typora'; $cmd = $phpPath . ' ' . $picUploaderPath . ' ' . $option . ' ' . $filePath; exec($cmd, $output); if(isset($output[0])){ $this->fileUrl = $output[0]; $this->stateInfo = $this->stateMap[0]; // 存储附件 $this->save_attach($rt); } }else{ //处理ueditor与PicUploader不在同一台机器上的问题 } }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

xiebruce commented 4 years ago

不客气,也感谢再次打赏!

xiebruce commented 4 years ago

改造?你要改造以实现什么功能吗?

helenpayne commented 4 years ago

就是把上面的public function upload() 也能类似ueditor那么调用PicUploader上传,另外问一下PicUploader有办法上传文件夹么

xiebruce commented 4 years ago

这个upload()的功能体现在页面哪里(也就是页面哪个地方上传文件是调用upload的)?

上传文件夹理论上是可以的,不过有点麻烦,我看什么时候研究一下。

helenpayne commented 4 years ago

是这种一般的单文件上传按钮,就在发给您的包里 TIM截图20200712171829

sw586 commented 4 years ago

是这种一般的单文件上传按钮,就在发给您的包里 TIM截图20200712171829

大佬撤回上面的咨询, 我自己搞定了,感谢你耐心解答!!祝大佬生活愉快 早日实现人生巅峰!!!

xiebruce commented 4 years ago

dayrui/Core/Controllers/Api/File.php文件中找到upload()函数,在// 附件归档前面添加一行调用自定义函数的代码

//添加这行(调用自定义函数)
$rt = $this->uploadByPicUploader($rt);

// 附件归档
$data = \Phpcmf\Service::M('Attachment')->save_data($rt['data']);
if (!$data['code']) {
    exit(dr_array2string($data));
}

这是自定义函数,把它放到dayrui/Core/Controllers/Api/File.php文件中

/**
 * 自定义上传函数(调用PicUploader上传)
 * @param        $rt
 * @param string $type
 *
 * @return mixed|string
 */
public function uploadByPicUploader($rt, $type='local'){
    $fileUrl = '';
    $filePath = $rt['data']['path'];
    if($type == 'local'){
        $phpPath = '/usr/local/opt/php@7.3/bin/php';
        $picUploaderPath = '/Users/bruce/www/personal/PicUploader/index.php';
        $option = '--type=typora';
        $cmd = $phpPath . ' ' . $picUploaderPath . ' ' . $option . ' ' . $filePath;
        exec($cmd, $output);
        if(isset($output[0])){
            $fileUrl = $output[0];
        }
    }else{
        //处理ueditor与PicUploader不在同一台机器上的问题
    }
    if(preg_match('/http[s]?:\/\/(.*?)$/', $fileUrl)){
        $rt['data']['url'] = $fileUrl;
        $rt['data']['preview'] = '<a href="javascript:dr_preview_image(\'' . $fileUrl . '\');"><img src="' . $fileUrl . '"></a>';
    }
    return $rt;
}
helenpayne commented 4 years ago

太好了!谢谢

xiebruce commented 4 years ago

不客气