Here it goes. I don't remember where but I found it somewhere on the web and have been using it for one of my project.
To get the results run the following in PHP and it will return back the link of images in array. Then you can use file_get_contents() to get the image from its URL and then save it to on your server. Not too hard. If you don't know PHP then hire some guy, not a hard task.
Usage:
PHP Code:
imageSearch("keyword here");
Following contains the function and class, make sure to include it.
PHP Code:
function imageSearch($query) {
$image_data = object_to_array(googleImageSearch($query));
foreach($image_data as $image_obj) {
$image_array = object_to_array($image_obj);
$image_links[] = $image_array['source'];
}
return $image_links;
}
function object_to_array($data) {
if(is_array($data) || is_object($data)) {
$result = array();
foreach($data as $key => $value) {
$result[$key] = object_to_array($value);
}
return $result;
}
return $data;
}
// Image sizes
define ('GIS_LARGE', 'l');
define ('GIS_MEDIUM', 'm');
define ('GIS_ICON', 'i');
define ('GIS_ANY', '');
// Image types
define ('GIS_FACE', 'face');
define ('GIS_PHOTO', 'photo');
define ('GIS_CLIPART', 'clipart');
define ('GIS_LINEART', 'lineart');
function googleImageSearch ($query, $page = 1, $size = GIS_ANY, $type = GIS_ANY)
{
$retVal = array();
// Get the search results page
$response = file_get_contents('http://images.google.com/images?hl=en&q=' . urlencode ($query) . '&imgsz=' . $size . '&imgtype=' . $type . '&start=' . (($page - 1) * 21));
// Extract the image information. This is found inside of a javascript call to setResults
preg_match('/dyn.setResults\(\[(.*?)\]\);/is', $response, $match);
if (isset($match[1])) {
// Grab all the arrays
preg_match_all('/\[(.*?)\"\]/', $match[1], $m);
foreach ($m[1] as $item) {
// Explode on each paramter (comma delimeter)
$item = urldecode(str_replace('\x', '%', $item));
preg_match_all('/\"(.*?)\"/', $item, $params);
// Check for more than one paramter. Not sure why, but there seem to be empty array sets between actual results
if (count($params[1]) > 0) {
$params = $params[1];
// Important array indices
// 0 - Link to Google image result. This is the page that displays when you click a result through normal image search
// 3 - URL of source image
// 4 - Width of the thumbnail image
// 5 - Height of the thumbnail image
// 6 - Title of the image
// 9 - Width, height and size of the image (In the format of 'width × height - size')
// 10 - Image type
// 11 - Originating domain
// 18 - URL of google's thumbnail
preg_match('/([\d]+) × ([\d]+) - ([\d]+)/', $params[9], $dimensions);
$t = null;
$t->resultLink = 'http://images.google.com' . $params[0];
$t->source = $params[3];
$t->title = $params[6];
$t->width = $dimensions[1];
$t->height = $dimensions[2];
$t->size = $dimensions[3];
$t->type = $params[10];
$t->domain = $params[11];
$t->thumb = null;
$t->thumb->src = $params[18];
$t->thumb->width = $params[4];
$t->thumb->height = $params[5];
$retVal[] = $t;
}
}
}
return $retVal;
}
Bookmarks