nBLOG

2006.07.13 10:01 AM
681 words

TOPIC(S)
Lightboxing and Flickr
2006.07.13 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 December 1, 2008 @ 7:49 am

4 Responses
post a comment Email author 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 author 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 author at January 30, 2007 7: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 author 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.

Probably Related
910 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 during…
939 words
I decided to maximize the use of Flickr as an image repository specifically for my blog. While it would be nice to just move the…
501 words
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 When…
598 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…
258 words
I went back to the music section, and redid the AJAX code I used. I remembered the Prototype library (prototype.js) I was using for the…
Same day, different year
563 words
So while the world is in solidarity as far as the-loss-we-feel-of-losing-a-great-muscian goes what I find revolting is this whole issue about the financial aftermath of…
366 words
If you haven’t seen this movie… I suggest you get your asses of your computer monitors and watch it. I absolutely loved this movie. In…
1527 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…
315 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…
251 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…