Vixiom Axioms

December 28, 2007

Hacking attachment_fu to work with Flash/Flex uploads and crop square images

Filed under: Flash, Flex, Ruby, Ruby on Rails Alastair @ 1:19 pm

Rick Olson’s attachment_fu is my favorite file upload plug-in because let’s you use three different image manipulation tools [rmagick, mini-magick, image science] and storage options [file system, database, amazon s3]. However it doesn’t yet support two features I use on every CMS I build, Flash/Flex file upload (images will upload but won’t be resized) and square image cropping. Here’s how to tweak it to get both features working.

First up, support for Flash/Flex upload (I should really drop the ‘Flash/’ part as I only use Flex now) , first up Flex upload… Ilya Devers posted the solution on Google groups, but I get to claim 1% credit as my blog is mentioned in his post :P

The problem is really on the Flex side of things as all uploads come through as ‘application/octet-stream’ for the mime-type. attachement_fu can upload any kind of file so it checks the mime-type before running it’s resize code, since it’s looking for an image it skips over the Flex uploaded files. Ilya’s rather ingenious solution is to override attachment_fu and use the file system to check the file type. To overide attachment_fu add the ‘uploaded_data=’ and ‘get_content_type’ methods to your upload model.

class Upload < ActiveRecord::Base
  belongs_to :image

  has_attachment :content_type => :image,
                 :storage => :file_system,
                 :processor => MiniMagick,
                 :max_size => 2000.kilobytes,
                 :resize_to => 620×465>,
                 :thumbnails => { :thumb => [90, 90] }

  #override from has_attachment plugin
  def uploaded_data=(file_data)
    return nil if file_data.nil? || file_data.size == 0
    self.filename = file_data.original_filename if respond_to?(:filename)
    if file_data.is_a?(StringIO)
      file_data.rewind
      self.temp_data = file_data.read
    else
      self.temp_path = file_data.path
    end
    # in the original the next line occured earlier, and just used file_data.content_type
    self.content_type = get_content_type((file_data.content_type.chomp if file_data.content_type))
  end

  #uses the os’s “file” utility to determine the file type, yanked and modified slightly from file_column.
  def get_content_type(fallback=nil)
    begin
      content_type = `file -bi “#{File.join(temp_path)}`.chomp
      content_type = fallback unless $?.success?
      content_type.gsub!(/;.+$/,) if content_type
      content_type
    rescue
      fallback
    end
  end
end

Next is cropping square images with mini-magick. Currently if you request a square image attachment_fu will stretch rather than crop the image, this time I’ll ‘borrow’ the solution from Craig Ambrose. This time you have to dig deeper down into the depths of the rails plugins directory to edit ‘vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb’ and replace the resize_image method with the following.

# Performs the actual resizing operation for a thumbnail
def resize_image(img, size)
  size = size.first if size.is_a?(Array) && size.length == 1
  if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
    if size.is_a?(Fixnum)
      resize_and_crop(img, size)
    else
      size[0] == size[1] ? resize_and_crop(img, size[0]) : img.resize(size.join(x))
    end
  else
    img.resize(size.to_s)
  end
  self.temp_path = img
end

