How can I upload a PNG to imgur and save its URL?

I have a screenshot I want to post to Facebook but I can only post a link to it. So I want to upload it to imgur, automatically from a script in Unity, and return the URL directly linking to the image.

They have an API page but I can’t figure out how to translate that into a WWW call from Unity. It seems like it should be a lot simpler than it looks from their examples.

I’ll admit I haven’t read the full API but only peek at the ImgurExample class from the page you linked.

What they do is they take in an image path, read its content as byte, then convert it to a base64 string. Your image is now effectively a string of text which can be turned back into an image (think: serialization). They then create a standard POST call to the imgur url and pass it the text, imgur then return some text. I assume that imgur convert your image string back into an image and that the text it returns is the final image url you need. Using it within Unity should be fairly easy.

The first way I can think of is to … use the very example provided - that is valid C# code and I assume it’ll work out-of-the-box. I’m not 100% sure that Mono provides the System.Net namespace, but they probably do - and unless you plan to go crossplatform, you could still workaround the issue by importing your .NET dll (assuming you’re on Windows).

The second way I can think of is to use only Unity stuff, that is assured to work. You can use the first half of the example to convert your image to text. Once you have the imgur url, your image-text - and apparently an imgur api key - you will use the WWWForm class to create your own POST. That form should include 2 text fields named ‘image’ and ‘key’ and holding the relevant value. Pass that WWWForm through a WWW aimed at the imgur url and yield that. Once its all done, www.text should hold the final url.

Disclaimer: I assumed all that stuff from looking at the example one(1) minute, I might be totally off.

EDIT: if you had access to webserver space, it’d also be easy to create a php or script - but in the end, that’s what imgur does, you still have to do the unity side stuff and it’d be pretty much the same.

EDIT2: no idea why the System.Net is an url, its not me who set it.

link text