欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ThinkPHP5+UEditor图片上传到阿里云对象存储OSS功能示例

程序员文章站 2023-09-08 09:34:51
本文实例讲述了thinkphp5+ueditor图片上传到阿里云对象存储oss。分享给大家供大家参考,具体如下: thinkphp5使用富文本ueditor,将富文本编辑...

本文实例讲述了thinkphp5+ueditor图片上传到阿里云对象存储oss。分享给大家供大家参考,具体如下:

thinkphp5使用富文本ueditor,将富文本编辑框内上传在本地的图片,修改到阿里云对象存储oss

thinkphp5加载ueditor ···· 略

ueditor下载:

(或本站下载:)

阿里云对象存储sdk下载:

一、配置项

ueditor目录:\public\static\admin\lib\ueditor\1.4.3
oss配置文件目录:\application\config\oos.php
oss sdk目录:\extend\oos

二、代码

1、oss配置文件

<?php
return [
  'endpoint' => 'xxxx',
  'accesskeyid' => 'xxxxxxxxxxx',
  'accesskeysecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
  'bucket' => 'xxxxx',
  'uploadurl' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //个人配置用,上传图片访问头部完整链接
];

2、在ueditor下写入oos.class.php控制器

*注:本人用的是相对路径,请自行对照自己的目录结构替换掉文件引入地址

<?php
require_once realpath(dirname(__file__) . '/../../../../../../../') . '/extend/oos/autoload.php';
use oss\ossclient;
use oss\core\ossexception;
class oos
{
  protected $oos = null;
  protected $bucket = null;
  //获取oos客户端
  protected function getossclient(){
    if($this->oos === null){
      $config = require realpath(dirname(__file__) . '/../../../../../../../') .'/application/config/oos.php';
      $this->bucket = $config['bucket'];
      try {
        $this->oos = new ossclient($config['accesskeyid'], $config['accesskeysecret'], $config['endpoint'], false);
      } catch (ossexception $e) {
        printf(__function__ . "creating ossclient instance: failed\n");
        printf($e->getmessage() . "\n");
        return null;
      }
    }
    return $this->oos;
  }
  //上传
  public function upload($file,$save){
    $config = require realpath(dirname(__file__) . '/../../../../../../../') .'/application/config/oos.php';
    $save = 'upload/'.$save;
    $ossclient = $this->getossclient();
    if (is_null($ossclient)) exit('链接存储失败');
    $result = $ossclient->uploadfile($this->bucket, $save, $file);
    return !empty($result['x-oss-request-id']);
  }
}

3、修改ueditor 上传图片的php文件\public\static\admin\lib\ueditor\1.4.3\php\action_crawler.php

<?php
/**
 * 抓取远程图片
 * user: jinqn
 * date: 14-04-14
 * time: 下午19:18
 */
set_time_limit(0);
include("uploader.class.php");
include("oos.class.php");
// 引入oss对象
$oos_config = require realpath(dirname(__file__) . '/../../../../../../../') .'/application/config/oos.php';
$oos = new oos();
/* 上传配置 */
$config = array(
  "pathformat" => $config['catcherpathformat'],
  "maxsize" => $config['catchermaxsize'],
  "allowfiles" => $config['catcherallowfiles'],
  "oriname" => "remote.png"
);
$fieldname = $config['catcherfieldname'];
/* 抓取远程图片 */
$list = array();
if (isset($_post[$fieldname])) {
  $source = $_post[$fieldname];
} else {
  $source = $_get[$fieldname];
}
foreach ($source as $imgurl) {
  $item = new uploader($imgurl, $config, "remote");
  $info = $item->getfileinfo();
  $year = date('ymd',time());//图片路径 (年/月) 自己设置
  $img_name = time().rand(1,1000).$info['type'];
  $bos_url = "ueditor_upload/xinjieshi/image/$year/$img_name";//用作保存的图片路径和名字
  $oos->upload($_server['document_root'].'/'.$info['url'],$bos_url);
  array_push($list, array(
    "state" => $info["state"],
    "url" => $oos_config['uploadurl'].$bos_url,
    "size" => $info["size"],
    "title" => htmlspecialchars($info["title"]),
    "original" => htmlspecialchars($info["original"]),
    "source" => htmlspecialchars($imgurl)
  ));
}
/* 返回抓取数据 */
return json_encode(array(
  'state'=> count($list) ? 'success':'error',
  'list'=> $list
));