laravel框架ueditor上传图片到阿里云oss

首先下载ueditor文件编辑器      http://ueditor.baidu.com/website/download.html  
将下载的ueditor解压后放到laravel框架下的public文件中
我放在了这

下面是具体内容操作和修改
第一步新建文件:OssInUe.php   (位置:public/vendor/laravel-u-editor/php/)

<?php
if (is_file('../../../../vendor/autoload.php')) {
    require_once  '../../../../vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;

/**
 * Created by PhpStorm.
 * User: crjy
 * Date: 2017/10/13
 * Time: 15:46
 */
class OssInUe{
    public function __construct(){

    }
    function uploadToAliOSS($file,$fileType){
        $entension = $fileType; //上传文件的后缀
        $newName = date('YmdHis').mt_rand(100000,999999).$entension;//上传到oss的文件名   文件重命名
        $accessKeyId = '阿里云id';
        $accessKeySecret ='阿里云key';
        $endpoint = '你的端点';//域名,端点名
        $bucket= '你的bucket名';//" <您使用的Bucket名字,注意命名规范>";
        $object = '上传的路径'.$newName;//" <您使用的Object名字,注意命名规范>";         (可以写到.env文件中调用)
        $content = $file["tmp_name"];//上传的文件
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint,false);
            $ossClient->setTimeout(3600); /* seconds */
            $ossClient->setConnectTimeout(10); /* seconds */
//            $ossClient->putObject($bucket, $object, $content);
//             先把本地的example.jpg上传到指定$bucket, 命名为$object
            $ossClient->uploadFile($bucket, $object, $content);
            $signedUrl = $ossClient->signUrl($bucket, $object);
            $path = explode('?',$signedUrl)[0];
            $obj['status'] = true;
            $obj['path'] = $path;
        } catch (OssException $e) {
            $obj['status'] = false;
            $obj['path'] = "";
            print $e->getMessage();
        }
        return $obj;
    }
}


第二步修改文件:Uploader.class.php    (位置:public/vendor/laravel-u-editor/php/)

<?php
require_once  __DIR__.'/OssInUe.php';
/**
 * Created by JetBrains PhpStorm.
 * User: taoqili
 * Date: 12-7-18
 * Time: 上午11: 32
 * UEditor编辑器通用上传类
 */
class Uploader
{
   
    /**
     * 构造函数
     * @param string $fileField 表单名称
     * @param array $config 配置项
     * @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名
     */
    public function __construct($fileField, $config, $type = "upload")
    {

             /* 
                  代码太多删了
              */
        
    }
    /**
     * 上传文件的主处理方法
     * @return mixed
     */
    private function upFile()
    {
        /* 
             代码太多删了
         */
        
        //检查文件大小是否超出限制
        if (!$this->checkSize()) {
            $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
            return;
        }

        //检查是否不允许的文件格式
        if (!$this->checkType()) {
            $this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
            return;
        }

        /*支持阿里云OSS修改:第一处,start*/
        //创建目录失败
        /*if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
            $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
            return;
        } else if (!is_writeable($dirname)) {
            $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
            return;
        }*/

        //移动文件
        /*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];
        }*/
        $ossInUe = new OssInUe();
        $obj = $ossInUe->uploadToAliOSS($file,$this->fileType);
        if ($obj['status'] == true){
            $this->fullName = $obj['path'];
            $this->stateInfo = $this->stateMap[0];
        }else{
            $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
        }
        /*支持阿里云OSS修改:第一处,end*/
        
    }

    /**
     * 处理base64编码的图片上传
     * @return mixed
     */
    private function upBase64()
    {
        /* 
           代码太多删了
        */
        
        //检查文件大小是否超出限制
        if (!$this->checkSize()) {
            $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
            return;
        }
        /*支持阿里云OSS修改:第二处,start*/
        /*//创建目录失败
        if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
            $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
            return;
        } else if (!is_writeable($dirname)) {
            $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
            return;
        }*/

        //移动文件
        /*if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败
            $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
        } else { //移动成功
            $this->stateInfo = $this->stateMap[0];
        }*/
        $ossInUe = new OssInUe();
        $obj = $ossInUe->uploadToAliOSS($img,$this->fileType);
        if ($obj['status'] == true){
            $this->fullName = $obj['path'];
            $this->stateInfo = $this->stateMap[0];
        }else{
            $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
        }
        /*支持阿里云OSS修改:第二处,end*/
        
    }

    /**
     * 拉取远程图片
     * @return mixed
     */
    private function saveRemote()
    {
        /* 
             代码太多删了
        */
        
        //打开输出缓冲区并获取远程图片
        ob_start();
        $context = stream_context_create(
            array('http' => array(
                'follow_location' => false // don't follow redirects
            ))
        );
        readfile($imgUrl, false, $context);
        $img = ob_get_contents();
        ob_end_clean();
        preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);

        $this->oriName = $m ? $m[1]:"";
        $this->fileSize = strlen($img);
        $this->fileType = $this->getFileExt();
        $this->fullName = $this->getFullName();
        $this->filePath = $this->getFilePath();
        $this->fileName = $this->getFileName();
        $dirname = dirname($this->filePath);

        //检查文件大小是否超出限制
        if (!$this->checkSize()) {
            $this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
            return;
        }

        /*支持阿里云OSS修改:第三处,start*/
        /* //创建目录失败
         if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
             $this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
             return;
         } else if (!is_writeable($dirname)) {
             $this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
             return;
         }

         //移动文件
         if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败
             $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
         } else { //移动成功
             $this->stateInfo = $this->stateMap[0];
         }*/
        $ossInUe = new OssInUe();
        $obj = $ossInUe->uploadToAliOSS($img,$this->fileType);
        if ($obj['status'] == true){
            $this->fullName = $obj['path'];
            $this->stateInfo = $this->stateMap[0];
        }else{
            $this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");
        }
        /*支持阿里云OSS修改:第三处,start*/
    }

    /**
     * 上传错误检查
     * @param $errCode
     * @return string
     */
    private function getStateInfo($errCode)
    {
        return !$this->stateMap[$errCode] ? $this->stateMap["ERROR_UNKNOWN"] : $this->stateMap[$errCode];
    }

    /**
     * 获取文件扩展名
     * @return string
     */
    private function getFileExt()
    {
        return strtolower(strrchr($this->oriName, '.'));
    }


    /* 
          后面代码太多没有修改   删了
      */
    

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值