- Home /
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.
The javascript upload script on the API page seems to be creating a form object containing the image info, which is then sent to a upload.json file on the imgur domain. To send the image using this script you would probably first have to create a php file containing the javascript as well as some php to check for any variables set in the url to your php file. I'm not sure how you are sending your image to your webpage. eg are you sending all the actual source code of the image? Unity communicates with your webpage through sending info in the url. To do what you are attempting will as far as I know require you to send the image data not directly to imgur, but rather to a webpage created by you containing the upload script. The upload script would then receive the image info from unity and send it to the imgur API. Sorry I can't be of more help, but I've never tried uploading an image through unity.. just my thoughts on how one might go about it ;)
Answer by by0log1c · Sep 07, 2012 at 01:51 AM
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 asp.net 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.