I want to allow registered users to insert (i.e. upload) images into a Redactor rich text field in a front-end form.
I dowloaded the Image Manager plugin from Imperavi (https://imperavi.com/redactor/plugins/imagemanager/) and set up my Redactor config as follows:
{% includeJsResource "lib/redactor/redactor.js" %}
{% includeJsResource "lib/redactor/plugins/source.js" %}
{% includeJsResource "lib/redactor/plugins/fullscreen.js" %}
{% includeJsResource "lib/redactor/plugins/imagemanager.js" %}
{% includeCssResource "lib/redactor/redactor.css" %}
{% set redactorJS %}
$('#{{ elId }}').redactor({
buttons: ['format', 'bold', 'italic', 'deleted', 'lists', 'link', 'html', 'image'],
formatting: ['p', 'blockquote', 'h2', 'h3'],
plugins: ['source','fullscreen', 'imagemanager'],
minHeight: 300,
imageUpload: '/upload.php',
});
{% endset %}
{% includeJs redactorJS %}
I can see the image button in the toolbar and the browse files modal when I click the image button. However, it doesn’t upload anything because imageUpload: '/upload.php',
is obviously incorrect. What, if anything, can I replace upload.php
with? If this isn’t possible, what else could I do to achieve this functionality? Thanks!
UPDATE:
I have the following code in craft/plugins/imagesuploaderscript/controllers/ImagesUploaderScriptController.php
from CloudApp share link in comment below:
namespace Craft;
class ImagesUploaderScriptController extends BaseController {
protected $valid_extensions = array('jpg', 'jpeg', 'png');
protected $assetSourceId = 1;
protected $assetFolderId = 1;
public function actionUpload()
{
$this->requireAjaxRequest();
$errors = array();
$success = array();
if (!$_FILES['file']['error'])
{
$filename = $_FILES['file']['name'];
$file = $_FILES['file']['tmp_name'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($extension, $this->valid_extensions))
{
$errors[] = "$filename has an invalid extension.";
} else {
$uploadDir = craft()->assetSources->getSourceById($this->assetSourceId)->settings['path'];
if (move_uploaded_file($file, $uploadDir . $filename))
{
IOHelper::deleteFile($file);
$file = $uploadDir . $filename;
$fileModel = new AssetFileModel();
$fileModel->sourceId = $this->assetSourceId;
$fileModel->folderId = $this->assetFolderId;
$fileModel->filename = IOHelper::getFileName($filename);
$fileModel->kind = IOHelper::getFileKind(IOHelper::getExtension($filename));
$fileModel->size = filesize($file);
$fileModel->dateModified = IOHelper::getLastTimeModified($file);
craft()->assets->storeFile($fileModel);
// print_r($fileModel);
// displaying file
$image = array(
'url' => 'images/'.$filename,
'id' => 1
);
$this->returnJSON($image);
exit;
}
else
{
$errors[] = "$filename was unable to be saved.";
}
}
}
$this->returnJSON(compact('errors'));
exit;
}
}
How do I get my form to talk to it? Also, what else do I need to do to get it to work?
craft/app/resources/js/craft/redactor.js::10511
you could search for an existing one or create your own. All in all you have to send a request to the AssetController with the file and the parent folder and return the img tag. I've done it with quilljs.com but not with the redactor. If you want I can show you the default code