Lightboxing and Flickr
2006.07.13 10:01 AM
704 words

TOPIC(S)
Lightboxing and Flickr
July 13, 2006 10:01 AM

NOTICE: CONTENTS NO LONGER APPLICABLE

The content discussed in this post is no longer valid; as I’m now using a different “lightbox” implementation.
More here

JC made me check out his installation of a cool Wordpress plugin that does lightboxing. I decided to apply the same stuff in my blog - and modify it a bit to be able to link to the image’s Flickr page somewhat dynamically.

Results

If you’re not interested in the details, then just spot the difference between these two lightbox-enabled links (I just hope it does work as I intended it to).

Here’s an image which I linked to Flickr

Here’s an image which I linked to the Gallery within my domian

LINK REMOVED (as the old gallery doesn’t exist anymore)

If you noticed the difference, and are interested in how I did that - read on…

Lightbox 2.0

My first concern was that the lightbox script might require that all your files are locally hosted (meaning coming from the same domain - your/my domain). Of course, given that I try to host my blog images in Flickr now - that certainly will not do. To cut a long story short, it can load hotlinked images - and naturally my next instinct was to incorporate my Flickr linking into the script. Which required a bit of hacking into the js file.

Basically, the script (or collection of scripts to be more precise) checks out all <a> tags in a given page and checks if it has an attribute rel="lightbox" - which is the trigger that it is a lightbox-enabled anchor. You’d normally use this with images, so the script is built to load images.

so you have code like:

<a rel="lightbox" href="your_BIG_image_here" title="optional_caption_here">
    <img src="your_SMALL_image_here" alt="optional_data" />
</a>

Which it will then create the lighbox instance for and grabs some data into the array. The one I paid attention to was imageArray[activeImage][0] - which was the URL of the [BIG] image/or the URL in your lightbox anchor.

Next was simply just to inject code that does this:

  1. check the URL, and strip it off the extraneous data, leaving the Flickr ID alone, then reinserting it into a newly formed URL string.
  2. Add a link in the image caption to redirect to the images Flickr page.
  3. Do not do any of this when the URL isn’t from Flickr.

It’s a simple enough task, the only problem is the parsing of the URL, matching a flickr URL, and converting it to the standard Flickr photo page image URL. The Flickr image above uses http://static.flickr.com/54/182121733_f8659ad49c_o.png - which had to be converted to http://www.flickr.com/photos/nargalzius/182121733. The answer was to use regular expressions.

For #3, I used your typical if/else to match a regular expression as true or false. I used /(.+flickr.+)/i which basically meant a case insensitive match which had the word “flickr” between any one or more characters - which obviously solved the detection of a flickr URL. If that tested as true, it then gets that URL and does a replace function using the JavaScript syntax:

imageArray[activeImage][0].replace(/(http:\/\/static.flickr.com\/.+\/)(\d+)(.+)/g, "http://www.flickr.com/photos/nargalzius/$2");

Which meant:

  1. Take the first value from the array (which is http://static.flickr.com/54/182121733_f8659ad49c_o.png)
  2. Match it to /(http:\/\/static.flickr.com\/.+\/)(\d+)(.+)/g - which further meant
    • Match in groups: first group matches http://www.static.flickr.com/, then any one or more characters which ending with a forward slash. This will incidentally match: http://static.flickr.com/54/ as group 1.
    • Second group matches any one or more digits - which will match 182121733
    • Lastly, match any one or more characters as the third group _f8659ad49c_o.png (just to be safe and sure that underscore was delimited properly)
  3. Take that/those match(es) and replace them with http://www.flickr.com/photos/nargalzius/ plus the data from the second group.

And voila! you have an instant JavaScript match and replace of any valid Flickr hotlinking URL - which is displayed in the caption. In case you haven’t noticed it yet, the difference between those two links are: for the Flickr, you should see a link at the bottom left of the image… to its respective Flickr page.


post updated on August 2, 2008 @ 01:21PM

4 comments
post a comment email JC at July 13, 2006 10:21 AM, JC said:

Flickr also has a policy that requires a linkback to the page hosting the picture as part of its terms of service. That's a pretty clever workaround!

post a comment email webmaster at July 13, 2006 12:18 PM, webmaster said:

It's funny their terms of service never crossed my mind - yet I still put linkbacks regardless. Even my older non-lighbox posts actually link to the images' respective Flickr pages - I guess it's all thanks to their "notes" feature hehehe.

post a comment email tom at January 30, 2007 07:04 PM, tom said:

did you manage to find a code to feed the lightbox with an image set on flickr, and not just a single picture?

i'm really a newbee to this...

tx, Tom

post a comment email Carlo at March 7, 2007 12:49 AM, Carlo said:

Sorry Tom, I don't think that's even possible. There's no way you can deduce an images' relation to a "set" in Flickr by just looking at its URL - which is pretty much the only information the Lightbox will get to work with.

Same goes when you work with a set's URL, there's no way of deducing from that URL what images fall into it - unless you rig the Lightbox to the Flickr API some way.

Same day, different year
1551 words
We’re headed for Singapore this weekend. I’ll be back on Monday. It’s a bummer I don’t have a camera with me since I plan to...
325 words
According to a slashdot article, a french company released the Jackito, which, I quote: “…new kind of PDA has no stylus, doesn’t offer handwriting recognition...
283 words
Well, my sister’s home, and I got all my shit. Whoopee! Bag Let’s kick-off with the stuff I had purchased for my Mentor. The Mentor’s...
Probably Related
604 words
… and then my heart went boom! Remember that song? Well, it doesn’t really have anything to do with this post apart from the reference...
936 words
Well, it’s not so much of a hack… or is it? By definition, it actually is - yet judging from how people like to define...
920 words
*POST UPDATED FOR 1.1.1 While a 1.1.1 unlock is just around the corner, I thought I would share some “1.0.2 hacks” I’ve collected over the...
427 words
It’s really amazing, jaw-droppingly so, how people who have hacked/unlocked iPhones still go through with the 1.1.1 update even after Apple explicitly told them that...
942 words
It’s been a long while since I last posted anything - doing NYC hours for work can really take its toll on you. But...
38 words
I decided to redo my Moleskine pen holder hack this time with black duct tape - which I mentioned would probably look better. Sure enough...
973 words
One of the problems a notebook owner will face is the fact that they will not only have to lug notebook all around, all the...
637 words
I noticed that I’ve been getting erratic behaviour with my router whenever I had bittorrent running. After a while it would always slow down...