logo

JTBC5.0实现编辑器粘贴图片自动上传

2025-02-19 点击 78

支持自动上传的图片:

1.电脑图片复制后(单张,多张),在编辑器粘贴

2.截图软件截图后,在编辑器粘贴

3.聊天软件中图片复制后(单张),在编辑器粘贴

4.办公软件中图片复制后(单张) ,在编辑器粘贴

不支持办公软件多张图片复制粘贴自动上传。

不支持网站上的图片复制后粘贴自动上传,这属于远程图片本地化功能。


实现方法:

1.修改文件\Public\common\assets\js\components\jtbc\jtbcEditor\jtbcEditor.js

V5.0.1.4及以后版本,修改文件位置:Public\common\assets\js\components\jtbc\jtbcFieldEditor\jtbcFieldEditor.js

添加一行

language:'zh-Hans',
images_upload_url: '/upload.php',//这一行是新添加的
setup: function(editor)

2.上传文件upload.php

上传位置:\Public\

直接拷贝以下代码自行新建upload.php文件

备注:本代码来自tinymce官方文件后经本站修改,原文件网址:httPS://www.tiny.cloud/docs/tinymce/6/php-upload-handler/

<?php
  /***************************************************
   * Only these origins are allowed to upload images *
   ***************************************************/
  $accepted_origins = array("http://localhost", "http://192.168.1.1", "http://127.0.0.1", "http://git.hosts.run", "http://hosts.run");
  $origins_switch = true;//默认开启域名检测,只允许以上域名调用本上传文件进行上传
  /*********************************************
   * Change this line to set the upload folder *
   *********************************************/

  if (isset($_SERVER['HTTP_ORIGIN']) && $origins_switch) {
    // same-origin requests won't set an origin. If the origin is set, it must be valid.
    if (in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)) {
      header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
    } else {
      header("HTTP/1.1 403 Origin Denied");
      return;
    }
  }

  // Don't attempt to process the upload on an OPTIONS request
  if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    header("Access-Control-Allow-Methods: POST, OPTIONS");
    return;
  }

  reset ($_FILES);
  $temp = current($_FILES);
  if (is_uploaded_file($temp['tmp_name'])){
    /*
      If your script needs to receive cookies, set images_upload_credentials : true in
      the configuration and enable the following two headers.
    */
    // header('Access-Control-Allow-Credentials: true');
    // header('P3P: CP="There is no P3P policy."');

    // Sanitize input文件名含中文不通过
    //if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])) {
    //    header("HTTP/1.1 400 Invalid file name.");
    //    return;
    //}

    // Verify extension
    if (!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) {
        header("HTTP/1.1 400 Invalid extension.");
        return;
    }

    // Accept upload if there was no origin, or if it is an accepted origin
    $imageFolder = 'upload/'.date('Y').'/'.date('m').'/';
    if (!is_dir($imageFolder))
    {
      mkdir($imageFolder, 0755,true);
      chmod($imageFolder, 0755);
    }
    $filetowrite = $imageFolder . mt_rand(100,999).$temp['name'];
    move_uploaded_file($temp['tmp_name'], $filetowrite);

    // Determine the base URL
    $protocol = '//';//isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https://" : "http://";
    $baseurl = $_SERVER["HTTP_HOST"] . rtrim(dirname($_SERVER['REQUEST_URI']), "/") . '/';
    $baseurl = str_ireplace("\\","/",$baseurl);
    $baseurl = str_ireplace("//","/",$baseurl);
    // Respond to the successful upload with JSON.
    // Use a location key to specify the path to the saved image resource.
    // { location : '/your/uploaded/image/file'}
    echo json_encode(array('location' => $protocol . $baseurl .$filetowrite));
  } else {
    // Notify editor that the upload failed
    header("HTTP/1.1 500 Server Error");
  }
?>



0%