34

Every now and then I happen to come across JS questions that involve canvases and images in some way.
When attempting to answer, I usually create a runnable snippet.
Now, because of same-origin-policy, any images I use will "taint" the canvas, thus breaking some functionality I sometimes require.
That is, unless I manage to get a CORS enabled image, and here's the point:

On i.imgur.com, images are CORS-enabled.
On i.stack.imgur.com however (which is used for image uploads from the editor), images are not CORS-enabled.


Demo:

i.imgur.com:

var i = new Image();
i.crossOrigin = '';
i.onload = function() {
    document.write('Image loaded successfully');
};
i.onerror = function(e) {
    document.write('Error loading image');
};
i.src = 'https://i.imgur.com/fHyEMsl.jpg';

i.stack.imgur.com (only last line changed):

var i = new Image();
i.crossOrigin = '';
i.onload = function() {
    document.write('Image loaded successfully');
};
i.onerror = function(e) {
    document.write('Error loading image');
};
i.src = 'https://i.stack.imgur.com/PORPF.jpg';

And just for the record, both images are present:

one:

first image

and two:

second image


Could this be changed please?
Sure, I could just use "regular" Imgur as a workaround, but then what would be the point of Stack's Imgur account?
And I imagine if anyone can make this change happen, it's the folks at SE, so I'm asking you:

Can i.stack.imgur.com please be CORS-enabled?

  • No, this shouldn't be changed, since those aren't pictures of unicorns. – M.A.R. Oct 12 '15 at 20:20
  • 12
    I didn't think it was legal to host non-cat-based images on imgur. Perhaps that's the issue here? – Won't Oct 12 '15 at 20:44
  • 5
    @Won't There. Unicorns, cats, blood, fire and rainbows for you folks. Now give me CORS. :( – Siguza Oct 12 '15 at 20:53
  • IE doesn't support that based on MDN and I don't want to be left out... and I fail to understand why you can't use a data:image/base64;... for this purpose? Strangely enough both of your example show Image loaded successfully so now I'm even more confused... – rene Oct 12 '15 at 21:02
  • 4
    @rene I tried base64, but there's a character limit of 30'000 characters per post, and the base64 version of the deer image above is alone 35178 characters. The rest of the post in which I wanted to use that image is a bit more than 6000 characters, so sure, I could've searched for a smaller image too, but it was a question about image rotation, so small images kinda suck, and in that case I could've just gone with regular Imgur (as I did in the end). Also, when trying to insert a base64 blob into the editor again now, my tab constantly crashes. I kinda like links, they're somewhat shorter... – Siguza Oct 12 '15 at 21:11
  • @rene as for both loading, IE probably just ignores the whole thing. It's like the unpopular kid left alone, while the cool kids play with the cool stuff. (IE browser, I mean ;)) – Shadow The Dragon Wizard Oct 13 '15 at 13:46
  • @ShadowWizard No, IE just works (just verified with the developers tools and network panel open...) – rene Oct 13 '15 at 13:54
  • @rene yes it works, but it doesn't support those cool features discussed here. (the snippet does not use them) – Shadow The Dragon Wizard Oct 13 '15 at 13:55
  • @ShadowWizard :(((( – rene Oct 13 '15 at 14:19
11

As a temporary work-around you can use a proxy for the i.stack.imgur images. Using http://cors.io works in the example below.

var i = new Image();
i.crossOrigin = '';
i.onload = function() {
    document.write('Image loaded successfully with: "' + i.src + '"');
};
i.onerror = function() {
    i.src = 'http://cors.io?u=' + i.src;
    i.onerror = function() {
        document.write('Error loading image');
    }
};
i.src = 'https://i.stack.imgur.com/PORPF.jpg';

  • 3
    @Won't Please actually use the snippet to determine if it is runnable or not before removing the snippet controls. I am well aware of the difference between code formatting and snippets. – Diminutive Colossus Oct 13 '15 at 14:46
  • 3
    Ugh, I completely botched that one. My bad. – Won't Oct 13 '15 at 15:05
  • 9
    Treasure this moment, @Tiny - I've never seen Mr. Won't apologize before. ;) – Shadow The Dragon Wizard Oct 13 '15 at 15:19
  • Hmm I get error loading image with this, but not with the original images... – Tim Oct 28 '15 at 22:54
  • @TinyGiant Chrome 46.0.2490.80 and I see this: imgur.com/DXskcLG – Tim Oct 28 '15 at 23:07
  • @TinyGiant my bad, it was blocked by a spam filter that didn't show the error message in the snippet. Sorry :/ – Tim Oct 28 '15 at 23:08
  • 1
    This fails with Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://cors.io/?u=https://i.stack.imgur.com/PORPF.jpg. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). on Firefox 56. – Awesome Poodles Nov 3 '17 at 18:51
  • Even this solution seems to have been broken now – Ferrybig Feb 15 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .