flic.kr - Generate Flickr shortened URL


/ Published in: JavaScript
Save to your folder(s)

A short photo id is a base58 conversion of the photo id. Base58 is like base62 [0-9a-zA-Z] with some characters removed to make it less confusing when printed. (namely 0, O, I, and l).

So that leaves an alphabet of: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ


Copy this code and paste it in your HTML
  1. function flic_kr(aPhotoId) { // aPhotoId argument is Flickr photo id
  2. if (typeof aPhotoId !== 'number') {
  3. aPhotoId = parseInt(aPhotoId);
  4. }
  5. var enc='', alpha='123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
  6. var div = aPhotoId, mod;
  7. while (aPhotoId >= 58) {
  8. div = aPhotoId / 58;
  9. mod = aPhotoId - (58 * Math.floor(div));
  10. enc = '' + alpha.substr(mod, 1) + enc;
  11. aPhotoId = Math.floor(div);
  12. }
  13. return shortURL = "http://flic.kr/p/" + (div ? '' + alpha.substr(div, 1) + enc : enc);
  14. }

URL: http://www.flickr.com/groups/api/discuss/72157616713786392/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.