Home Backend Development PHP Tutorial Yii combines CKEditor to implement image upload function_PHP tutorial

Yii combines CKEditor to implement image upload function_PHP tutorial

Jul 13, 2016 am 10:28 AM
ckeditor yii upload picture

In a project I have been working on these days, I need to implement the image upload function in the WYSIWYG editor. I chose CKEditor because I prefer its interface. Although there is CKFinder that works well with CKEditor, its function is too complicated. After briefly looking at the documentation of CKEditor, I found that this function can be implemented by myself without resorting to CKFinder.

Although the following code is based on Yii Framework, the idea is exactly the same when using other frameworks or languages. Children's shoes in need can be referred to.

First of all, to enable the image upload function in CkEditor, you need to configure the filebrowserImageUploadUrl attribute of the editor:

Copy the code The code is as follows:

CKEDITOR.replace( 'editor1',
{
filebrowserUploadUrl : '/uploader/upload.php',
filebrowserImageUploadUrl : '/uploader/upload.php?type=Images'
} );

Then implement the image upload function on the corresponding URL and return the HTML code in a specific format to CKEditor, so that CKEditor can preview and insert the image normally.
Only part of the code of the controller is intercepted below. I implemented the Controller part like this:
Copy the code The code is as follows:

/**
* Save the uploaded image
*
* @return string javascript code
* @author lfyzjck
**/
public function actionImg($type, $CKEditor, $CKEditorFuncNum, $langCode = 'zh-cn')
{
if(empty($CKEditorFuncNum) | | $type != 'Images'){
$this->mkhtml($CKEditorFuncNum,'','Wrong function call');
}
if(isset($_FILES['upload '])){
//Get the image upload configuration
$options = Options::model()->findByPk(1);
$form = new UploadForm('image',$options );
$form->upload = CUploadedFile::getInstanceByName('upload');
if($form->validate()){
//File name: time + source file name
$target_filename = date('Ymd-hm',time()).$form->upload->getName();
$path = Yii::app()->basePath.' /../uploads/'.$target_filename; //Picture save path
$form->upload->saveAs($path);
$this->mkhtml($CKEditorFuncNum,Yii:: app()->baseUrl.'/uploads/'.$target_filename, "Upload successful");
}
else{
$this->mkhtml($CKEditorFuncNum,'',$form ->getError('upload'));
}
}
}
/**
* Return the prompt message of CKEditor
*
* @return void
* @author lfyzjck
**/
private function mkhtml($fn, $fileurl, $message)
{
$str = '';
exit($str);
}

The mkhtml function that needs special explanation will call the CKEditor function to generate prompt information. When the upload is successful, the image link will be returned, and CKEditor will generate an image preview based on the URL.

Then comes the UploadForm code, which will verify whether the format and size of the image meet the requirements.

Copy code The code is as follows:

class UploadForm extends CFormModel
{
public $upload;

private $options;
private $type;

public function __construct($type, $options){
$this->options = $options;
$this->type = $type;
}
/**
  * Declares the validation rules.
  * The rules state that username and password are required,
  * and password needs to be authenticated.
 */
public function rules()
{
return array(
array('upload', 'file',
'types' => $this-> options->getAttribute("allow_".$this->type."_type"),
'maxSize' => 1024 * (int)$this->options->getAttribute("allow_" .$this->type."_maxsize"),
'tooLarge'=>'File size exceeds limit',
),
);
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/802217.htmlTechArticleIn a project I have been working on these days, I need to implement the image upload function in the WYSIWYG editor. I chose CKEditor because I prefer its interface. Although it works well with CKEditor...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Steps to implement image uploading and display using CakePHP framework Steps to implement image uploading and display using CakePHP framework Jul 29, 2023 pm 04:21 PM

Steps to implement image upload and display using CakePHP framework Introduction: In modern web applications, image upload and display are common functional requirements. The CakePHP framework provides developers with powerful functions and convenient tools, making it simple and efficient to upload and display images. This article will introduce you to how to use the CakePHP framework to upload and display images. Step 1: Create a file upload form First, we need to create a form in the view file for users to upload images. The following is an example of

Create a rich text editor using PHP and CKEditor Create a rich text editor using PHP and CKEditor May 11, 2023 pm 04:06 PM

With the widespread use of web applications, creating rich text editors has become more and more common. CKEditor is widely recognized as one of the best rich text editors because of its good customizability and ease of use. This article will introduce how to create a rich text editor using PHP and CKEditor. Introduction to CKEditor CKEditor is an open source, cross-platform rich text editor implemented through JavaScript. It provides an intuitive and easy-to-understand toolbar, including font style, formatting, graphics, etc.

WeChat applet implements image upload function WeChat applet implements image upload function Nov 21, 2023 am 09:08 AM

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

How to use PHP and Vue to implement image upload function How to use PHP and Vue to implement image upload function Sep 25, 2023 pm 03:17 PM

How to use PHP and Vue to implement the image upload function. In modern web development, the image upload function is a very common requirement. This article will introduce in detail how to use PHP and Vue to implement the image upload function, and provide specific code examples. 1. Front-end part (Vue) First, you need to create a form for uploading images on the front-end. The specific code is as follows:<template><div><inputtype="fil

How to handle image uploading and compression in Vue technology development How to handle image uploading and compression in Vue technology development Oct 08, 2023 am 10:58 AM

How to handle image uploading and compression in Vue technology development In modern web applications, image uploading is a very common requirement. However, due to network transmission and storage reasons, directly uploading original high-resolution images may result in slow upload speeds and a large waste of storage space. Therefore, uploading and compressing images is very important. In Vue technology development, we can use some ready-made solutions to handle image uploading and compression. The following will introduce how to use vue-upload-comone

Problems encountered in image uploading and cropping when using Vue development Problems encountered in image uploading and cropping when using Vue development Oct 08, 2023 pm 04:12 PM

Title: Image uploading and cropping problems and solutions in Vue development Introduction: In Vue development, image uploading and cropping are common requirements. This article will introduce the image uploading and cropping problems encountered in Vue development, and give solutions and specific code examples. 1. Image upload problem: Selecting the image upload button cannot trigger the file selection box: This problem is usually because the event is not bound correctly or the bound event does not take effect. You can bind the click event in the template and trigger the file selection box in the corresponding method. Code example:

How to use ThinkPHP6 to upload images How to use ThinkPHP6 to upload images Jun 20, 2023 pm 09:25 PM

With the development of the Internet, image uploading has become an essential feature in website and application development. In the field of PHP, ThinkPHP6 has become a very popular development framework. In this article, we will introduce how to use ThinkPHP6 to implement image upload. 1. Create project and controller First, we need to create a new ThinkPHP6 project. You can use Composer to install it, or you can download the latest version from the official website. After the installation is complete, enter in the console

How to use Yii3 framework in php? How to use Yii3 framework in php? May 31, 2023 pm 10:42 PM

As the Internet continues to develop, the demand for web application development is also getting higher and higher. For developers, developing applications requires a stable, efficient, and powerful framework, which can improve development efficiency. Yii is a leading high-performance PHP framework that provides rich features and good performance. Yii3 is the next generation version of the Yii framework, which further optimizes performance and code quality based on Yii2. In this article, we will introduce how to use Yii3 framework to develop PHP applications.

See all articles