开发环境: MAC OS X 10.9.5 ruby '2.0.0' gem 'rails', '4.0.1' gem 'carrierwave', '0.10.0' gem 'mini_magick', '3.8.1'
$ convert -version
Version: ImageMagick 6.8.9-7 Q16 x86_64 2014-08-31 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib freetype jng jpeg ltdl lzma png xml zlib
参照 Railscasts PRO #182 cropping images revised,可以正常更新,预览截图,但提交保存之后生成的 large 和 thumb 图片,是在原图基础上生成的,不是经过截图(crop)之后生成,应该是 img.crop 不正常。google 了一天也找不到答案。
请有经验的同学帮帮忙指点一下。
class ProjectPhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/project/#{model.id}"
end
def cache_dir
'uploads/tmp/project-cache'
end
def extension_white_list
%w(jpg jpeg gif png)
end
# Create different versions of your uploaded files:
version :large do
process resize_to_limit: [1024, -1]
end
version :thumb do
process :crop
process resize_to_fill: [200, 50]
end
def crop
if model.crop_x.present?
resize_to_limit(1024, -1)
manipulate! do |img|
x = model.crop_x.to_i
y = model.crop_y.to_i
w = model.crop_w.to_i
h = model.crop_h.to_i
img.crop "#{w}x#{h}+#{x}+#{y}"
end
end
end
end