def resize_and_crop(image, square_size)
  if image[:width] < image[:height]
    shave_off = ((image[:height] - image[:width])/2).round
    image.shave(0x#{shave_off})
  elsif image[:width] > image[:height]
    shave_off = ((image[:width] - image[:height])/2).round
    image.shave(#{shave_off}x0)
  end
  image.resize(#{square_size}x#{square_size})
  return image
end

To crop an image you use ‘:thumb => [90, 90]‘ as in the Model code above. That’s it!

Digg! submit Hacking attachment_fu to work with Flash/Flex uploads and crop square images to stumbleupon.com submit Hacking attachment_fu to work with Flash/Flex uploads and crop square images to del.icio.us submit Hacking attachment_fu to work with Flash/Flex uploads and crop square images to reddit.com Like this post? subscribe to the feed.

December 12, 2007

Flex Builder 3 Beta 3 is out

Filed under: Flex Alastair @ 10:06 pm

http://labs.adobe.com/technologies/flex/flexbuilder3/

UPDATE: and stuff compiles waaaaaay faster nice!

Digg! submit Flex Builder 3 Beta 3 is out to stumbleupon.com submit Flex Builder 3 Beta 3 is out to del.icio.us submit Flex Builder 3 Beta 3 is out to reddit.com Like this post? subscribe to the feed.

December 7, 2007

Rails 2.0 is go

Filed under: Ruby, Ruby on Rails Alastair @ 2:00 pm

Tons of new stuff, my favorite is Cookie store sessions

The default session store in Rails 2.0 is now a cookie-based one. That means sessions are no longer stored on the file system or in the database, but kept by the client in a hashed form that can’t be forged. This makes it not only a lot faster than traditional session stores, but also makes it zero maintenance. There’s no cron job needed to clear out the sessions and your server won’t crash because you forgot and suddenly had 500K files in tmp/session.

Digg! submit Rails 2.0 is go to stumbleupon.com submit Rails 2.0 is go to del.icio.us submit Rails 2.0 is go to reddit.com Like this post? subscribe to the feed.

December 6, 2007

Akelos PHP framework apes Rails

Filed under: PHP, Ruby on Rails Alastair @ 11:40 pm

Monkey see monkey do. I’m usually wary of PHP frameworks that are ‘inspired’ by Ruby on Rails but the Akelos folks have done a really good job with their port. They even have a screencast narrated by guy who’s funny accent you can’t quite place :P

Every now and then I get a project that has to be in PHP (usually because of hosting requirements) so it’s nice to have everything laid out in a familiar way.

Digg! submit Akelos PHP framework apes Rails to stumbleupon.com submit Akelos PHP framework apes Rails to del.icio.us submit Akelos PHP framework apes Rails to reddit.com Like this post? subscribe to the feed.

December 4, 2007

Two new ways to load data into Flash

SWX Ruby is a port of SWX (originaly PHP), which claims to be “Ruby’s fastest library for exchanging data with Flash” I haven’t tried it because it doesn’t yet support Flash player 9 but I wouldn’t doubt it’s the fastest as many other methods aren’t too speedy (Although RubyAMF is much quicker lately).

as3yaml, you guessed it, is an Actionscript 3 YAML 1.1 parser and emitter.

Digg! submit Two new ways to load data into Flash to stumbleupon.com submit Two new ways to load data into Flash to del.icio.us submit Two new ways to load data into Flash to reddit.com Like this post? subscribe to the feed.

Want a job working with Flex and Rails?

Filed under: ActionScript, Flex, Ruby, Ruby on Rails Alastair @ 3:01 pm

Jobs that combine Flex and Rails don’t come down the pike too often. Even more rare is one from a start up with a lot of buzz like Blist.

Speaking of pikes the job is in Seattle so all you Northwest Flex/Railers send in your resume and the solution to Blist’s programming challenge.

Without using any built in date or time functions, write a function or method that accepts two mandatory arguments. The first argument is a string of the format “[H]H:MM {AM|PM}” and the second argument is an integer. Assume the integer is the number of minutes to add to the string. The return value of the function should be a string of the same format as the first argument. For example AddMinutes(”9:13 AM”, 10) would return “9:23 AM”. The exercise isn’t meant to be too hard. I just want to see how you code. Feel free to do it procedurally or in an object oriented way, whichever you prefer. Use any language you want. Write production quality code.

Digg! submit Want a job working with Flex and Rails? to stumbleupon.com submit Want a job working with Flex and Rails? to del.icio.us submit Want a job working with Flex and Rails? to reddit.com Like this post? subscribe to the feed.

December 3, 2007

Hints of big AMF news this month

Filed under: Flash, Flash Remoting, Flex Alastair @ 12:43 pm

From Ted (on flex) and Ryan Stewart.

Most are guessing AMF will be open sourced, anything that makes AMF more popular will be good news.

Digg! submit Hints of big AMF news this month to stumbleupon.com submit Hints of big AMF news this month to del.icio.us submit Hints of big AMF news this month to reddit.com Like this post? subscribe to the feed.

Powered by WordPress