้็ฏๆ็ซ ๆไธๆญฅๆญฅๅฏฆไฝ CKEditor ๅจ CodeIgniter ไธญๅ็ไปฅๅๆชๆกไธๅณๅฐ server ็ๅ่ฝ,้ฆๅ
ๆๅๅๅพ CKEditor ๅฎ็ถฒไธ่ผๆๆฐ็็ๆฌ,้ๅ demo ไฝฟ็จ็ๆฏ 4.6,ๅญๆชๅพๆๆๆพๅจ assets/ckeditor/ ไธ,ๆๆไฝฟ็จ Sybio/ImageWorkshop ้ๅๅฅไปถๅฐๅ็ๅ็ฌฌไธๆฌก็ๅฃ็ธฎ,ๆๅ้ๅธธไธๅธๆ user ๅญ่ถ
ๅคง็ๆฑ่ฅฟๅจๆๅ้ๅฆ,็ข็ซไธๆฏ็ธ็ฐฟ,ๅๅฐๅฏไปฅ้ ๅฉ้กฏ็คบ็บๆบๅ。
assets/ckeditor/config.js
CKEDITOR.editorConfig = function( config ) {
config.toolbar =
[
{name:'document', items:['Source','-','Preview','Print']},
{name:'clipboard', items:['Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo']},
{name:'basicstyles', items:['Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat']},
'/',
{name:'paragraph', items:['NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl']},
{name:'links', items:['Link','Unlink']},
{name:'insert', items:['Image','Table']},
{name:'tools', items:['Maximize']}
];
config.enterMode = CKEDITOR.ENTER_BR;
config.filebrowserImageUploadUrl = baseUrl + 'ckeditor/image_upload';
config.filebrowserUploadUrl = baseUrl + 'ckeditor/file_upload';
};
้ๆฏๆๅธธ็จ็ config,ๅทฅๅ ทๅฏไปฅ่ช่กๅขๆธ。
application/views/ckeditor.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CKEditor Upload Demo</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="assets/ckeditor/ckeditor.js"></script>
<script src="assets/ckeditor/adapters/jquery.js"></script>
<script type="text/javascript" charset="utf-8">
var baseUrl = "<?php echo base_url(); ?>";
window.CKEDITOR_BASEPATH = "<?php echo base_url('assets/ckeditor'); ?>";
$(function() {
$('.ckeditor').ckeditor();
});
</script>
</head>
<body>
<?php echo form_open_multipart('ckeditor/upload'); ?>
<?php echo form_textarea(['name' => 'content', 'class' => 'ckeditor']); ?>
<?php echo form_submit('submit', 'ไธๅณ'); ?>
<?php echo form_close(); ?>
</body>
</html>
็ฐกๅฎ็่จญๅฎไธไธ ckeditor ๅฟ ่ฆ็็ฐๅข。
application/controllers/Ckeditor.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
use PHPImageWorkshop\ImageWorkshop;
class Ckeditor extends CI_Controller
{
/**
* Upload path.
*
* @var string
*/
private $path = 'assets/uploads/ckeditor/';
/**
* Index page.
*/
public function index()
{
$this->load->helper('form');
$this->load->helper('url');
$this->load->view('ckeditor');
}
/**
* CKEditor image upload
*
* Upload by ImageWorkshop package
*
* @return string
*/
public function image_upload()
{
$this->load->helper('url');
$funcNum = $this->input->get('CKEditorFuncNum');
$source = $_FILES['upload']['tmp_name'];
$file = $_FILES['upload']['name'];
$ext = pathinfo($file, PATHINFO_EXTENSION);
$ratio = 1200; // ๅฏฌๅบฆๆๅคงๆฏไพ
$allows = [1, 2, 3]; // GIF, JPEG, PNG
$newName = date('YmdHis').rand(1000, 9999); //ๆฐๆชๅ
$message = '';
$url = '';
try {
$exif = exif_imagetype($source);
if (!$exif) {
throw new Exception('ๆชๆกๆ ผๅผ้ฏ่ชค');
}
if (!in_array($exif, $allows)) {
throw new Exception('ๅ
ๅ
่จฑ gif, jpeg, png');
}
$imageInfo = getimagesize($source);
$layer = ImageWorkshop::initFromPath($source);
// ่ฅๅฏฌๅบฆๅคงๆผๆฏไพ,็ธฎๆพ็ฝฎๆฏไพ
if ($imageInfo[0] > $ratio) {
$layer->resizeInPixel($ratio, null, true);
}
$fileName = "{$newName}.{$ext}";
$layer->save($this->path, $fileName);
$url = base_url($this->path.$fileName);
} catch (Exception $e) {
$message = $e->getMessage();
}
$string = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction({$funcNum}, '{$url}', '{$message}');</script>";
$this->output
->set_content_type('text/html')
->set_output($string);
}
/**
* CKEditor image upload
*
* @return string
*/
public function file_upload()
{
$this->load->helper('url');
$funcNum = $this->input->get('CKEditorFuncNum');
$source = $_FILES['upload']['tmp_name'];
$file = $_FILES['upload']['name'];
$ext = pathinfo($file, PATHINFO_EXTENSION);
$newName = date('YmdHis').rand(1000, 9999); //ๆฐๆชๅ
$message = '';
$url = '';
try {
$fileName = "{$newName}.{$ext}";
$target = $this->path.$fileName;
if (move_uploaded_file($source, $target)) {
$url = base_url($target);
} else {
$message = 'ไธๅณๅคฑๆ';
}
} catch (Exception $e) {
$message = $e->getMessage();
}
$string = "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction({$funcNum}, '{$url}', '{$message}');</script>";
$this->output
->set_content_type('text/html')
->set_output($string);
}
}
ๆๅๅปบ็ซไบ Ckeditor ็ controller,ๆ form ๆพๅจ้ฆ้ ,้ๅ้จไปฝๅ ๅซไบๅ็่ทๆชๆกไธๅณ็็จๅผ็ขผ,ๅ็้จไปฝไนๅ ๅซไบๆชๆกๆ ผๅผๆชขๆฅ、ๆชๆกๅฐบๅฏธๆชขๆฅ,ๅฆๆ้้่ฆๆชขๆฅๆดๅค็ๆฑ่ฅฟๅฐฑๅจ try catch ่ฃก้ขๅๅ 。