Skip to content

Instantly share code, notes, and snippets.

@bpholt
Created November 7, 2013 01:50
Show Gist options
  • Select an option

  • Save bpholt/7347593 to your computer and use it in GitHub Desktop.

Select an option

Save bpholt/7347593 to your computer and use it in GitHub Desktop.
Gallery2 Album to HiDPI <img>s
(function (albumID, $) {
'use strict';
$.extend({
interpolate: function (s, data) {
var variable;
for (variable in data) {
if (data.hasOwnProperty(variable)) {
s = s.replace(new RegExp('{' + variable + '}', 'g'), data[variable]);
}
}
return s;
}
});
function handleGallery2Response(data) {
var lines = data.split('\n'),
pattern = /^image\.(.+)\.(\d+)$/,
template = '<a href="/gallery2/d/{name}-2/{title}" title="{description}" rel="lightbox[g2image]"><img src="/gallery2/d/{resized.2.name}-2/{title}" srcset="/gallery2/d/{resized.2.name}-2/{title} 1x, /gallery2/d/{resized.1.name}-2/{title} 2x" alt="{caption}" width="{resized.2.width}" height="{resized.2.height}"/></a>',
images = [];
$.each(lines, function () {
var keyVal = this.split('='),
parsedKey,
fieldName,
imageIndex,
image;
if (pattern.test(keyVal[0])) {
parsedKey = pattern.exec(keyVal[0]);
fieldName = parsedKey[1];
imageIndex = parsedKey[2] - 1;
image = images[imageIndex] || {};
image[fieldName] = keyVal[1];
images[imageIndex] = image;
}
});
$.each(images, function (i) {
if (this['extrafield.Description']) {
this.description = this['extrafield.Description'];
} else {
this.description = this.caption;
}
if (!this['resized.1.name']) {
console.warn($.interpolate('{title} with ID {name} is missing HiDPI resize', this));
}
if (!this['resized.2.name']) {
console.warn($.interpolate('{title} with ID {name} is missing default resize', this));
}
images[i] = this;
});
$.each(images, function () {
console.log($.interpolate(template, this));
});
}
$.post('/gallery2/main.php?g2_controller=remote:GalleryRemote', {
'g2_form[cmd]': 'fetch-album-images',
'g2_form[protocol_version]': '2.4',
'g2_form[set_albumName]': albumID,
'g2_form[albums_too]': 'no',
'g2_form[random]': 'no',
'g2_form[extrafields]': 'yes',
'g2_form[all_sizes]': 'yes'
}, handleGallery2Response);
}(6985, jQuery));
@bpholt
Copy link
Author

bpholt commented Nov 7, 2013

Gallery2 has a terrible HTTP API. /sigh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment