加载一个随机flickr图像&附加到div

时间:2012-03-16 15:20:21

标签: jquery json image flickr

我基本上试图加载一个从特定用户和特定集合中获取的随机flickr图像,然后将其显示在id为“flickr-wrap”的div中。我正在尝试操纵这个JSON代码来做我想要的但却没有线索从哪里开始。此代码目前加载了大量图片(我只想加载一个)并使用标签(但我想要用户和设置),有人可以帮我解决吗?

 $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",{
    id: "51997044@N03",
    tagmode: "any", 
    format: "json" },
  function(data) {
    $("<img/>").attr({src: data.items[0].media.m.replace('_m.','.')}).appendTo("#flickr-wrap");
  });

修改

我已经停止了很好的循环,现在已经更新了上面的代码来提取照片set.gne而不是public.gne,并通过删除一些代码行稍微改变了我在photoset中调用的方式。 现在我需要做的就是从该集中提取随机图像。以下是我到目前为止所做的事情:

$.getJSON("http://api.flickr.com/services/feeds/photoset.gne?set=72157626230243906&nsid=51997044@N03&lang=en-us&format=json&jsoncallback=?",
  function(data) {
    $("<img/>").attr({src: data.items[0].media.m.replace('_m.','.')}).appendTo("#flickr-wrap");
  });

修改

还没有随机的工作呢。最烦人的。真的可以在这里使用一些帮助。绝望!

5 个答案:

答案 0 :(得分:3)

data.items是图像的数组,所以只需获取第一个图像,不要遍历数组。

而不是

$.each(data.items, function(i,item){...}

DO

$("<img/>").attr({src: data.items[0].media.m.replace('_m.','.')}).appendTo("#flickr-wrap");

答案 1 :(得分:1)

我在上面的回答中注意到了几个拼写错误。这是一个纠正了错别字的代码,还有一些小的改动。


    function getPicture(the_user_id, your_div_id){
        var apiKey = "YOUR-API-KEY"; // replace this with your API key

        // get an array of random photos
        $.getJSON(
            "http://api.flickr.com/services/rest/",
            {
                method: 'flickr.interestingness.getList',
                api_key: apiKey,
                format: 'json',
                nojsoncallback: 1,
                per_page: 10 // you can increase this to get a bigger array
            },
            function(data){

                // if everything went good
                if(data.stat == 'ok'){
                    // get a random id from the array
                    var photo = data.photos.photo[ Math.floor( Math.random() * data.photos.photo.length ) ];

                    // now call the flickr API and get the picture with a nice size
                    $.getJSON(
                        "http://api.flickr.com/services/rest/",
                        {
                            method: 'flickr.photos.getSizes',
                            api_key: apiKey,
                            photo_id: photo.id,
                            format: 'json',
                            nojsoncallback: 1
                        },
                        function(response){
                            if(response.stat == 'ok'){
                                var the_url = response.sizes.size[5].source;
                                $("#"+your_div_id).append("");
                            }
                            else{
                                console.log(" The request to get the picture was not good :\ ")
                            }
                        }
                    );

                }
                else{
                    console.log(" The request to get the array was not good :( ");
                }
            }
        );
    };

答案 2 :(得分:1)

这是一个更新的小提琴+示例,允许您通过标记获取随机图像(我需要这个并认为它可能有帮助)

http://jsfiddle.net/6gY83/4/

完整示例:

function getPicture(tags, cb) {
    var apiKey = "fa214b1215cd1a537018cfbdfa7fb9a6"; // replace this with your API key

    // get an array of random photos
    $.getJSON(
        "https://api.flickr.com/services/rest/?jsoncallback=?", {
            method: 'flickr.photos.search',
            tags: tags,
            api_key: apiKey,
            format: 'json',
            per_page: 10 // you can increase this to get a bigger array
        },
        function(data) {

            // if everything went good
            if (data.stat == 'ok') {
                // get a random id from the array
                var photo = data.photos.photo[Math.floor(Math.random() * data.photos.photo.length)];

                // now call the flickr API and get the picture with a nice size
                $.getJSON(
                    "https://api.flickr.com/services/rest/?jsoncallback=?", {
                        method: 'flickr.photos.getSizes',
                        api_key: apiKey,
                        photo_id: photo.id,
                        format: 'json'
                    },
                    function(response) {
                        if (response.stat == 'ok') {
                            var the_url = response.sizes.size[5].source;
                            cb(the_url);
                        } else {
                            console.log(" The request to get the picture was not good :\ ")
                        }
                    }
                );

            } else {
                console.log(" The request to get the array was not good :( ");
            }
        }
    );
};

像这样调用代码:

getPicture('<your_tag>', function(url) {
    $("#random").attr("src", url)
});

答案 3 :(得分:0)

您可能需要提取更大的图片,因为您需要here的API密钥。然后,你可以调用这个函数,就是这样:

function getPicture(the_user_id, your_div_id){
    var apiKey = "YOUR-API-KEY"; // replace this with your API key

    // get an array of random photos
    $.getJSON(
        "http://api.flickr.com/services/rest/",
        {
            method: 'flickr.people.getPublicPhotos',
            api_key: apiKey,
            user_id: the_user_id,
            format: 'json',
            nojsoncallback: 1,
            per_page: 10 // you can increase this to get a bigger array
        },
        function(data){

            // if everything went good
            if(data.stat == 'ok'){
                // get a random id from the array
                var photoId = data.photos.photo[ Math.floor( Math.random() * data.photos.photo.length ) ];

                // now call the flickr API and get the picture with a nice size
                $.getJSON(
                    "http://api.flickr.com/services/rest/",
                    {
                        method: 'flickr.photos.getSizes',
                        api_key: apiKey,
                        photo_id: photoId,
                        format: 'json',
                        nojsoncallback: 1
                    },
                    function(response){
                        if(response.stat == 'ok'){
                            var the_url = response.sizes.size[5].source;
                            $("#"+your_div_id).append('<img src="' + the_url + '" />');
                        }
                        else{
                            console.log(" The request to get the picture was not good :\ ")
                        }
                    }
                );

            }
            else{
                console.log(" The request to get the array was not good :( ");
            }
        }
    );
};

答案 4 :(得分:0)

最后,我不得不采取完全不同的方法。事实证明Flickr徽章API已更改,并且在我试图找出此问题的答案时增加了更多灵活性。它基本上完成了我现在需要的工作:http://www.flickr.com/badge.gne