Archive for the Category Tips

 
 

DWTF Open Source License




Scott Raymond’s “Ajax on Rails” contains a nifty trick for form fields, following the Rails mantra of DRY and convention over configuration. Creating a field with an associated label, error checking, and formatting is as simple as

  1.  
  2.    <%= f.text_field :phone %>
  3.  

This produces a label using the humanized “Phone: “, a <div> for holding validation errors, an input tag, and appropriate field values. Here’s a complete form using Scott’s hackery from page 298:

  1.  
  2. <%# See +standard_form+ in application_helper.rb %>
  3.   <%= f.text_field :name %>
  4.   <%= f.date_select :start_date, :label => "First Call" %>
  5.   <%= f.date_select :end_date, :label => "Last Call" %>
  6.   <%= f.text_field :phone %>
  7.   <%= f.text_field :email %>
  8.   <%= f.text_area :body, :label => "Notes" %>
  9.   <%= standard_submit %>
  10. <% end %>
  11.  

Scott’s magic is embodied in a form helper, LabelingFormBuilder. For yucks, I Googled in search of a Rails plugin by that name. I stumbled across labeling_form_builder from Seth Rasmussen. I peeked at the license. Seth cracked me up. Only in open source can you get away with this (words bleeped):

  1.  
  2.            DO WHAT THE F**K YOU WANT TO PUBLIC LICENSE
  3.                    Version 2, December 2004
  4.  
  5. Copyright (C) 2007-2008 Seth Thomas Rasmussen
  6.  
  7. Everyone is permitted to copy and distribute verbatim or modified
  8. copies of this license document, and changing it is allowed as long
  9. as the name is changed.
  10.  
  11.            DO WHAT THE F**K YOU WANT TO PUBLIC LICENSE
  12.   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  13.  
  14.  0. You just DO WHAT THE F**K YOU WANT TO.
  15.  

Animated GIFs and video thumbnails from YouTube videos

A few weeks ago I was in a meeting, and my CEO kept saying “just need more cowbell.” People would laugh. What the hell? I had no idea. I missed the memo.

Googling around I found the original skit. It made me want to clip out the little phrase, and keep it handy for sending around the office. One thing led to another and I wrote a minisite while commuting, Tube Chopper. It takes snapshots, avatars, mp3 clips from YouTube videos.

I whipped out my Mac and showed friends (in their 40’s) the original skit over wine and appetizers. Cheese almost came out of their noses.

Make your own Youtube clips. Any loss of productivity is totally intentional.

Fisheye effect in ImageMagick

A fisheye view of Obama

A fisheye view of Obama

ImageMagick is my favorite tool for messing around with images.  At PhotoBucket, we dedicated dozens of machines to “mogrify” uploaded images into thumbnails and appropriate sizes for user accounts.  I use it on my pet projects like an image zoomer and a youtube clipper.

Recently I discovered the “-fx” or special effects options of the ImageMagick toolset.  This was written in 1996 as probably a neat hack.  The code interprets a calculator-like FX language for each channel of every pixel.   This is painfully slow and screams for optimization.  Yet its so much fun. Take the following snippet of FX:

  1. kk=w*0.5;
  2. ll=h*0.5;
  3. dx=(i-kk);
  4. dy=(j-ll);
  5. aa=atan2(dy,dx);
  6. rr=hypot(dy,dx);
  7. rs=rr*rr/hypot(kk,ll);
  8. px=kk+rs*cos(aa);
  9. py=ll+rs*sin(aa);
  10. p{px,py}

Variable names are tricky. Several characters are reserved to represent pixels (p), various channels (r, g, b), locations (i,j), sizes (w,h) and more.

Lines 1 and 2 of the FX code calculates the center of an image in x,y coordinates as (kk,ll).

Lines 3 through 6 compute the polar coordinates of the current pixel i,j as a radius rr and angle aa. The internal function hypot() is a shortcut for hypotenuse of a triangle, which is the Euclidean distance between points (kk,ll) and the current pixel (i,j).

Now comes the silly part. I take the radius of the polar coordinates and square it, then divide by maximum radius of the image, for a new radius RS for “radius scaled” in Line 7. From there, I calculate the cartesian coordinate that correponds to a pixel at radius RS from the center, at the original angle AA, in lines 8 and 9. This creates a 2d bell-shaped surface that is flat at the edges curves up toward infinity at the center. Finally, Line 10 stores the pixel located at (px,py) in the current pixel (i,j) using the “p” operator.

I store this equation in a file, “effect.fx”. I downloaded a picture of Obama as obama.jpg:

Obama as we all see him

Obama as we all see him

Next I applied convert using the fx operator:

  1. convert -fx @effect.fx obama.jpg fish.jpg

This takes a few minutes on my MacBook pro. The result is a fisheye view of Obama! Fun hack, with apologies to Obama.