jsschmitt Posted July 6, 2009 Share Posted July 6, 2009 The following code works, except that it is always throwing a chunk of code into an image tag at the beginning. I can't figure out how to filter out that chunk of code. Try for yourself: http://anomymage.com/tests/do/search/ <form name="input" action="./" method="get"> Search: <input type="text" name="string" /> <input type="Submit" value="Go" /> </form> <?php function google_image ($q) { $q_filter = "*.*"; $q = str_replace ( " " , "+", $q ); $get_data = file_get_contents ("http://images.google.com/images?q=".$q . "+" . $q_filter . "&imgsz=small|medium|large|xlarge&hl=en&sa=G&safe=off&gbv=1"); $exp1 = explode ( "http://tbn", $get_data ); $images = array(); foreach ($exp1 as $exp) { $exp2 = explode ( "width", $exp ); $images[] = "http://tbn" . $exp2[0]; } return $images; } $search = $_GET['string']; $images = google_image ($search); foreach ($images as $image) { echo "<img src='$image'>\n"; } ?> Also, if anyone has any idea on how to increase the number of images shown, that would be fantastic ^.^ Link to comment https://forums.phpfreaks.com/topic/164933-google-images-grabber-need-help/ Share on other sites More sharing options...
shergold Posted July 6, 2009 Share Posted July 6, 2009 sorry that this is off topic but please check your pms. thanks, shergold. Link to comment https://forums.phpfreaks.com/topic/164933-google-images-grabber-need-help/#findComment-869744 Share on other sites More sharing options...
jsschmitt Posted July 10, 2009 Author Share Posted July 10, 2009 No one knows anything? Link to comment https://forums.phpfreaks.com/topic/164933-google-images-grabber-need-help/#findComment-873118 Share on other sites More sharing options...
thebadbad Posted July 10, 2009 Share Posted July 10, 2009 Firstly, the link you posted doesn't run that script. Where does the "Full Size" link text come from? And I have absolutely no idea how exploding at "width" to get the last part of the image URLs could work. I would grab the images with something like this: <?php function google_images($q, $page) { $q = urlencode($q); $start = ($page - 1) * 21; $get_data = file_get_contents("http://images.google.com/images?q=$q&hl=en&safe=off&imgsz=small|medium|large|xlarge&start=$start"); preg_match_all('~/imgres.+?"","([^:]+","([^"]+)".+?(http://tbn[^"]+)"~is', $get_data, $matches, PREG_SET_ORDER); $images = array(); foreach ($matches as $data) { $images[] = array( 'thumb' => $data[3] . '?q=tbn:' . $data[1] . $data[2], 'original' => $data[2] ); } return $images; } echo '<pre>' . print_r(google_images('test', 1), true) . '</pre>'; ?> I added a page variable to the function, so you're not stuck with the images on page one only. You know it's only searching medium sized images, right? And my God, Google's source code is f**ked up. Didn't keep me from grabbing the URLs though Link to comment https://forums.phpfreaks.com/topic/164933-google-images-grabber-need-help/#findComment-873182 Share on other sites More sharing options...
Mchl Posted July 11, 2009 Share Posted July 11, 2009 Don't know about Images, but on Maps they're pretty good at blocking bots. Link to comment https://forums.phpfreaks.com/topic/164933-google-images-grabber-need-help/#findComment-873426 Share on other sites More sharing options...
jsschmitt Posted July 13, 2009 Author Share Posted July 13, 2009 Firstly, the link you posted doesn't run that script. Where does the "Full Size" link text come from? And I have absolutely no idea how exploding at "width" to get the last part of the image URLs could work. I would grab the images with something like this: <?php function google_images($q, $page) { $q = urlencode($q); $start = ($page - 1) * 21; $get_data = file_get_contents("http://images.google.com/images?q=$q&hl=en&safe=off&imgsz=small|medium|large|xlarge&start=$start"); preg_match_all('~/imgres.+?"","([^:]+","([^"]+)".+?(http://tbn[^"]+)"~is', $get_data, $matches, PREG_SET_ORDER); $images = array(); foreach ($matches as $data) { $images[] = array( 'thumb' => $data[3] . '?q=tbn:' . $data[1] . $data[2], 'original' => $data[2] ); } return $images; } echo '<pre>' . print_r(google_images('test', 1), true) . '</pre>'; ?> I added a page variable to the function, so you're not stuck with the images on page one only. You know it's only searching medium sized images, right? And my God, Google's source code is f**ked up. Didn't keep me from grabbing the URLs though Ok... to show my noob-ness. How would I go about echoing out each portion of the array? Link to comment https://forums.phpfreaks.com/topic/164933-google-images-grabber-need-help/#findComment-874637 Share on other sites More sharing options...
jsschmitt Posted July 13, 2009 Author Share Posted July 13, 2009 Wait... figured it out ^.^ Link to comment https://forums.phpfreaks.com/topic/164933-google-images-grabber-need-help/#findComment-874676 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.