TinyMCE – How to insert an image that is already on the server?
Image by Keara - hkhazo.biz.id

TinyMCE – How to insert an image that is already on the server?

Posted on

Are you tired of uploading images to your TinyMCE editor every time you want to add one to your content? Do you have a vast library of images already stored on your server, just waiting to be used? Well, you’re in luck! In this article, we’ll take you by the hand and guide you through the process of inserting an image that’s already on the server using TinyMCE. Buckle up, folks, and let’s dive in!

Prerequisites

Before we begin, make sure you have the following:

  • TinyMCE installed and configured on your website or application.
  • A server with a directory containing the images you want to use.
  • Basic knowledge of HTML and JavaScript (not required, but it helps).

Step 1: Configure TinyMCE

Open your TinyMCE configuration file (usually `tinymce.js` or `tinymce.config.js`) and add the following code:

tinymce.init({
  selector: 'textarea', // Replace with your text area selector
  plugins: 'image',
  file_picker_types: 'image',
  file_picker_callback: function(cb, value, meta) {
    var input = document.createElement('input');
    input.setAttribute('type', 'file');
    input.setAttribute('accept', 'image/*');
    input.onchange = function() {
      var file = this.files[0];
      var reader = new FileReader();
      reader.onload = function() {
        var imageDataUrl = reader.result;
        cb(imageDataUrl, {
          filename: file.name
        });
      };
      reader.readAsDataURL(file);
    };
    input.click();
  }
});

This code initializes TinyMCE, enables the image plugin, and sets up a file picker callback function that will allow us to select images from our server.

Step 2: Create a Server-Side Script

Create a new server-side script (e.g., `image_proxy.php`) that will handle image requests and serve the images from your server. Here’s an example using PHP:

<?php
  $image_path = $_GET['image_path'];
  $image_url = $_SERVER['DOCUMENT_ROOT'] . '/' . $image_path;
  if (file_exists($image_url)) {
    $image_data = file_get_contents($image_url);
    header('Content-Type: image/jpeg'); // or png, gif, etc.
    echo $image_data;
  } else {
    header('HTTP/1.0 404 Not Found');
  }
?>

This script takes an `image_path` parameter, checks if the file exists on the server, and serves the image if it does. Make sure to update the script to fit your server-side language and image type.

Step 3: Update TinyMCE Configuration

Go back to your TinyMCE configuration file and add the following code:

tinymce.init({
  // ...
  image_list: [
    {
      title: 'Server Images',
      value: 'server_images'
    }
  ],
  images_upload_handler: function(blobInfo, success, failure, progress) {
    var xhr, formData;
    xhr = new XMLHttpRequest();
    xhr.withCredentials = false;
    xhr.open('GET', 'image_proxy.php?image_path=' + blobInfo.blobUri());
    xhr.onload = function() {
      if (xhr.status !== 200) {
        failure('HTTP Error: ' + xhr.status);
        return;
      }
      var imageDataUrl = xhr.responseText;
      success(imageDataUrl);
    };
    xhr.onerror = function() {
      failure('Error during upload: ' + xhr.statusText);
    };
    xhr.send();
  }
});

This code adds a new image list option called “Server Images” and defines an `images_upload_handler` function that will make a GET request to our server-side script to retrieve the image.

Step 4: Insert the Image

Now that everything is set up, let’s insert an image into our TinyMCE editor!

Open your TinyMCE editor and click on the “Insert Image” button. In the “Image List” dropdown, select “Server Images”. You will see a text input field where you can enter the path to the image on your server (e.g., `/images/image1.jpg`).

Click “Insert” and… voilà! The image should now be inserted into your editor.

Troubleshooting

Having trouble getting it to work? Here are some common issues and solutions:

Issue Solution
Image not displaying Check if the image path is correct and the file exists on the server. Also, make sure the server-side script is correctly configured and serving the image.
HTTP Error 404 Verify that the server-side script is correctly handling the image request and that the image file exists on the server.
CORS issues Make sure to configure CORS headers on your server to allow cross-origin requests.

Conclusion

And that’s it! With these simple steps, you should now be able to insert images that are already on your server into TinyMCE. No more uploading images every time you want to add one to your content. Enjoy the convenience and flexibility of using images from your server!

If you have any questions or need further assistance, feel free to ask in the comments below. Happy coding!

Additional Resources

For more information on TinyMCE and its configuration options, check out the official TinyMCE Documentation.

Need help with server-side scripting? Refer to the official documentation for your server-side language (e.g., PHP, C#, etc.).

Stay tuned for more tutorials and guides on using TinyMCE and server-side scripting!

Frequently Asked Question

Got stuck with TinyMCE and images? Worry not, friend! We’ve got you covered. Here are some answers to your burning questions about inserting an image that’s already on the server.

How do I insert an image into TinyMCE if it’s already uploaded on my server?

Easy peasy! You can use the `image` plugin in TinyMCE and specify the URL of the image. For example, if your image is located at `http://example.com/image.jpg`, you can insert it into your content using the following code: ``. If you’re using the TinyMCE editor, you can also use the “Insert image” button and enter the URL in the “Source” field.

What if I don’t know the URL of the image on my server?

No worries! You can use the “Browse” button in the “Insert image” dialog to search for the image on your server. This will allow you to navigate through your server’s directory structure and select the image you want to insert.

Can I insert an image using a relative URL instead of an absolute one?

Yes, you can insert an image using a relative URL. For example, if your image is located in a directory called `images` in the same directory as your HTML file, you can use a relative URL like ``. This can be useful if you’re working with a content management system (CMS) or a website with a complex directory structure.

How do I specify the alt text for an image in TinyMCE?

Good question! You can specify the alt text for an image in TinyMCE by adding the `alt` attribute to the `img` tag. For example: ``. You can also use the “Insert image” dialog and enter the alt text in the “Image description” field.

Can I resize an image in TinyMCE after inserting it?

You bet! After inserting an image in TinyMCE, you can resize it by dragging the corners of the image to the desired size. You can also use the “Image” button in the toolbar and select “Image properties” to specify a custom width and height.