<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Móricz Gergő on Medium]]></title>
        <description><![CDATA[Stories by Móricz Gergő on Medium]]></description>
        <link>https://medium.com/@skiilaa?source=rss-b3191c4d2f70------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*oFNUDbqBIsqIc6W6GJESLw.png</url>
            <title>Stories by Móricz Gergő on Medium</title>
            <link>https://medium.com/@skiilaa?source=rss-b3191c4d2f70------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 14:10:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@skiilaa/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How I created a magical Imgur.]]></title>
            <link>https://medium.com/@skiilaa/how-i-created-a-magical-imgur-169b05f3c54a?source=rss-b3191c4d2f70------2</link>
            <guid isPermaLink="false">https://medium.com/p/169b05f3c54a</guid>
            <category><![CDATA[image-recognition]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Móricz Gergő]]></dc:creator>
            <pubDate>Fri, 07 Apr 2017 04:52:29 GMT</pubDate>
            <atom:updated>2017-04-07T04:52:29.556Z</atom:updated>
            <content:encoded><![CDATA[<p>I have wanted to do something with AI for a long time now. I had already made prediction applications in python, but recently, I was more interested in recognition with AI. If I had never gotten free stickers from <a href="https://dev.to/">dev.to</a>, I would have never googled Clarifai and never started this project.</p><p>The basic idea was to help artists and photographers to get more views on an image, by automatically assigning tags to the picture based on what Clarifai recognizes. With this, the risk of not getting your picture found because you used the wrong tags is very low.</p><p>So I created an application, and I didn’t expect too much from the AI, but when I uploaded a stock photo, I was amazed.</p><p>It made such accurate guesses that it must have been trained on more than a billion images.</p><p>Then I started to assemble the application.</p><p>I decided to call it “SateMage”.</p><h3>DAY 1</h3><h3>The frontend</h3><p>I started to code a <em>“Hang tight! We are opening soon.”</em> temporary homepage, then I created the frontend upload form, the frontend image-viewer page and the 404, 500, etc. pages.</p><h3>The backend</h3><p>The real interesting piece about this is the backend.</p><h3>Uploading</h3><p>When the frontend upload form sends a request to the backend, these are the things that need to run:</p><ol><li>Encode image into base64</li><li>Upload it to imgur</li><li>Get tags from clarifai</li><li>Send tags, link and id to database</li></ol><p>For the first step, I used <a href="https://github.com/expressjs/multer">multer</a>’s MemoryStorage option, to be able to just get a buffer variable, instead of saving it and then reading it.</p><p>Then, I just needed to use this piece of code to turn the buffer into base64:</p><pre>var fileBase = req.file.buffer.toString(&#39;base64&#39;);</pre><p>Step 1 completed.</p><p>Onto step 2, I needed to upload the image to imgur in base64 format. I used the <a href="https://www.npmjs.com/package/imgur">imgur</a> package, that is able to do that.</p><p>With these couple of lines of code, I can upload the file to imgur:</p><pre>imgur.uploadBase64(fileBase)<br>    .then(function (json) {<br>        //...<br>    })<br>    .catch(function (err) {<br>        //...<br>    });</pre><p>Step 2 accomplished. Step 3 is the golden crown of the idea. If it doesn’t work, this can go in the trash.</p><p>I used the official <a href="https://www.npmjs.com/package/clarifai">Clarifai</a> module, and after analyzing the response a bit, I got it to work. Step 3 (thank goodness) works.</p><p>Now, Step 4. We know there is always something shitty with MySQL. Thankfully, that didn’t happen at inserting the images. The upload worked perfectly.</p><h3>Viewing</h3><p>The viewing. I wasted so much time on this, that I am still angry.</p><p>So, it had to do this:</p><ol><li>Load data from MySQL database</li><li>Display picture and tags</li></ol><p>(As PHP developers know) Projects with MySQL can’t work on the first try. They just can’t. Sadly.</p><p>So, step 1. There was user input here, so I decided to use prepared statements. I thought it was a good idea, because it worked like a charm in my PHP projects.</p><p>Then, I got into SQL hell. Variables were suddenly undefined, callback was suddenly called multiple times, and I was really frustrated. I ended up using connection.escape() and guess what, it worked like a charm.</p><p>After I wasted 2 or so hours on the MySQL part, the displaying was easy. Just used some placeholder values that were replaced when reading the files from disk, for example “:imgurUrlHere”, and “:clarifaiTagsHere”.</p><h3>DAY 2</h3><p><strong>And SQL still can’t work on first try.</strong></p><h3>Searching</h3><p>This was the easiest of all functions(at least after I figured out reversing the natural sort of MySQL).</p><p>I basically used SELECT with a % wildcard in the LIKE operator, like %query%, then place the results in the search HTML template.</p><p><strong>NOTE: I also added the natural sort-reverse tweak.</strong></p><p>The frontend was also changed: I added a search form into the header.</p><h3>The Index Page</h3><p>The index page wasn’t easy to make.</p><p>I used the card-group class in BootStrap for keeping the cards that contain the images in a line, and replaced some placeholder texts with the cards.</p><p>I executed 2 SQL queries:</p><ol><li>8 newest images</li><li>8 most popular images</li></ol><h3>1. Newest images</h3><p>I used the “natural sort-reverse” tweak that I mentioned earlier.</p><p>It uses variables to reverse the natural sort:</p><pre>SELECT *, @counter := @counter + 1 AS &#39;counter&#39; FROM images, (SELECT @counter := 0) r ORDER BY counter DESC LIMIT 8</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*PCVF6FvOWKyk0D6t.jpg" /></figure><p>I would much prefer the following than that solution:</p><pre>SELECT * FROM images ORDER BY NATURAL DESC LIMIT 8</pre><p>But of course this doesn’t exist, so deal with it I guess.</p><p>After getting the results of this, I use a foreach loop to build the HTML that will replace the placeholder text.</p><h3>2. Most popular images</h3><p>This is a simple SELECT query with an ORDER BY operator:</p><pre>SELECT * FROM images ORDER BY views DESC LIMIT 8</pre><p>This selects the top 8 rows based on the field views.</p><h3>The viewcounter</h3><p>The viewcounter was fairly easy to make.</p><p>The following SQL query is executed after the image is sent to the user, and it is ignored if an error happens:</p><pre>UPDATE images SET views=views+1 WHERE id=&#39;imageId&#39;</pre><p>This query increments the view field by 1 where the id matches the specified ID.</p><h3>The future</h3><p>I have obviously not mothballed this project. I have to work on the search, the index page, test the backend, and I am likely going to make the project open source in the future.</p><p>The site has been released under a subdomain of my site, <a href="http://satemage.skiilaa.me/">satemage.skiilaa.me</a>.</p><p>If you want to help this project grow bigger, <a href="http://patreon.com/skiilaa">become a Patreon</a>.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=169b05f3c54a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>