Matt Hackmann

MattHackmann

The thoughts and goings-on of some programmer dude.

PHP Google Image Search Library

Github Repository: Fork It! Code: [ Download ] Demo: [ Try it out ]

EDIT 2012-05-31: I have updated the code to work with the latest source generated by Google Images, so it works again. Being that this is a scraper, I cannot make an guarantees that it will continue to operate correctly in the future. If you see any issues, let me know and I will do my best to patch accordingly.

Sometime ago, I found myself wanting to be able to programatically perform a Google image search (it was for album art on the music page). As Google does not provide an API for backend scripting, I wrote a quick scraper that does the job quite nicely. Hit the break for info on how to use this.


include ('gis.php');
$results = googleImageSearch('chobits');
print_r($results);

Here's the function prototype:


googleImageSearch($query, $page = 1, $size = GIS_ANY, $type = GIS_ANY);

This returns an array of stdObject with the following properties:

  • resultLink - Link to Google image result. This is the page that displays when you click a result through normal image search
  • source - URL of the source image
  • title - Title associated with the image
  • width - Width of the image
  • height - Height of the image
  • size - Size of the image
  • type - Format of the image (jpg, png, etc)
  • domain - Domain of source image
  • thumb - stdObject with information on Google's thumbnail of the image. Properties are:
  • thumb->src - URL to the thumbnail
  • thumb->width - Width of the thumbnail
  • thumb->height - Height of the thumbnail

Size constants are (all fairly self-explanatory):

  • GIS_LARGE
  • GIS_MEDIUM
  • GIS_SMALL

And constants for type:

  • GIS_FACE
  • GIS_PHOTO
  • GIS_CLIPART
  • GIS_LINEART

If you have any comments, questions or issues with this code, just leave a comment below and I'll do my best to help you!