Please note this is a self-answered question.
This question is about the ToastUI Image Editor v3.3.0, but it may also apply to newer versions.
When you load an image using this official example:
// Create image editor
var imageEditor = new tui.component.ImageEditor('#my-image-editor canvas', {
cssMaxWidth: 1000, // Component default value: 1000
cssMaxHeight: 800 // Component default value: 800
});
// Load image
imageEditor.loadImageFromURL('img/sampleImage.jpg', 'My sample image')
The editor will not load the image. The function neither throws, nor returns anything indicating a failure and you don't get any error messages. It returns a promise that resolves as specified in the documentation.
It only loads an image by specifying it in the initial config and you can't change it afterwards:
// Create image editor
var imageEditor = new tui.component.ImageEditor('#my-image-editor canvas', {
includeUI: {
loadImage: {
path: 'img/sampleImage.jpg',
name: 'My sample image'
},
},
cssMaxWidth: 1000, // Component default value: 1000
cssMaxHeight: 800 // Component default value: 800
});
It appears that the loadImageFromURL function is broken and according to other users loadImageFromFile
has the same problem.
Issues about this have been raised on GitHub, but have basically been ignored. It's been a month now and unfortunately it still hasn't been fixed.
So the question is how can the image editor be tricked into working while this issue exists.
Here is a fiddle showing that it doesn't work: https://fiddle.sencha.com/#view/editor&fiddle/2org
TL;DR:
Here is a working fiddle: https://fiddle.sencha.com/#view/editor&fiddle/2p0o
Long version:
There are four problems:
loadImageFromURL
, otherwise you may get an error or a silent failureAccess-Control-Allow-Origin
header and explicitly allow your domain to access it, otherwise the image editor can not access it.This first problem can be solved by loading a blank image like this:
The second problem can be solved by waiting for the image editor to get out of its lock state using undocumented functionality. You can patch your
loadImageFromURL
during runtime like this:The third problem can be resolved by taking the object that the promise returned by
loadImageFromURL
resolves with and passing the new and old width/height properties to theui.resizeEditor
function like this:The fourth problem can be a bit confusing. Let me explain. On websites you can include pretty much any external image you want in using an
<img>
tag, but if you want to access an external image using JavaScript, the server providing the image has to explicitly allow you to do that using theaccess-control-allow-origin
header. On Amazon S3 for instance, the servers don't allow this by default. You have to manually set the server to allow your or any domain to access it. See here. If you are using a different server, you could for example set theaccess-control-allow-origin
to*
like wikipedia has done on this image. Then you (and the image editor) could access that image from the JavaScript of any domain.