-
Notifications
You must be signed in to change notification settings - Fork 3
/
load-screenshot.js
125 lines (115 loc) · 4.07 KB
/
load-screenshot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function photoIDtoShortURL(id) {
function intval (mixed_var, base) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: stensi
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Matteo
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: intval('Kevin van Zonneveld');
// * returns 1: 0
// * example 2: intval(4.2);
// * returns 2: 4
// * example 3: intval(42, 8);
// * returns 3: 42
// * example 4: intval('09');
// * returns 4: 9
// * example 5: intval('1e', 16);
// * returns 5: 30
var tmp;
var type = typeof( mixed_var );
if (type === 'boolean') {
return (mixed_var) ? 1 : 0;
} else if (type === 'string') {
tmp = parseInt(mixed_var, base || 10);
return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
} else if (type === 'number' && isFinite(mixed_var) ) {
return Math.floor(mixed_var);
} else {
return 0;
}
}
function base_encode(num, alphabet) {
// http://tylersticka.com/
// Based on the Flickr PHP snippet:
// http://www.flickr.com/groups/api/discuss/72157616713786392/
alphabet = alphabet || '123456789abcdefghijkmnopqrstuvwxyz' +
'ABCDEFGHJKLMNPQRSTUVWXYZ';
var base_count = alphabet.length;
var encoded = '';
while (num >= base_count) {
var div = num/base_count;
var mod = (num-(base_count*intval(div)));
encoded = alphabet.charAt(mod) + encoded;
num = intval(div);
}
if (num) encoded = alphabet.charAt(num) + encoded;
return encoded;
}
return 'http://flic.kr/p/' + base_encode(id);
};
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return null;
}
function scaleScreenshotToFitOnPage(img) {
// This page was designed to print the "Failbook" hack,
// whose dimensions are 1024x579.
var idealAspect = 1024 / 579;
var height = img.height();
var width = img.width();
var aspect = width / height;
if (aspect < idealAspect) {
// Here we're solving for 'scale' in the following equation:
// width / (height * scale) = idealAspect
var scale = Math.floor((width / (idealAspect * height)) * 100);
img.css({width: scale + "%"});
}
}
$(window).ready(function() {
var photo_id = getQueryVariable("photo_id");
if (!photo_id) {
$("form").submit(function() {
var re = /http:\/\/www.flickr.com\/photos\/.*\/([0-9]+)/;
var match = $("input#url").val().match(re);
if (match) {
window.location = window.location.href + "?photo_id=" + match[1];
} else {
alert("I don't recognize that as a valid Flickr URL.");
}
return false;
});
$("form").fadeIn();
return;
}
var url = "http://api.flickr.com/services/rest/?" +
"method=flickr.photos.getInfo&" +
"api_key=e59b6a956fe1cdf4dad125cb3c1c1321&" +
"photo_id=" + photo_id + "&format=json&nojsoncallback=1";
jQuery.getJSON(url, function(obj) {
var photo = obj.photo;
var photoURL = "http://farm" + photo.farm + ".static.flickr.com/" +
photo.server + "/" + photo.id + "_" + photo.secret +
"_b.jpg";
var title = photo.title._content;
var desc = photo.description._content.replace(/\n/g, '<br>');
var flickrURL = photo.urls.url[0]._content;
var shortURL = photoIDtoShortURL(photo_id);
document.title = title;
$("div#header h1.title").text(title);
$("div#body .description").html(desc);
$("a#flickr_link").attr("href", flickrURL);
$("a#shorturl").attr("href", shortURL).text(shortURL.slice(7));
$("img#screenshot").attr("src", photoURL).load(function() {
$("#container").fadeIn();
scaleScreenshotToFitOnPage($(this));
});
});
});