PHP Google Image Search Library
Code: [ Download ]
Demo: [ Try it out ]
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
- include ('gis.php');
- $results = googleImageSearch('chobits');
- print_r($results);
- ?>
Here's the function prototype:
Code: php
- 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!
Comments
Hi ,iam unable to download the gis.php file . please help me
Reply@irphan Fixed the link. Sorry about that!
Replyhi 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]
ReplyI would use a foreach loop, like so:
foreach ($results as $result) {
echo $result->source;
}
Hope that helps ya!
ReplyHow to increase the image count in search
ReplyGoogle 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.
ReplyGreat jobs
ReplyHi!
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?
ReplyInteresting. What was your search query?
ReplyHi, do you know how to display the picture? Currently, its all in text form.
Thanks
Reply@thger Could you elaborate? You are writing the url into an image tag, yes? (unless you're doing something different entirely)
Replyhi, 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 ()
ReplyHi, 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.
ReplyHi,
your demo does,t work.
Can you please tell me how It works?
And how your script run?
thanks
Reply@shipar Sure enough. Looks like Google changed up their code (always an issue with writing scrapers). I'll have to rework the library.
Reply"Google changed up" their code for Images? so we have to find out [smile]
ReplyHi 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
ReplyHi 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
Reply@jnana Google has since changed their code and my library no longer works. Such is life when messing with scrapers.
Reply