bwaindwain
bwaindwain

Reputation: 60

CKEditor image2 change initial size

When uploading an image with image2 plugin, the height & width are initially set to the image's dimensions. Some of our users are uploading massive images and click OK to insert it onto the page without first choosing a reasonable size. The image fills the entire editor and they can't see what they're doing.

How can I set the initial size to something like 300px wide?

We're using CKEditor 4.11.1 with image2 plugin.

I was able to achieve this by hacking plugins/image2/dialog/image2.js and adding this to onChangeSrc() around line 148:

 // Limit initial size
 if (width > editor.config.image_initial_width) {
   height = Math.round( editor.config.image_initial_width * ( height / width ) )
   width = editor.config.image_initial_width;
 }
 if (height > editor.config.image_initial_height) {
   width = Math.round( editor.config.image_initial_height * ( width / height) );
   height = editor.config.image_initial_height;
 }

and then defining config.image_initial_height=300 and config.image_initial_width=300.

But how can I achieve this without hacking?

Upvotes: 1

Views: 1317

Answers (1)

bwaindwain
bwaindwain

Reputation: 60

Here's what worked for me.

Replace the image2 onChange event with our own, but keep a reference and call it.

Need to define two variables in config:

    config.ckeditor_image2_initial_width = 300;
    config.ckeditor_image2_initial_height = 300;
jQuery(document).ready(function() {
  if (typeof CKEDITOR !== 'undefined') {
    CKEDITOR.on('dialogDefinition', function(e) {
      if (e.data.name == 'image2') {
        var infoTab = e.data.definition.getContents('info');
        var src_field = infoTab.elements[0].children[0].children[0];
        var widthfield = infoTab.elements[2].children[0];
        var height_field = infoTab.elements[2].children[1];
        var src_was_changed = 0;

        // Add a change function to the height field.
        height_field.onChange = heightChanged;

        // We need to add a change event to the src field but it already has
        // one from image2 plugin. Replace it with our own but keep a reference
        // and call it with CKEditor tools.
        // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools.html#method-override
        src_field.onChange = CKEDITOR.tools.override(src_field.onChange, function(original) {
          return function() {
            // Call the original image2 onChangeSrc() function.
            original.call(this);
            var dialog = this.getDialog();
            var widget_image_src = 0;
            if (e.editor.widgets.selected.length) {
              widget_image_src = e.editor.widgets.selected[0].data.src;
            }
            // Fire only when image src is set and has actually changed.
            if (this.getValue() && (this.getValue() !== widget_image_src)) {
              var initial_width = e.editor.config.ckeditor_image2_initial_width;
              var initial_height = e.editor.config.ckeditor_image2_initial_height;
              if (typeof initial_width !== 'undefined' || typeof initial_height !== 'undefined') {
                // Set a flag to be used in heightChanged().
                src_was_changed = 1;
              }
            }
          }
        });

        // Change event for the image height field.
        function heightChanged() {
          if (src_was_changed) {
            src_was_changed = 0;
            var dialog = this.getDialog();
            var initial_width = e.editor.config.ckeditor_image2_initial_width;
            var initial_height = e.editor.config.ckeditor_image2_initial_height;
            var width_field = dialog.getContentElement('info', 'width');
            var height_field = dialog.getContentElement('info', 'height');
            var new_width = orig_width = width_field.getValue();
            var new_height = orig_height = height_field.getValue();
            if (new_height > initial_height) {
              new_height = initial_height;
              new_width = Math.round(orig_width * (initial_height / orig_height));
            }
            if (new_width > initial_width) {
              new_width = initial_width;
              new_height = Math.round(orig_height * (initial_width / orig_width));
            }
            width_field.setValue(new_width);
            height_field.setValue(new_height);
          }
        }
      }
    });
  }
});

Upvotes: 1

Related Questions