Skip to content

Instantly share code, notes, and snippets.

@markandey
Created March 3, 2012 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markandey/1966805 to your computer and use it in GitHub Desktop.
Save markandey/1966805 to your computer and use it in GitHub Desktop.
GetPictures from google filter supports based on color
/************************
function getPictures: returns image from query
filter: 0 no filter, 1 for grayscale and subsequent for
color from color index
isFlickr: true if only flickr photos
callback: function to call after load is complets
function(image,results){
image is image object of first image
results are the array of images
}
*************************/
function getPictures(text, filter,isFlickr,callback) {
var colors = [
GimageSearch.COLOR_RED,
GimageSearch.COLOR_ORANGE,
GimageSearch.COLOR_YELLOW,
GimageSearch.COLOR_GREEN,
GimageSearch.COLOR_TEAL,
GimageSearch.COLOR_BLUE,
GimageSearch.COLOR_PURPLE,
GimageSearch.COLOR_PINK,
GimageSearch.COLOR_WHITE,
GimageSearch.COLOR_GRAY,
GimageSearch.COLOR_BLACK,
GimageSearch.COLOR_BROWN
];
function searchComplete(data) {
if (data.results.length < 1) {
return;
}
var newImg = new Image();
newImg.src = data.results[0].url;
function afterLoad() {
if(typeof(callback)==='function'){
callback(newImg,data.results);
}
}
$(newImg).load(afterLoad);
}
var imageSearch = new google.search.ImageSearch();
imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE, google.search.ImageSearch.IMAGESIZE_MEDIUM);
if (filter > 0) {
if (filter == 1) {
imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_COLORIZATION, google.search.ImageSearch.COLORIZATION_GRAYSCALE);
} else if ((filter + 2) < colors.length) {
imageSearch.setRestriction(GimageSearch.RESTRICT_COLORFILTER, colors[filter - 2]);
}
}
imageSearch.setSearchCompleteCallback(this, searchComplete, [imageSearch]);
if (isFlickr) {
imageSearch.setSiteRestriction('flickr.com');
}
imageSearch.execute(text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment