Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>Note</em>: This is using the anonymous imgur uploader with my anonymous key. The imgur site restricts uploads to 50 uploads/hour which should be fine normally, but this may cause a problem if a lot of people try this simultaneously. So please get your own anonymous key here: </p> <p><a href="http://imgur.com/register/api_anon" rel="nofollow noreferrer">http://imgur.com/register/api_anon</a></p> <p>And then replace the key in the code below with your own key (<em>thanks!</em>).</p> <p>The trickiest part to code was the conversion from a Mathematica expression to PNG image to Base64 encoding to URL encoding. There are about a 1,000 ways to do it wrong and I think I managed to try them all.</p> <p>The code breaks down into a few pieces: </p> <ul> <li>Construct the POST url</li> <li>Make the HTTP connection</li> <li>Send the POST url</li> <li>Read back the result, which is XML</li> <li>Extract the imgur url from the XML</li> <li>Format the imgur url as markdown (or as a <em>Mathematica</em> <code>Hyperlink</code> function).</li> </ul> <p>Here is the code:</p> <pre><code>imgur[expr_] := Module[{url, key, image, data, jUrl, jConn, jWriter, jInput, buffer, byte, xml, imgurUrl}, Needs["JLink`"]; JLink`JavaBlock[ JLink`LoadJavaClass["java.net.URLEncoder"]; url = "http://api.imgur.com/2/upload"; key = "c07bc3fb59ef878d5e23a0c4972fbb29"; image = ExportString[ExportString[expr, "PNG"], "Base64"]; data = URLEncoder`encode["key" , "UTF-8"] &lt;&gt; "=" &lt;&gt; URLEncoder`encode[ key , "UTF-8"] &lt;&gt; "&amp;" &lt;&gt; URLEncoder`encode["image" , "UTF-8"] &lt;&gt; "=" &lt;&gt; URLEncoder`encode[ image , "UTF-8"] ; jUrl = JLink`JavaNew["java.net.URL", url]; jConn = jUrl@openConnection[]; jConn@setDoOutput[True]; jWriter = JLink`JavaNew["java.io.OutputStreamWriter", jConn@getOutputStream[]]; jWriter@write[data]; jWriter@flush[]; jInput = jConn@getInputStream[]; buffer = {}; While[(byte = jInput@read[]; byte &gt;= 0), AppendTo[buffer, byte]]; ]; xml = ImportString[FromCharacterCode[buffer], "XML"]; imgurUrl = Cases[xml, XMLElement["original", {}, {string_}] :&gt; string, \[Infinity]][[1]]; "![Mathematica graphic](" &lt;&gt; imgurUrl &lt;&gt; ")" ] </code></pre> <p>Testing:</p> <pre><code>In[]:= g = Graphics[{Blue, Disk[]}, PlotRange -&gt; 1.2, ImageSize -&gt; Small]; pic = Overlay[{Blur[Binarize@g, 10], g}]; imgur[pic] Out[]= ![Mathematica graphic](http://i.imgur.com/eGOlL.png) </code></pre> <p>And the actual image:</p> <p><img src="https://i.imgur.com/eGOlL.png" alt="Mathematica graphic"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload