Retrieve a Random Image URL From Google With PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Poulton
    New Member
    • Apr 2011
    • 1

    #1

    Retrieve a Random Image URL From Google With PHP

    A recent project involved me obtaining a random image from the Web and using it in some specific fashion. I chose to use PHP to access Google Images for a random image URL. The image could be generic or related to a specific topic.

    Here's the PHP function that queries Google Images and returns an image URL:
    Code:
    function GetRandomImageURL($topic='', $min=0, $max=100)
    {
      // get random image from Google
      if ($topic=='') $topic='image';
      $ofs=mt_rand($min, $max);
      $geturl='http://www.google.ca/images?q=' . $topic . '&start=' . $ofs . '&gbv=1';
      $data=file_get_contents($geturl);
    	
      $f1='<div id="center_col">';
      $f2='<a href="/imgres?imgurl=';
      $f3='&amp;imgrefurl=';
    	
      $pos1=strpos($data, $f1)+strlen($f1);
      if ($pos1==FALSE) return FALSE;
      $pos2=strpos($data, $f2, $pos1)+strlen($f2);
      if ($pos2==FALSE) return FALSE;
      $pos3=strpos($data, $f3, $pos2);
      if ($pos3==FALSE) return FALSE;
      return substr($data, $pos2, $pos3-$pos2);
    }
    This function generates a random number, sends that off as a "start" parameter with an image query to Google, and grabs the HTML code generated. The "gbv=1" parameter is required - without it, Google will return a generic page instead of the image results.

    If any markers ($f1, $f2 or $f3) are not found, the function will return FALSE. Otherwise, the first image (from the "start" parameter) is parsed, and the corresponding URL is retrieved.

    Note that Google may alter its HTML along the way, making this function obsolete. Also, large "start" values seem to exceed the number of images Google is willing to display. At the time of this writing, the maximum was about 980.

    The $topic parameter allows the option of specifying a certain type of image. Pass 'vehicle' and an image of a vehicle is the target; pass 'balloon', and.. well, you get the idea. Note that this isn't guaranteed to focus on a topic, as someone may have posted a picture with 'balloon' as an image tag but without any balloons in the picture.

    Although any randomiser function could be used, mt_rand() is used here due to its speed and reasonable randomness.

    This example function demonstrates the use of the above GetRandomImageU RL() function:
    Code:
    function ShowRandomImage($topic='')
    {
      echo('<table border="1"><tr><td>');
      $url=GetRandomImageURL($topic);
      if ($url==FALSE) echo('Error while searching');
      else {
        echo(htmlentities($url) . '<br />');
        echo('<img width="640" src="' . $url . '" />');
      }
      echo('</td></tr></table>');
    }
    
    ShowRandomImage();
    ShowRandomImage('rothschild');
    Enjoy!
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    Although I haven't tested it, it looks pretty cool.

    Comment

    • makakee
      New Member
      • Dec 2013
      • 1

      #3
      Hmmm, this doesn't seem to work anymore. Any ideas why?

      Comment

      Working...