Jump to content

Google Images Grabber - need help


jsschmitt

Recommended Posts

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

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 ;)

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.