Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- V1.3.1 // Updated the script to accommodate flickrs new API change. This now uses SSL.
- V1.3 // I added the option for the user to specify the resolution the picture received should be. This uses
- Flikrs URL suffixes, so to determine what suffix you want, go here: https://www.flickr.com/services/api/misc.urls.html
- Then edit the size_suffix variable. As a note, the file type can be .jpg .png or .gif. It has to be the original format tho.
- V1.2 // I really should test my updates before posting them here... It works now. Yes I tested it :P
- V1.1 // Script no longer pops up with alerts boxes, and random, but not the same feature now works.
- V1.0 // Posted, worked sorta, but forgot to remove alert() statements, and in case of the same images were picked, it would miss an image.
- Flickr image puller.
- This script pulls random images from a flickr account, and changes the src of any
- image tag, with the class flickr-img, into the src of the randomly picked image.
- The script also sets the alt tag to the name of the picture, to be HTML compliant.
- The script also won't allow the same image to be picked twice, unless there's
- no more pictures to pick from.
- Made by Mikkel Jeppesen, use it as you please, but please leave credit :)
- */
- /*
- The user ID can be found by going to the photostream of the user
- you want to pull the images from, and checking the url.
- It's your responsibility to have permission to use the images you use
- */
- var user_id = 'xxxxxxxxx@yxx';
- //You can get an api key here: http://www.flickr.com/services/apps/create/apply
- var api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
- var size_suffix = 'z.jpg'; //To change the size of the returned image. See header changelog v1.3 for more info.
- function getRandomInt (minInt, maxInt, amount, maxAvail)
- {
- var randArr = new Array();
- randArr[0] = 0;
- var r = 0;
- var e = 0;
- var redo = false;
- for(var i=0; i<amount; i++)
- {
- r = Math.floor(Math.random() * (maxInt - minInt + 1)) + minInt;
- for(var p=0; p<randArr.length; p++)
- {
- if(r == randArr[p] && randArr.length <= maxAvail)
- {
- redo = true;
- }
- }
- if(redo)
- {
- i--;
- }
- else
- {
- randArr[i] = r;
- }
- }
- return randArr
- }
- function jsonFlickrApi(rsp)
- {
- if (rsp.stat != 'ok')
- {
- return;
- }
- var img_url = '';
- var imgAlt = '';
- var imgCount = rsp.photos.photo.length;
- var elmCount = $('.flickr-img').length;
- var imgIndexArr = getRandomInt(0,imgCount,elmCount, imgCount);
- $('.flickr-img').each(function(i)
- {
- img_url = 'http://farm' + rsp.photos.photo[imgIndexArr[i]].farm + '.static.flickr.com/' + rsp.photos.photo[imgIndexArr[i]].server + '/' + rsp.photos.photo[imgIndexArr[i]].id + '_' +rsp.photos.photo[imgIndexArr[i]].secret + '_' + size_suffix;
- imgAlt = rsp.photos.photo[imgIndexArr[i]].title;
- $(this).attr('src',img_url);
- $(this).attr('alt',imgAlt);
- })
- }
- $(document).ready(function()
- {
- $.getScript('https://api.flickr.com/services/rest/?format=json&sort=random&method=flickr.people.getPhotos&api_key='+api_key+'&user_id='+user_id)
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement