<?php
class GoogleSearch {

function image_search($keywords) {
	$host = 'http://images.google.co.jp/images';
	$encoding = mb_internal_encoding();
	$params['as_q'] = str_replace(' ','+',mb_convert_encoding($keywords,'UTF-8',$encoding));
	$params['hl'] = 'ja';
	$params['output'] = 'images';
	$params['svnum'] = '10';
	$params['as_sitesearch'] = 'response.jp';
	
	$query = http_build_query($params);
	$request = "$host?$query";
	$result = file($request);
	foreach($result as $line) {
		if(preg_match("/window.dyn/",$line)) {
			$images = explode(';',$line);
			break;
		}
	}
	
	foreach($images as $image_source) {
		if(preg_match("/dyn.Img\((.*)\)/",$image_source,$matches)) {
			$args = explode('","',$matches[1]);
			mb_convert_variables('EUC-JP','auto',$args);
			$args = str_replace(array('\x3c','\x3e'),array('&lt;','&gt;'),$args);
			$thumbnail_host = $args[14];
			$thumbnail_param = 'tbn:'.$args['2'].$args[3];
			$thumbnail['thumbnail_url'] = "$thumbnail_host?q=$thumbnail_param";
			$thumbnail['thumbnail_width'] = $args[4];
			$thumbnail['thumbnail_height'] = $args[5];
			$thumbnail['image_url'] = $args[3];
			$thumbnail['caption'] = $args[6];
			$thumbnail['copy_of_size'] = $args[9];
			$thumbnail['image_type'] = $args[10];
			$thumbnail['domain'] = $args[11];

			$page = parse_str('&page_url='.substr($args[0],1));
			$thumbnail['page_url'] = $page_url;
			$thumbnail['page_lang'] = $hl;
			$thumbnail['image_width'] = $w;
			$thumbnail['image_height'] = $h;
			
			$results[] = $thumbnail;
		}
	}
	return $results;
}

} // end of class GoogleSearch
?>