During my dealings with twitter avatars (see here) I discovered how to resize and crop an image using MiniMagick. This is useful when dealing with twitter avatars as the large size ones that the twitter api provides are not always square and the smaller ones may be too small for some applications. Below is the simple function :
def resize_and_crop(image, size)
if image.width image.height
remove = ((image.width - image.height)/2).round
Something which may be of use to all bloggers and social app builders is that it is pretty easy to pull someones twitter profile photo in RoR heres the code :
# call the twitter api
url = 'http://twitter.com/users/show/show.xml?email='+CGI.escape(firstname.lastname@example.org)
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(url)).body
# extract profile image tag information
doc = REXML::Document.new(xml_data)
profile_image = nil
doc.elements.each('user/profile_image_url') do |ele|
profile_image = ele.text
# if we have an image and its not a default avatar then continue
if profile_image and ! profile_image.include? "static.twitter.com"
# do as you will with the avatar
Creating a controller filter that accepts a parameter is less than obvious as I found today while trying to add more than basic authentication to a controller.
Turns out that you need to do it like this :
before_filter :only => [:create, :update, :destroy] do |controller|
rather than allowing a parameters array to pass into the before_filter function which would be nice.