Top Menu

There are two files with *.php extension that you need to retrieve a random image from Google Images and visually display it. However, you may also need to randomly show an image every few seconds on your main page, and an iframe will do the trick.

You will need a Google Image search file to include in your file. Download the gisearch.zip that contains the php file.

Once you downloaded that file, create a new file PHP file called images.php and add the standard HTML tags to it.

*Notice at line 4, the page refreshes every 10 seconds with a refresh meta tag.

What if you don’t want the entire page by itself and want to include it on your main page. It’s best to use an iframe to load your images.php page that auto refreshes every 10 seconds.

And that’s it. Now you have a random image from Google Images that you searched for and displayed on your main page.

About The Author

I write about my life! I would appreciate it if you comment and tell me how life is treating you? :)

10 Comments

  1. Good, I improve to repeat it recursively, sometimes the string retrieves and try to show an image that doesn’t exist or is forbidden hotlinking, so I improve retrieving image size and repeating the function if is empty or zero. Cheers

  2. Hi Mona. Thanks for this article. It was very helpful. But I can’t list the first 20 images. Can you help me
    ? Thank you again.

    • Hi,

      In the file gisearch.php, the function:

      function select_image($SEARCH) {
      // get the images from Google
      $IMAGES = $this->get_images($SEARCH);

      // select a random image
      $IMAGE = $IMAGES[rand(1, sizeof($IMAGES))]; ///this line

      // return the image
      return $IMAGE;
      }

      Change: $IMAGE = $IMAGES[rand(1, sizeof($IMAGES))] to $IMAGE = $IMAGES[rand(20, sizeof($IMAGES))]

      I think that will do the trick. I haven’t tested it, but that’s the only spot that will allow you to change the number of images retrieved.

      • I was surprised for answered quickly. 🙂 I’ve tried. Unfortunately, it did not work. Do you think there’s another alternative?

        • I love answering comments! I try to as soon as I get them. Forget the rand 20 in gisearch.php. Put it back to 1.
          I am sorry I should have double checked first and tested it before I advise you. When you display the image, let the new image be in a loop. I am not sure how great the browser can handle this, but try it and will solve the issue:


          < ?php include("gisearch.php"); for($h=0; $h<20; $h++){ $i = new gisearch("flowers"); //must be in the loop because you want a new image every time to display echo ' < img src="'. $i->get_link() .'" >';
          } ?>

          • This will not display the first 20 images, but random 20 images according to the function select_image($SEARCH) { … maybe the rand is not necessary if you want to always display in order from first.

          • I also edited the utf8 characters error in the image names.

            added

            // set UTF-8 coding
            $HTML = mb_convert_encoding($HTML, ‘HTML-ENTITIES’, “UTF-8”);

            code in gisearch.php. And index php content :

            include(“gisearch.php”);
            $query = $_GET[“q”];
            for ($x = 0; $x get_link();
            $imageType = strtolower(substr($imageLink,-4));
            if ($imageType == “.jpg” || $imageType == “.jpeg” || $imageType == “.png”) {
            echo ‘get_link() .'”/>’;
            echo ” . $i->get_link();
            echo ” . $i->get_source();
            echo ” . $i->get_title();
            }
            };

            I have uploaded the files for you in my web site : http://hafzullah.com/mona.zip

          • That is an amazing detailed solution! Thanks for sharing and I hope others will benefit from it too!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Close