Screen Capture Software

It is a very convenient tool for bloggers. When you write posts on your blog you need to capture and crop images from different sources - this tool is for you. Download Screen Capture Software

Sunday, August 30, 2009

Get Dynamic Thumbnail for you site

I implemented a new feature over at my Jenny McCarthy and Hayden Panettiere websites this past week.

I've been posting YouTube videos on both sites for quite some time, but have only been providing text descriptions to go along with the links from the main video pages. I finally decided that I wanted to figure out how to dynamically include thumbnail image previews of the clips, not only to display on the main video pages, but also in web feeds where applicable.

First I got all fancy and was sending in YouTube API requests to get at the information. The youtube.videos.get_details function provides thumbnail information in its node.

However, once I had implemented a preliminary approach using that method, I quickly realized there was a very obvious pattern to the thumbnail URLs being returned with each call:

http://img.youtube.com/vi/LiIboq6XCOg/2.jpg

The only portion of the above URL that seems to change is the video ID between the fourth and fifth forward slashes.

So rather than calling the API multiple times to get thumbnails for all the videos presented on the page, I decided to keep all function calls local, opting to create my own internal function for extracting the YouTube video ID from my XHTML-compliant embed code using a regular expression and then inserting that between the http://img.youtube.com/vi/ and /2.jpg portions of the static thumbnail URL.

The result is much faster and works like a charm.

No comments: