 View Cart Login        

Garbage Burrito!

An online internet web blog by Ben Kittrell

  • Blog
  • Tweets
  • Contact
  • Photos
  • Favorite Burrito
  • Burrito Shop

 About Ben

BenKittrell.jpgCo-Founder of Doodlekit. Coder, Home Renovator, Enjoyer of Refreshing Beverages.

Sometimes I blog here, sometimes on Doodlekit and sometimes I tweet.

My Resume

Fork Me on Github!

160x600.gif

free website builder

 

 Blog Entries

  • Google Fiber Experiment: Cloud Video Transcoding
  • Doodlekit 2.0
  • New Doodlekit Release in 2013
  • Variable Arguments with Callback in CoffeeScript
  • Sequence Diagrams for Asynchronous Flows in Node.js
more  

 Blog Comments

  • David Bennett: Google Fiber Experiment: Cloud Video Transcoding
  • Akash: Console 2: The Windows Command Prompt Replacement
  • Ben Kittrell : Doodlekit 2.0
  • Akash: Console 2: The Windows Command Prompt Replacement
  • Ryan McCullough: Doodlekit 2.0
more  

 Links


  • website reviewer
  • live on - sharing memories
  • kids website builder
  • resume website
  • martial arts website builder
  • affiliate website builder
  • teacher website

 Login

 Forgot Password?
 Back to Blog

JavaScript Image Cropping with jsCropperUI and Rails

Ben Kittrell
 11/13/2006 03:30PM

We just added a cool new feature to doodlekit™ that lets users upload their own header image for their layout.  The obvious problem with this is resizing and cropping images is a pain, even if you happen to have photoshop.  There's a better way.  I knew I could do the cropping with RMagick, but how do I know what to crop?  Enter DEfusion's JavaScript Image Cropper UI.  This was just perfect, as it uses prototype and scriptaculous, which I'm already using through Rails.  Not only that but it supports ratio constraints, which is very important for my purposes.

As for the implementation, it was fairly simple.  I just looked at the example they had for fixed ratios, only changed a few things.

My javascript import looks like this...

<%= javascript_include_tag("prototype", "builder", "dragdrop", "cropper", "scriptaculous") %>

I changed the ratioDim line to use the size of the target space as the default dimensions.

ratioDim: { x: <%= @layout.header_width %>, y: <%= @layout.header_height %> },

This way, they can resize the cropping area, but only with respect to the target ratio.  I changed the text fields to hidden fields and just slapped a form tag around them that posts to a Rails action that looks like this.

  def crop_and_close
    @settings = @site.settings
    @layout = @site.layout
    if params[:x1]
      img = Magick::Image::read(@settings.header_image_file).first
      img.crop!(::Magick::CenterGravity, params[:x1].to_i, params[:y1].to_i,
         params[:width].to_i, params[:height].to_i, true)
      img.resize!(@layout.header_width, @layout.header_height)
      img.write @settings.header_image_file
    end
  end

I'm already using RMagick, and file_column to upload the image btw.  This simply uses RMagick to crop the file, and then resizes it to the target size for the layout.

This was so super easy, and so far it works in FF, IE, and Opera!  If you're looking to do something like this, I highly recommend this Javascript cropping UI. 

 Comments

 Bitbutter  12/17/2006 07:51AM 

Thanks for this. Also useful for allowing users to choose which portion of an image will be used for a fixed width and height thumbnail.

 Sam Coles  01/30/2007 11:45PM 

Hi,

Thanks for the article. I encounter this requirement often enough that <a href="http://sam.isitva.com/articles/2007/01/29/cropping-images-with-rails">I wrote a plugin</a> to set all this up using ActiveRecord validations. Let me know what you think.

 Bobby  06/19/2007 01:26AM 

Hi - question about the way you implement the upload/crop process: do you save the image first, then crop and update? I use attachment_fu, and need to save thumbnails as well. What I did was after "create" in which the image is saved (along with its thumbnails), I redirect to another page where the uncropped image is displayed, and then sent to a "crop" action that uses your code snippet above. My problem is, how do I then update the thumbs as well?

 Jens  08/15/2007 05:38AM 

Hi,

the tool is very nice. but what do I do with the backend code? I am using Asp but, when I use the code on my server. I can only see the picture in the small preview, the big preview is black where the square is.

I hope someone can help.. biip19@hotmail.com

 Patricia Miranda My Website  07/22/2008 12:04PM 

A Bermuda modeladora Anti Celulite Colter® é feita de poliamida e elastano, sendo que ao tecido é incorporada a biocerâmica emissora de raio infra vermelho longo que estimula o metabolismo celular e a micro circulação local dissolvendo o excesso de gordura, conheça
também outros produtos como calças, short, meias e camisas com biocerâmica

 Leon My Website  08/30/2008 12:17PM 

This is really cool. Ta. I found a similar article which might also help others :

http://www.googleme.com.au/articles/how_to_crop_images_using_asp_and_javascript.asp

 sanjai My Website  09/22/2008 03:58AM 

Hello Sir/Madam,

Pls Help me,

I want to crop a particular portion, and that portion is highlighted

B.Sanjai
VerveMedia
Chennai

 sanjai My Website  09/22/2008 04:27AM 

Pls Help me,

I want to crop a particular portion, and that portion is highlighted

B.Sanjai
VerveMedia
Chennai

 kroppr My Website  11/18/2008 08:47AM 

what about rotate and zoom the image then create the cropped picture for header?
like this script does:

<a href="http://kroppr.rborn.info">Kroppr - image crop script</a>

:)

 Andrew Forrest My Website  01/30/2009 06:14PM 

Crop it using ASP and Javascript

http://www.imagecowboy.com/articles/how_to_crop_images_using_asp_and_javascript.asp

 DropShip My Website  04/06/2009 10:04PM 

Very Good .

In China is the best !

 Post a Comment



 Search

Follow On...

  • Facebook
  • Twitter
  • LinkedIn
  • Google+
  • Pinterest
  • RSS Feeds
  • Blog Posts,
  • Blog Comments,
  • Gallery Images,
  • Forum Topics

Copyright © 2006 Garbage Burrito. All Rights Reserved.
hosted by doodlekit™- free website builder

 

 powered by Doodlekit™ Doodlekit Free Website Builder