私が読んでいる:Rails、Carrierwave、Minimagickでアップロードした画像を切り抜くには?
をそして私が試した:
# encoding: utf-8
class ProjectPictureUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :cropper
# process :crop
process resize_to_fit: [200, 200]
end
def cropper
manipulate! do |img|
# if model.crop_x.present?
image = MiniMagick::Image.open(current_path)
crop_w = (image[:width] * 0.8).to_i
crop_y = (image[:height] * 0.8).to_i
crop_x = (image[:width] * 0.1).to_i
crop_y = (image[:height] * 0.1).to_i
# end
img = img.crop "#{crop_x}x#{crop_y}+#{crop_w}+#{crop_h}"
img
end
end
def crop
if model.crop_x.present?
resize_to_limit(700, 700)
manipulate! do |img|
x = model.crop_x
y = model.crop_y
w = model.crop_w
h = model.crop_h
w << 'x' << h << '+' << x << '+' << y
img.crop(w)
img
end
end
end
end
をその後、私はcroppeを使用R:未定義のローカル変数またはメソッド `crop_h '/uploads/tmp/20121006-2227-4220-9621/thumb_Koala.jpg:#
、次いで作物:未定義のメソッド` crop_x' #
用モデル:
class Project < ActiveRecord::Base
...
mount_uploader :picture, ProjectPictureUploader
end
のRails 3.2の、Win7、
変換-version バージョン:ImageMagickの6.7.9-4 2012年9月8日Q16 http://www.imagemagick.org特徴:OpenMP
ええ、私は#cropperにタイプミスを見つけたが、それは助けていませんでした。 – Leonid