Carrierwave作物特定版本

我正致力于使用carrierwave和Jcrop裁剪图像。 它是Railscasts第182集和第253集的组合。我有裁剪工作,但它裁剪原始。 无论如何都强迫manupulate! 使用不同的版本?

 def crop_image(x,y,w,h) manipulate! do |img| img.crop(x.to_i, y.to_i, w.to_i, h.to_i) end end 

或者有没有办法从模型调用中设置版本?

 attr_accessor :crop_x, :crop_y, :crop_w, :crop_h attr_accessible :description, :image, :crop_x, :crop_y, :crop_w, :crop_h after_update :reprocess_image, :if => :cropping? def cropping? !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank? end def reprocess_image image.crop_image(crop_x, crop_y, crop_w, crop_h) end 

在railscast中,Ryan的解决方案是通过查找大版本和原始版本之间的比率来转换coords以使用原始图像。 通过遵循相同的逻辑,我能够使用Carrierwave和jCrop。 有趣的是,Carrierwave不存储图像的尺寸。 我能够从这篇文章中解决一些问题: http : //code.dblock.org/ShowPost.aspx?Id = 194 。

这是我的解决方案。

user.rb

 class User < ActiveRecord::Base attr_accessor :password, :crop_x, :crop_y, :crop_h, :crop_w after_update :reprocess_profile, :if => :cropping? mount_uploader :profile, ProfileUploader def cropping? !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank? end def profile_geometry img = Magick::Image::read(self.profile.current_path).first @geometry = {:width => img.columns, :height => img.rows } end private def reprocess_profile self.profile.recreate_versions! end end 

account_controller.rb

 class AccountController < ApplicationController def crop @account = current_user end def crop_update @account = current_user @account.crop_x = params[:account]["crop_x"] @account.crop_y = params[:account]["crop_y"] @account.crop_h = params[:account]["crop_h"] @account.crop_w = params[:account]["crop_w"] @account.save redirect_to account_path end end 

profile_uploader.rb

 class ProfileUploader < CarrierWave::Uploader::Base def extension_white_list %w(jpg jpeg gif png) end version :large do process :resize_to_fit => [500, 500] end version :thumb do process :manualcrop process :resize_to_fill => [100, 100] end def manualcrop return unless model.cropping? manipulate! do |img| img = img.crop(model.crop_x.to_i,model.crop_y.to_i,model.crop_h.to_i,model.crop_w.to_i) end end end 

crop.html.erb

 <% content_for :head do %> <%= stylesheet_link_tag "jquery.Jcrop" %> <%= javascript_include_tag "jquery.Jcrop.min" %>  <% end %> <%= image_tag @account.profile_url(:large), :id => "cropbox" %> 

Preview

<%= image_tag @account.profile_url(:large), :id => "preview" %>
<%= form_for @account, :as => :account, :url => { :action => "crop_update" } do |f| %> <% for attribute in [:crop_x, :crop_y, :crop_h, :crop_w] %> <%= f.hidden_field attribute, :id => attribute %> <% end %>

<%= f.submit "Crop" %>

<% end %>

style.css文件

 .preview { width:100px; height:100px; overflow:hidden; } 

如果我正确理解了这个问题,更简单的方法就是向Jcrop发送trueSize选项。

 <% geo = @item.image_geometry %> $('#cropbox').Jcrop({boxWidth:656,boxHeight:350,onChange:update_crop,onSelect: update_crop,aspectRatio:656/350,trueSize:[<%= "#{geo[0]}, #{geo[1]}"%>]}); 

Ryan刚刚更新了railscast#182,它现在使用CarrierWave

http://railscasts.com/episodes/182-cropping-images-revised