百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rjcrop

> 编程语言
开源

使用 Paperclip 和 Jcrop 进行图像裁剪的示例 Rails 应用

165 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

使用 Paperclip 和 Jcrop 进行图像裁剪的示例 Rails 应用

=RJCrop

This is a sample application that shows how to implement image cropping using the popular Paperclip Rails plugin and the Jcrop jQuery plugin for selecting the cropping area.

Paperclip project page: http://github.com/thoughtbot/paperclip/tree/master Jcrop home page: http://deepliquid.com/content/Jcrop.html

Most of the inspiration for this application was taken from this thread: http://groups.google.com/group/paperclip-plugin/browse_thread/thread/817266ea5b37580c and especially this helpful piece of code: http://groups.google.com/group/paperclip-plugin/msg/5b6dd7ade3ba6b87?hl=en

==Downloading and testing the application

git clone git://github.com/jschwindt/rjcrop.git cd rjcrop rake db:migrate git submodule init git submodule update ./script/server

After that you can point your browser to http://localhost:3000, upload an image and see how easy is to create a cropped image that can be used as an avatar.

==How does it work?

This application was featured by Ryan Bates on his Railscast #182 http://railscasts.com/episodes/182-cropping-images

The tricky part creating this application was to find a simple way of interacting with Paperclip in order to create the cropped images after the model was saved. In fact it was necessary to reprocess the attachments after they were initially loaded by the plugin.

===The model

class User { :normal => "240x240>", :small = > "55x55#" }, :processors => [:jcropper]

attr_accessor :crop_x, :crop_y, :crop_w, :crop_h

after_update :reprocess_avatar, :if => :cropping?

def cropping?
  !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end

# helper method used by the cropper view to get the real image geometry
def avatar_geometry(style = :original)
  @geometry ||= {}
  @geometry[style] ||= Paperclip::Geometry.from_file avatar.path(style)
end

private

def reprocess_avatar
  avatar.reprocess!
end

end

The model includes the +has_attached_file+ as usual but it specifies a custom processor +jcropper+ that is slightly different from the original thumbnail.rb processor provided by Paperclip.

The process of creating the avatar works like this:

  • The user uploads an image and Paperclip creates the :normal and the :small version and because none of the :crop_x, :crop_y, :crop_w, :crop_h are defined +cropping?+ returns false. This way the +jcropper+ processor works just like the original +thumbnail+ processor.

  • Then the user is presented with the +normal+ thumbnail that is used to choose the cropping rectangle.

  • Finally when the user model is updated and +cropping?+ returns true, the Paperclip reprocess! method is invoked and the +jcropper+ processor crop the original image according to the cropping rectangle.

Because now the cropping rectangle parameters are provided by the view, then the +crop_command+ specifies how to crop the original image. The string looks like: "-crop 500x500+125+450" that in the ImageMagick convert command means "crop the original image at offset 125,450 with a size of 500x500". Finally the images are resized using :normal => "240x240>" and :small = > "55x55" parameters.

===The lib/papercli_processors/jcropper.rb

Jcropper paperclip processor

This processor very slightly changes the default thumbnail processor in order to work properly with Jcrop

the jQuery cropper plugin.

module Paperclip # Handles thumbnailing images that are uploaded. class Jcropper / ; : }

==Author

Juan Schwindt juan(at)schwindt.org

Issues· 1 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言