TinyMCE is a great online WYSIWYG editor which is used for a lot of projects, for example this Wordpress blog application (the content backend). I use TinyMCE for custom projects too. TinyMCE is free, generates good HTML code and the interface is easy to learn even for non-webmaster.
The project is open source and the developer Moxiecode earns money by selling useful plugins for image and link management. Sometimes the Image manager “looks too big” for projects, in this case the following (free) solution is an option. You can download your TinyMCE copy from the TinyMCE website.
The editor has two different modes: simple and advanced mode. In the advanced mode is it possible to load several (already included) plugins. The advanced image function has an option to select images from a list. This list is a just a little JavaScript snippet which stores file name and the alternative text notation:
var tinyMCEImageList = new Array(["Logo", "logo.jpg"]);
Using PHP we are able to generate this list dynamically. But first we need a script that uploads the images and store the data into a database. We use for our example the PHP Upload Class from finalwebsites.com. The following snippet should do that job (check the examples with the upload class for more information about the upload process). Store this code together with an upload form in some PHP script.
< ?php include_once ('upload_class.php'); $max_size = 1024*250; $error = ''; if(isset($_POST['Submit'])) { $my_upload = new file_upload; $my_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/fotos/"; $my_upload->extensions = array(".png", ".gif", ".jpg"); $my_upload->rename_file = false; $my_upload->the_temp_file = $_FILES['upload']['tmp_name']; $my_upload->the_file = $_FILES['upload']['name']; $my_upload->http_error = $_FILES['upload']['error']; $my_upload->replace = "n"; if ($my_upload->upload()) { $conn = mysql_connect("localhost", "user", "pw"); mysql_select_db("database", $conn); mysql_query(sprintf("INSERT INTO foto_table SET file_name = '%s', alt_txt = '%s'", $my_upload->file_copy), mysql_real_escape_string($_POST['alt_txt'], $conn) ); } $error = $my_upload->show_error_string(); } ?>
Next we need a script that will create the image list from the data we have stored in our database:
< ?php $conn = mysql_connect("localhost", "user", "pw"); mysql_select_db("database", $conn); $dir = '/fotos/'; $sql = "SELECT * FROM foto_table"; $res = mysql_query($sql); $js = 'var tinyMCEImageList = new Array( // Name, URL'; while ($obj = mysql_fetch_object($res)) { $js .= ' ["'.$obj->alt_txt.'", "'.$dir.$obj->file_name.'"],'; } $js = rtrim($js, ',').');'; echo $js; ?>
Now we are able to use this dynamic list in our TinyMCE WYSIWYG editor. The following JavaScript snippet shows a very basic configuration, The script has many options and maybe you have already your own configuration in your content management system.
<script type="text/javascript"><!--mce:0--></script>
The most important part are the declarations for the plugin and the image list. With these settings you should notice the dialog box:

As you see the alternative name is used in this list and also for other attributes within the image element.
This example is a quick and free alternative if you don’t like to buy the commercial image manger. You can download the example files here.
you should check out the filebrowser callback:
file_browser_callback : ‘yourJsFileBrowser’,
I used it in a project:
file_browser_callback : ‘pixelFileBrowser’,
function pixelFileBrowser (field_name, url, type, win) {
var picker_url;
if (type==”file”) {
picker_url = ‘/admin/pages/js_list’;
} else {
picker_url = ‘/admin/files/browse’ + “?files_filter_by=” + type;
}
tinyMCE.openWindow({
file : picker_url,
title : “File Browser”,
width : 600,
height : 400,
close_previous : “no”
}, {
window : win,
input : field_name,
resizable : “yes”,
inline : “yes”, // This parameter only has an effect if you use the inlinepopups plugin!
editor_id : tinyMCE.getWindowArg(”editor_id”)
});
return false;
}
Thanks dude!
I didn’t new I could create a select image list in TinyMCE. I’ve made a CakePhp function that grabs my images from a resource folder.
The trick also works for TinyMCE Media plugin. You should only change the array name to “tinyMCEMediaList” and fill it with .swf, .cdr, .avi, .mov or other supported media files:
Here is the API:
http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media
Yes right, you can create this list for images, links, media and templates.
I use the link list in a current project. I created a list of pdf’s and pages (created with the CMS).
Hi Olaf,
I enjoyed your tutorial, but I am having trouble implementing the process. I am not sure which folder should the images.php script should be placed in b/c when I use TinyMCE and press and push the “image” button TinyMCE takes me immediately to the the default external image page:
../tiny_mce/plugins/advimage/image.htm
rather than
/tiny_mce/plugins/advimage/image.php
my javascript for tinymce looks like
tinyMCE.init( {
plugins : “advimage”,
external_image_list_url : “image.php5?x=”,
mode:”textareas”,
theme: “advanced”
});
I’d really appreciate any help you could provide…
Hi,
the list shows up inside the regular image dialog, if you have trouble with the pathes, just place the list file in the same folder where your form is located.
(if you have further questions please continue on our forum)
@kenrick,
maybe you can explain your comment a little more?