During my work I got confronted with Wordle. A colleague used Wordle to show what a specific website was about. I got interested in how these beautiful word clouds were made and if I could add them to my own website.

Regrettably, the Wordle algorithms cannot be shared by the author because of intellectual property reasons (IBM owns his work). Given the popularity of Wordle, I found it strange that no WordPress plugin existed that would generate similar word clouds. Searching the web for similar algorithms didn’t provide me with much answers either. But hey, how hard can it be? I decided to have a go at it myself.

What’s a word cloud? Basically it is a set of words fitted closely together in a oval shape. The words are of different sizes depending on their frequency in the corresponding text. Furthermore, the words may be of different colors and have different angles. I quickly decided I would support two angles only: horizontal and vertical words. The words would be different in size, and also the colors would match the frequency.

The algorithm is then relatively straightforward:

  1. calculate the font-size and color depending on the frequency of the words.
  2. calculate the size of the boxes that will contain the words. In PHP that is easily done using the GD library
  3. place the first box at coordinate (0,0).
  4. Add the coordinates of the box corners to a list
  5. Sort the list of corners based on their eucledian distance to the origin (0,0), keeping in mind the wanted oval shape of the cloud
  6. Place the next box to the first corner coordinate where the box doesn’t overlap already placed boxes
  7. Add the corners of the newly placed boc to the coordinates list
  8. Continue until all boxes are placed

Given this algorithm, create a few classes (Point, Box, Cloud, Word and WordCloud) that implement all this. Add some image generation code and caching of the results (it does take some time to create and position all the words) and you’re done! To integrate it with WordPress I used one of the tag cloud hooks (if you’re looking for hooks in WordPress Adam R. Brown keeps a great hooks database) . This hook provided me with all the information I needed: the tags, the links to those tags, the frequency numbers, etc. Furthermore, the wp_tag_cloud functions already support multiple formats so adding a new one was quite trivial. As a finishing touch I added a shortcode, so I didn’t need to go into a theme to change the tag cloud arguments to get my image cloud shown.

The plugin can be found on the WordPress extend site: http://wordpress.org/extend/plugins/imagecloud/

Two example can be found here and here.

It was fun creating this plugin. Hopefully you like the results. Feedback is always welcome!