Matt Hackmann

PHP Google Image Search Library

Posted On
Tags
api
code
php
release

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.

Code: php
  1. <?php
  2. include ('gis.php');
  3. $results = googleImageSearch('chobits');
  4. print_r($results);
  5. ?>

Here's the function prototype:

Code: php
  1. <?php
  2. googleImageSearch($query, $page = 1, $size = GIS_ANY, $type = GIS_ANY);
  3. ?>

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!

Comments

irphan
irphan

Hi ,iam unable to download the gis.php file . please help me

@irphan Fixed the link. Sorry about that!

sasha
sasha

hi i have a problem

how can i reed this array?

Array ( [0] => stdClass Object ( [resultLink] => http://images.google.com/imgres?imgurl=http://2.bp.blogspot.com/_Ibn9q9HHe9k/TEbQJDXRipI/AAAAAAAADIA/U5mbwr2iDY8/S350/books_1.gif&imgrefurl=http://granan10.blogspot.com/2011/05/question-of-week-5-9-11.html&usg=__hRG5oDSprICmg-mYJ3xL4lh8xl0=&h=300&w=300&sz=28&hl=en&start=1&zoom=1&itbs=1 [source] => http://2.bp.blogspot.com/_Ibn9q9HHe9k/TEbQJDXRipI/AAAAAAAADIA/U5mbwr2iDY8/S350/books_1.gif [title] => book [width] => 300 [height] => 300 [size] => 28 [type] => gif [domain] => granan10.blogspot.com [thumb] => stdClass Object ( [src] => http://t0.gstatic.com/images?q=tbn:ANd9GcThU5ZkFkLEXWUgxI6EtFGWhIKYdZrJL4anTBoh-qzfyRrFvKCkmr3GORI [width] => 116 [height] => 116 ) ) [1] => stdClass Object ( [resultLink] => http://images.google.com/imgres?imgurl=http://www.uiwp.uiuc.edu/porfolio_2008/erin_ludwick/BOOK.jpg&imgrefurl=http://www.uiwp.uiuc.edu/porfolio_2008/erin_ludwick/index.html&usg=__t-8VjUtG2hj0LmCE6OlCD3X5rpE=&h=435&w=700&sz=175&hl=en&start=2&zoom=1&itbs=1 [source] => http://www.uiwp.uiuc.edu/porfolio_2008/erin_ludwick/BOOK.jpg [title] => Book Clubs [width] => 700 [height] => 435 [size] => 175 [type] => jpg [domain]

dxprog
dxprog

I would use a foreach loop, like so:

foreach ($results as $result) {

echo $result->source;

}

Hope that helps ya!

Gokul
Gokul

How to increase the image count in search

dxprog
dxprog

Google itself limits the count and, as far as I'm aware, there's not way around that outside of multiple requests for each page of data.

Amit
Amit

Great jobs

Smith
Smith

Hi!

Thank you for really great script!

I have a little problem with it. There is no Width and height and size in a resulting Object for all full images. Here is an example:

[14] => stdClass Object

(

[resultLink] => http://images.google.com/imgres?imgurl=http://st.kinopoisk.ru/im/wallpaper/1/0/8/kinopoisk.ru-Beatrice-Rosen-1081628--w--1280.jpg&imgrefurl=http://www.kinopoisk.ru/picture/1081628/w_size/1280/&usg=__PJnNr_R0Nh_tWUvKJDu7SL_mLLQ=&h=960&w=1280&sz=101&hl=ru&start=15&zoom=1&itbs=1

[source] => http://st.kinopoisk.ru/im/wallpaper/1/0/8/kinopoisk.ru-Beatrice-Rosen-1081628--w--1280.jpg

[title] => Беатрис Розен (Beatrice Rosen)

[width] =>

[height] =>

[size] =>

[type] => jpg

[domain] => kinopoisk.ru

[thumb] => stdClass Object

(

[src] => http://t1.gstatic.com/images?q=tbn:ANd9GcSJRQBfT-0YkeU1GvGByFc38NqKH2xUr8mEJu6KAJDgV4AGvOdnA2aqBOYa

[width] => 150

[height] => 113

)

Can you help me to solve this problem?

dxprog
dxprog

Interesting. What was your search query?

thger
thger

Hi, do you know how to display the picture? Currently, its all in text form.

Thanks

@thger Could you elaborate? You are writing the url into an image tag, yes? (unless you're doing something different entirely)

monty
monty

hi, i have followed everything to the letter but all i get is an empty array returned. any ideas on how to fix this. ( print_r($results) gives me Array ()

Mowiba
Mowiba

Hi, Thanks for this. One problem is that URL of source is not returned.This is also the case for your example http://labs.dxprog.com/gis/Any ideas?Thanks.

shipar
shipar

Hi,

your demo does,t work.

Can you please tell me how It works?

And how your script run?

thanks

@shipar Sure enough. Looks like Google changed up their code (always an issue with writing scrapers). I'll have to rework the library.

plrtowprdpress
plrtowprdpress

"Google changed up" their code for Images? so we have to find out [smile]

jnana
jnana

Hi please do some help for me,,,, m beginner of PHP how to retrieve the first image from the google search.......

plz help me to solve this problem

jnana
jnana

Hi i hv tried ur above sample code but it does't work... plz inform me about which version php software i need to install now m usingeasyphp5.3.8.0

@jnana Google has since changed their code and my library no longer works. Such is life when messing with scrapers.

chrismccoy
chrismccoy

care to share dosearch.php? curious minds

@dxprog

thanks mate, curious can you scrape more than 1 page at once? or does it only allow you to do 1 at a time, so you would have to look through a number running the googleImageSearch function with 1 then 2 then 3, etc.

@chrismccoy Unfortunately, google only gives one page at a time, so you'll have to loop through all the page numbers.

@Scriptbaba Certainly aren't going for performance what with the calls to eval and code obfuscation by base64 encoding.

Stay away from this one, guys.

viorel
viorel

its only return the image links so i need urgent help you do a great job with this script is what i need to go next with my project

@viorel I'll do what I can :). I'm not sure I follow what your issue is, though.

Leave A Comment