[GAE]AppEngine::Imagesを使ってだいたい正方形のサムネイルを出す

160*160のサムネイルをImage Cropのところに出そうとしています。
Imageは元々の画像

show.html.erb

<div>
  <strong>Image:</strong>
  <%=image_tag(url_for(:action => :puts_image, :id => @article.id), :alt => @article.filename) %>
</div>
<div>
  <strong>Image Crop:</strong>
  <%=image_tag(url_for(:action => :puts_crop_image, :id => @article.id), :alt => @article.filename) %>
</div>

application_controller.rb

  def puts_image
    @article = Article.get(params[:id].to_i)
    send_data(@article.data, :disposition => 'inline', :type => @article.type)
  end
    
  def puts_crop_image
    width = 160.0
    height = 160.0

    @article = Article.get(params[:id].to_i)
    image = AppEngine::Images.load(@article.data)
    org_width = image.width
    org_height = image.height

    scale_w = height / org_width
    scale_h = height / org_height
    scale =  (scale_w < scale_h)? scale_h : scale_w

    thumbnail_width = (org_width * scale).to_i
    thumbnail_height = (org_height * scale).to_i
    image = image.resize(thumbnail_width, thumbnail_height)

    x  = (thumbnail_width - width) / 2.0
    y  = (thumbnail_height - height) / 2.0
    image = image.crop(x/thumbnail_width, y/thumbnail_height, 1.0-(x/thumbnail_width), 1.0-(y/thumbnail_height))

    send_data(image.data, :disposition => 'inline', :type => @article.type)
  end 

こんな感じに

f:id:tohtas:20100222035845p:image

こちらを参考にしました

正方形なサムネイルを簡単に出す