35

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.stack.imgur.com/i8e7s.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?

9
  • 1
    No, this shouldn't be changed, since those aren't pictures of unicorns.
    – M.A.R.
    Oct 12, 2015 at 20:20
  • 13
    I didn't think it was legal to host non-cat-based images on imgur. Perhaps that's the issue here?
    – user1228
    Oct 12, 2015 at 20:44
  • 6
    @Won't There. Unicorns, cats, blood, fire and rainbows for you folks. Now give me CORS. :(
    – Siguza
    Oct 12, 2015 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, 2015 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, 2015 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 ;)) Oct 13, 2015 at 13:46
  • @ShadowWizard No, IE just works (just verified with the developers tools and network panel open...)
    – rene
    Oct 13, 2015 at 13:54
  • @rene yes it works, but it doesn't support those cool features discussed here. (the snippet does not use them) Oct 13, 2015 at 13:55
  • @ShadowWizard :((((
    – rene
    Oct 13, 2015 at 14:19

1 Answer 1

12

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';

9
  • 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. Oct 13, 2015 at 14:46
  • 3
    Ugh, I completely botched that one. My bad.
    – user1228
    Oct 13, 2015 at 15:05
  • 9
    Treasure this moment, @Tiny - I've never seen Mr. Won't apologize before. ;) Oct 13, 2015 at 15:19
  • Hmm I get error loading image with this, but not with the original images...
    – Tim
    Oct 28, 2015 at 22:54
  • @TinyGiant Chrome 46.0.2490.80 and I see this: imgur.com/DXskcLG
    – Tim
    Oct 28, 2015 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, 2015 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. Nov 3, 2017 at 18:51
  • 1
    Even this solution seems to have been broken now
    – Ferrybig
    Feb 15, 2019 at 10:22
  • Whoever ran the Heroku app on that webpage isn’t running it anymore.
    – Cole Tobin
    Dec 7, 2020 at 19:48

You must log in to answer this question.

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