MyBB Community Forums

Full Version: Editor Image Sizing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is just a check for information regarding the fact that linking a URL as an image only resizes if you give both the height and width?

A forum I look after now, used to use 1.8.15 and we have got up to speed with 1.8.37 but have lost the ability to input a height ONLY and the width auto re-sizes when using a URL.

I have done a search or 2 but found nothing I can use to fix this .... as I have been asked if I can fix it!

So I guess the question is:  Is this a know issue which has a fix already or do I need to find the code and adapt it to accept only a width (and then it automatically re-sizes the image height)?
There is a class in postbit for 'scaleimages' that automatically adjusts the width to 100% within the post_body class. You could edit those dimensions within that class to suit your needs (that would resize after post) but I'm unsure about auto-resize within SCEditor itself.

Have a look through here and might be able to find what you're looking for...
https://www.sceditor.com/integration/
Thanks Matty but I already have all images (except the background image) set to a max of 800px wide by auto height.

I wanted to know that the old forum editor did allow only putting in a width (which it then automatically adjusted the height) so I picked version 1.8.21 and uploaded the jquery.sceditor.bbcode.min.js file.

And it did!
Image used was 320 x 227

1st is original
2nd only had the width of 160 entered
3rd width of 80
4th width of 40

[attachment=46746]

and they post

[attachment=46747]

The function for the 'image dropdown' in 1.8.37 is:

image: {
        _dropDown: function(t, e, n, r) {
          var o = Ee("div");
          Me(o, pt("image", {
            url: t._("URL:"),
            width: t._("Width (optional):"),
            height: t._("Height (optional):"),
            insert: t._("Insert")
          },
          !0));
          var i = Re(o, "#image")[0];
          i.value = n,
          Oe(o, "click", ".button",
          function(e) {
            i.value && r(i.value, Re(o, "#width")[0].value, Re(o, "#height")[0].value),
            t.closeDropDown(!0),
            e.preventDefault()
          }),
          t.createDropDown(e, "insertimage", o)
        },
        exec: function(e) {
          var o = this;
          mt.image._dropDown(o, e, "",
          function(e, t, n) {
            var r = "";
            t && (r += ' width="' + parseInt(t, 10) + '"'),
            n && (r += ' height="' + parseInt(n, 10) + '"'),
            r += ' src="' + ft(e) + '"',
            o.wysiwygEditorInsertHtml("<img" + r + " />")
          })
        },
        tooltip: "Insert an image"
      },


and for 1.8.21 is:

       image: {
          _dropDown: function(t, e, n, o) {
            var r = De("div");
            Re(r, ht("image", {
              url: t._("URL:"),
              width: t._("Width (optional):"),
              height: t._("Height (optional):"),
              insert: t._("Insert")
            },
            !0));
            var i = He(r, "#image")[0];
            i.value = n,
            Fe(r, "click", ".button",
            function(e) {
              i.value && o(i.value, He(r, "#width")[0].value, He(r, "#height")[0].value),
              t.closeDropDown(!0),
              e.preventDefault()
            }),
            t.createDropDown(e, "insertimage", r)
          },
          exec: function(e) {
            var r = this;
            mt.image._dropDown(r, e, "",
            function(e, t, n) {
              var o = "";
              t && (o += ' width="' + t + '"'),
              n && (o += ' height="' + n + '"'),
              r.wysiwygEditorInsertHtml("<img" + o + ' src="' + e + '" />')
            })
          },
          tooltip: "Insert an image"
        },


I am sure that there is much more in the file which I would need to find and I don't want to use the 1.8.21 version of the file .... but it is a start
So I have it working.

After going through all the sceditor JS files .... I realized that I only needed to change the BBcode_sceditor script!

So I put back all the 1.8.37 files and then changed that one file from line 594 through 690 (but I only actually removed some few parts .... which makes me concerned about doing the same to the actual forum).

I find no errors but have I created security holes?

The parts removed I shall make Red in the code block .... OK can't do that so will need to post the whole code and make the removed parts RED!

~~~~~~~

// Update image command to support MyBB syntax
$.sceditor.formats.bbcode.set('img', {
format: function (element, content) {
if ($(element).data('sceditor-emoticon'))
return content;

var url = $(element).attr('src'),
width = $(element).attr('width'),
height = $(element).attr('height'),
align = $(element).data('scealign');

var attrs = width !== undefined && height !== undefined && width > 0 && height > 0
? '=' + width + 'x' + height
: ''
;

if (align === 'left' || align === 'right')
attrs += ' align='+align

return '[img' + attrs + ']' + url + '[/img]';
},
html: function (token, attrs, content) {
var width, height, match,
align = attrs.align,
attribs = '';


if (attrs.defaultattr) {
match = attrs.defaultattr.split(/x/i);

width  = match[0];
height = (match.length === 2 ? match[1] : match[0]);

if (width !== undefined && height !== undefined && width > 0 && height > 0) {
attribs +=
' width="' + $.sceditor.escapeEntities(width, true) + '"' +
' height="' + $.sceditor.escapeEntities(height, true) + '"';
}
}

if (align === 'left' || align === 'right')
attribs += ' style="float: ' + align + '" data-scealign="' + align + '"';

return '<img' + attribs +
' src="' + $.sceditor.escapeUriScheme(content) + '" />';
}
})

$.sceditor.command.set('image', {
_dropDown: function (editor, caller) {
var $content;

$content = $(
'<div>' +
'<div>' +
'<label for="image">' + editor._('URL') + ':</label> ' +
'<input type="text" id="image" placeholder="https://" />' +
'</div>' +
'<div>' +
'<label for="width">' + editor._('Width (optional)') + ':</label> ' +
'<input type="text" id="width" size="2" />' +
'</div>' +
'<div>' +
'<label for="height">' + editor._('Height (optional)') + ':</label> ' +
'<input type="text" id="height" size="2" />' +
'</div>' +
'<div>' +
'<input type="button" class="button" value="' + editor._('Insert') + '" />' +
'</div>' +
'</div>'
);

$content.find('.button').on('click', function (e) {
var url = $content.find('#image').val(),
width = $content.find('#width').val(),
height = $content.find('#height').val()
;

var attrs = width !== undefined && height !== undefined && width > 0 && height > 0
? '=' + width + 'x' + height
: ''
;

if (url)
editor.insert('[img' + attrs + ']' + url + '[/img]');

editor.closeDropDown(true);
e.preventDefault();
});

editor.createDropDown(caller, 'insertimage', $content.get(0));
},
exec: function (caller) {
$.sceditor.command.get('image')._dropDown(this, caller);
},
txtExec: function (caller) {
$.sceditor.command.get('image')._dropDown(this, caller);
},
});


~~~~~~~

So that works and that part of the code becomes:


	// Update image command to support MyBB syntax
	$.sceditor.formats.bbcode.set('img', {
		format: function (element, content) {
			if ($(element).data('sceditor-emoticon'))
				return content;

			var url = $(element).attr('src'),
				width = $(element).attr('width'),
				height = $(element).attr('height'),
				align = $(element).data('scealign');

			var attrs = width !== undefined && width > 0
				? '=' + width
				: ''
			;

			if (align === 'left' || align === 'right')
				attrs += ' align='+align

			return '[img' + attrs + ']' + url + '[/img]';
		},
		html: function (token, attrs, content) {
			var	width, height, match,
				align = attrs.align,
				attribs = '';

			// handle [img=340x240]url[/img]
			if (attrs.defaultattr) {
				match = attrs.defaultattr.split(/x/i);

				width  = match[0];
				height = (match.length === 2 ? match[1] : match[0]);

				if (width !== undefined && width > 0) {
					attribs +=
						' width="' + $.sceditor.escapeEntities(width, true) + '"';
				}
			}

			if (align === 'left' || align === 'right')
				attribs += ' style="float: ' + align + '" data-scealign="' + align + '"';

			return '<img' + attribs +
				' src="' + $.sceditor.escapeUriScheme(content) + '" />';
		}
	})

	$.sceditor.command.set('image', {
		_dropDown: function (editor, caller) {
			var $content;

			$content = $(
				'<div>' +
				'<div>' +
				'<label for="image">' + editor._('URL') + ':</label> ' +
				'<input type="text" id="image" placeholder="https://" />' +
				'</div>' +
				'<div>' +
				'<label for="width">' + editor._('Width (optional)') + ':</label> ' +
				'<input type="text" id="width" size="2" />' +
				'</div>' +
				'<div>' +
				'<label for="height">' + editor._('Height (optional)') + ':</label> ' +
				'<input type="text" id="height" size="2" />' +
				'</div>' +
				'<div>' +
				'<input type="button" class="button" value="' + editor._('Insert') + '" />' +
				'</div>' +
				'</div>'
			);

			$content.find('.button').on('click', function (e) {
				var url = $content.find('#image').val(),
					width = $content.find('#width').val(),
				;

				var attrs = width !== undefined && width > 0
					? '=' + width
					: ''
				;

				if (url)
					editor.insert('[img' + attrs + ']' + url + '[/img]');

				editor.closeDropDown(true);
				e.preventDefault();
			});

			editor.createDropDown(caller, 'insertimage', $content.get(0));
		},
		exec: function (caller) {
			$.sceditor.command.get('image')._dropDown(this, caller);
		},
		txtExec: function (caller) {
			$.sceditor.command.get('image')._dropDown(this, caller);
		},
	});

Have I broke anything else do you think or made it insecure?

So I did want to remove the ability to input a height .... and I did (the idea was to remove any way to use the input area for 'bad' input) .... but it suddenly flipped back and I couldn't remove it again??

I changed this part of the code:

'<label for="height">' + editor._('Height (optional)') + ':</label> ' +
'<input type="text" id="height" size="2" />' +

to:

'<label for="height">' + editor._('Height (optional)') + ':</label> ' +
'<input type="hidden" value="" id="height" size="2" />' +

I know that within the sceditor files it is mentioned 3 times .... and I wonder it altering those would remove the "height" dropdown???

Go it to do what I needed .... I put all files back as per 1.8.37 and the changes are ....

In "bbcodes-sceditor.js" .... from line 594 to line 690 the code was changed to this:

	// Update image command to support MyBB syntax
	$.sceditor.formats.bbcode.set('img', {
		format: function (element, content) {
			if ($(element).data('sceditor-emoticon'))
				return content;

			var url = $(element).attr('src'),
				width = $(element).attr('width'),
				height = $(element).attr('height'),
				align = $(element).data('scealign');

			var attrs = width !== undefined && width > 0
				? '=' + width
				: ''
			;

			if (align === 'left' || align === 'right')
				attrs += ' align='+align

			return '[img' + attrs + ']' + url + '[/img]';
		},
		html: function (token, attrs, content) {
			var	width, height, match,
				align = attrs.align,
				attribs = '';

			// handle [img=340x240]url[/img]
			if (attrs.defaultattr) {
				match = attrs.defaultattr.split(/x/i);

				width  = match[0];
				height = (match.length === 2 ? match[1] : match[0]);

				if (width !== undefined && width > 0) {
					attribs +=
						' width="' + $.sceditor.escapeEntities(width, true) + '"';
				}
			}

			if (align === 'left' || align === 'right')
				attribs += ' style="float: ' + align + '" data-scealign="' + align + '"';

			return '<img' + attribs +
				' src="' + $.sceditor.escapeUriScheme(content) + '" />';
		}
	})

	$.sceditor.command.set('image', {
		_dropDown: function (editor, caller) {
			var $content;

			$content = $(
				'<div>' +
				'<div>' +
				'<label for="image">' + editor._('URL') + ':</label> ' +
				'<input type="text" id="image" placeholder="https://" />' +
				'</div>' +
				'<div>' +
				'<label for="width">' + editor._('Width (optional)') + ':</label> ' +
				'<input type="text" id="width" size="2" />' +
				'</div>' +
				'<div>' +
				'<label for="height">' + editor._('') + '</label> ' +
				'<input type="hidden" value="" id="height" size="2" />' +
				'</div>' +
				'<div>' +
				'<input type="button" class="button" value="' + editor._('Insert') + '" />' +
				'</div>' +
				'</div>'
			);

			$content.find('.button').on('click', function (e) {
				var url = $content.find('#image').val(),
					width = $content.find('#width').val(),
				;

				var attrs = width !== undefined && width > 0
					? '=' + width
					: ''
				;

				if (url)
					editor.insert('[img' + attrs + ']' + url + '[/img]');

				editor.closeDropDown(true);
				e.preventDefault();
			});

			editor.createDropDown(caller, 'insertimage', $content.get(0));
		},
		exec: function (caller) {
			$.sceditor.command.get('image')._dropDown(this, caller);
		},
		txtExec: function (caller) {
			$.sceditor.command.get('image')._dropDown(this, caller);
		},
	});

And in "jquery.sceditor.bbcode.min.js" .... very small changes.

Find this line (the file is minimized so you can either copy it >> format it >> find the line >> min it >> re-upload .... or just find and change it):

<div><label for="height">{height}</label> <input type="text" id="height" size="2" dir="ltr" /></div>

It is in this part:

image: '<div><label for="image">{url}</label> <input type="text" id="image" dir="ltr" placeholder="https://" /></div><div><label for="width">{width}</label> <input type="text" id="width" size="2" dir="ltr" /></div><div><label for="height">{height}</label> <input type="text" id="height" size="2" dir="ltr" /></div><div><input type="button" class="button" value="{insert}" /></div>',

And change the single line to:

<div><label for="height"></label> <input type="hidden" id="height" size="2" dir="ltr" /></div>

Then if I have it all correct (which it works with no errors .... that I have found) there will be only the option of changing the image width and it will auto the height!

[attachment=46767]
So, I have missing quote data?

When quoting a post which holds an image, there is no link to it (small green arrow) and no time?

I can't work out why the 2 file changes have caused it??

An example from the db

[quote="lost_puppy" pid="1540" dateline="1710351935"]
[quote="user1"]
Looks like it's werkin' like a treat!

[img]https://i.imgur.com/p3fr2lI.gif[/img]
[/quote]

test on no image
[/quote]

test

In the "user1" quoted post there is no pid or dateline Huh
In the file "bbcodes-sceditor.js" starting at line 594 there is the below code .... I can read what it looks to be doing but why is it there?

It became part of the file in version 1.8.23.
If I remove it completely from the file (in 1.8.37) .... everything works as I want it to Huh 

// Update image command to support MyBB syntax
	$.sceditor.formats.bbcode.set('img', {
		format: function (element, content) {
			if ($(element).data('sceditor-emoticon'))
				return content;

			var url = $(element).attr('src'),
				width = $(element).attr('width'),
				height = $(element).attr('height'),
				align = $(element).data('scealign');

			var attrs = width !== undefined && height !== undefined && width > 0 && height > 0
				? '=' + width + 'x' + height
				: ''
			;

			if (align === 'left' || align === 'right')
				attrs += ' align='+align

			return '[img' + attrs + ']' + url + '[/img]';
		},
		html: function (token, attrs, content) {
			var	width, height, match,
				align = attrs.align,
				attribs = '';

			// handle [img=340x240]url[/img]
			if (attrs.defaultattr) {
				match = attrs.defaultattr.split(/x/i);

				width  = match[0];
				height = (match.length === 2 ? match[1] : match[0]);

				if (width !== undefined && height !== undefined && width > 0 && height > 0) {
					attribs +=
						' width="' + $.sceditor.escapeEntities(width, true) + '"' +
						' height="' + $.sceditor.escapeEntities(height, true) + '"';
				}
			}

			if (align === 'left' || align === 'right')
				attribs += ' style="float: ' + align + '" data-scealign="' + align + '"';

			return '<img' + attribs +
				' src="' + $.sceditor.escapeUriScheme(content) + '" />';
		}
	})

	$.sceditor.command.set('image', {
		_dropDown: function (editor, caller) {
			var $content;

			$content = $(
				'<div>' +
				'<div>' +
				'<label for="image">' + editor._('URL') + ':</label> ' +
				'<input type="text" id="image" placeholder="https://" />' +
				'</div>' +
				'<div>' +
				'<label for="width">' + editor._('Width (optional)') + ':</label> ' +
				'<input type="text" id="width" size="2" />' +
				'</div>' +
				'<div>' +
				'<label for="height">' + editor._('Height (optional)') + ':</label> ' +
				'<input type="text" id="height" size="2" />' +
				'</div>' +
				'<div>' +
				'<input type="button" class="button" value="' + editor._('Insert') + '" />' +
				'</div>' +
				'</div>'
			);

			$content.find('.button').on('click', function (e) {
				var url = $content.find('#image').val(),
					width = $content.find('#width').val(),
					height = $content.find('#height').val()
				;

				var attrs = width !== undefined && height !== undefined && width > 0 && height > 0
					? '=' + width + 'x' + height
					: ''
				;

				if (url)
					editor.insert('[img' + attrs + ']' + url + '[/img]');

				editor.closeDropDown(true);
				e.preventDefault();
			});

			editor.createDropDown(caller, 'insertimage', $content.get(0));
		},
		exec: function (caller) {
			$.sceditor.command.get('image')._dropDown(this, caller);
		},
		txtExec: function (caller) {
			$.sceditor.command.get('image')._dropDown(this, caller);
		},
	});