Drag and Drop Image Matching Search at Behance
Join the DZone community and get the full member experience.
Join For Freeso, i promise, this is the last behance api demo this week. i don't know why this has been on my mind so much, but, screw it, when inspiration strikes i just go along with it. for today's demo i'm going to show something a bit different - search by color.
as you can probably guess, behance allows us to search for projects that match a certain color (or color range). building that into my api would be kinda boring, so i decided to build something cooler. imagine being able to simply drop a picture onto your browser and have it return matching projects...
here is an example. in the screen shot below, the image you see on the top left is one i dragged directly from my desktop.
and another one. (again, the image on the top left, the kitten, came from my computer.)
ok, so how is it done? for the most part, pretty simply. the drag and drop aspect is rather trivial and i've blogged on it before. here is that code snippet.
$(document).ready(function() { //set my key behanceapi.setkey(behancekey); imgdom = $("#droppedimage"); resultsdom = $("#results"); $(document).on("drop", drophandler); $(document).on("dragover", dragoverhandler); }); //i simply validate the type function validfile(s) { if(s.match("image/*")) return true; return false; } function dragoverhandler(e) { e.preventdefault(); } function drophandler(e) { e.stoppropagation(); e.preventdefault(); if(!e.originalevent.datatransfer.files) return; var files = e.originalevent.datatransfer.files; var count = files.length; if(!count) return; //only one file allowed if(count > 1) { window.alert("you may only drop one file."); return; } var file = files[0]; if(!validfile(file.type)) { window.alert("you must drop an image."); return; } var reader = new window.filereader(); reader.onload = function (e) { imgdom.attr("src",e.target.result); imgdom.on("load", function(e) { docolor(); }); }; reader.readasdataurl(file); }
next - i make use of
colorthief
, a library i've mentioned a few times here. it has a function to return the dominant color of a photo.
function docolor() { var imageresults = []; console.log("do color"); var colorthief = new colorthief(); //dominantcolor = getdominantcolor(imgdom); var dom = colorthief.getcolor(imgdom[0]);
this returns an array of colors. from that i can build a rgb string and pass it to a new method of my api that i built to support the search api. note that behance supports ranges as well. i could make my demo a bit cooler if i created a range based on the color i got. colorthief also supports returning a set of dominant colors. i could use that api and perform multiple behance searches to return a collected set of possible matches.
var rgb = dom[0].tostring(16) + dom[1].tostring(16) + dom[2].tostring(16); resultsdom.html("<i>searching behance for stuff like this - prepare for teh awesome...</i>"); //ok, now search by color behanceapi.getprojectsbycolor(rgb, function(p) { console.log("back from the api"); //gather up the biggest images possible (nah, just check 2 biggest for(var i=0;i<p.length;i++) { if(p[i].covers[404]) { imageresults.push(p[i].covers[404]); } else if(p[i].covers[230]) { imageresults.push(p[i].covers[230]); } } var html = ""; for(var i=0;i<imageresults.length;i++) { html += "<img src='" + imageresults[i] + "'><br/>"; } resultsdom.html(html); });
and that's it. i've actually put the demo online so you can see, but please remember that behance limits api calls to 150 an hour. based on the average traffic of my blog that limit will probably be blown away. but you may get lucky. i've attached the code as a zip as well.

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments