DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Migrating from Sakila-MySQL to Couchbase - Part 3: Stored Procedures
  • MDC Logging With MuleSoft Runtime 4.4
  • Generate a Dependent Drop List in a Spreadsheet Using Go Language
  • Drag and Drop Using Angular 7

Trending

  • Spring Authentication With MetaMask
  • Automated Testing Lifecycle
  • Automated Testing: The Missing Piece of Your CI/CD Puzzle
  • Selecting the Right Automated Tests

Drag and Drop Image Matching Search at Behance

Raymond Camden user avatar by
Raymond Camden
·
Nov. 25, 13 · Interview
Like (0)
Save
Tweet
Share
3.95K Views

Join the DZone community and get the full member experience.

Join For Free

so, 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.

download attached file

Drops (app)

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Migrating from Sakila-MySQL to Couchbase - Part 3: Stored Procedures
  • MDC Logging With MuleSoft Runtime 4.4
  • Generate a Dependent Drop List in a Spreadsheet Using Go Language
  • Drag and Drop Using Angular 7

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: