Use javascript to get a random image from Google images
Asked Answered
S

2

25

I have this idea for my website that every time you visit the page, the background will be different. I want to get literally any picture from Google images and place it as my website's background using Javascript.

Basically every time you refresh the page a script will fetch a random picture from Google images and place it as the background or maybe at least download the picture.

How should I do this, or is it even possible?

Strongbox answered 16/9, 2015 at 18:21 Comment(6)
That is not particularly possible.Populace
You could use a service that does this for you such as lorempixel, but I'm not sure if this fits your needs. Although, getting a truly random picture from Google Images seems more than a little risky, given the amount of pictures on there that aren't fit for website backgrounds.Hanford
You are going to need to use the Google Image Search API to query images, then you can get a random result form the JSON data. Note it is deprecated, but will still work. You can also use their newer Custom Search API.Humphreys
There is always the Flikr API, from what I've read it's not bad too, all high resolution. One issue you will run into with your site is performance. Since it's random you won't have the ability to do any image processing so having a 10MB won't be unheard of.Lucero
Youd be wayyyyy better off getting a collection of images you like and hosting them on your domain. Then it becomes very VERY easy to pick a random one and display itMuckraker
I know but it would not be random then, the point is to make it random.Strongbox
M
31

It can be done via Google Images though much customization is required so the script behaves as you intended (including setting up a delay to handle rate-limiting; Google has a rate-limit of 64 items per search request over API) here is a basic example using Google Images api:

<html>
<head>
    <title></title>
    <script src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
    google.load('search', '1');
    google.setOnLoadCallback(OnLoad);
    var search;

    //i suggest instead of this to make keywords list so first to pick random keyword than to do search and pick random image
    var keyword = 'mountains';

    function OnLoad()
    {
        search = new google.search.ImageSearch();

        search.setSearchCompleteCallback(this, searchComplete, null);

        search.execute(keyword);
    }

    function searchComplete()
    {
        if (search.results && search.results.length > 0)
        {
            var rnd = Math.floor(Math.random() * search.results.length);

            //you will probably use jQuery and something like: $('body').css('background-image', "url('" + search.results[rnd]['url'] + "')");
            document.body.style.backgroundImage = "url('" + search.results[rnd]['url'] + "')";
        }
    }
    </script>
</head>
<body>

</body>
</html>

However may I suggest instead: Random images from flickr, here is another basic code for that (sky is the limit):

<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">

    var keyword = "mountains";

    $(document).ready(function(){

        $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
        {
            tags: keyword,
            tagmode: "any",
            format: "json"
        },
        function(data) {
            var rnd = Math.floor(Math.random() * data.items.length);

            var image_src = data.items[rnd]['media']['m'].replace("_m", "_b");

            $('body').css('background-image', "url('" + image_src + "')");

        });

    });
    </script>
</head>
<body>

</body>
</html>
Madge answered 16/9, 2015 at 19:11 Comment(3)
No longer working, because responsed "This API is no longer available." message.Arrington
It doesn't work anymore -> "Module "search" is not supported."Clynes
Both aren't working anymore, yes. Can anybody suggest working on either one - Google or Flickr API?Vital
S
2

although not technically what was asked, this could help give some structure to the randomness -- you could compose a couple dictionaries, of verbs, nouns, adjectives.. and mad-lib it up with a random adjective-noun verbing, (ie, fat bulldog running) then query google with that search, and pick a random picture from the results. this way, you can potentially reduce the inappropriate results, and also allow selection of specific dictionaries based on themes the user has selected perhaps. (ie, changing the available nouns based on user's likes)

Schizogenesis answered 30/1, 2019 at 1:36 Comment(2)
Consider adding more info about your answer.Vlf
This is a good idea, in general -- generate some random words (somehow) and search on that. Gives you some control over the randomness.Switch

© 2022 - 2024 — McMap. All rights reserved.