2012-10-06 30 views
5

我已閱讀:如何使用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 ř:未定義局部變量或方法`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 版權:ImageMagick Studio LLC 功能:OpenMP

回答

7

我明白了。從這裏

version :thumb do  
    process resize_to_fit: [300, nil] 
    process crop: '300x150+0+0' 
    #process resize_and_crop: 200 
    end 

private 

    # Simplest way 
    def crop(geometry) 
    manipulate! do |img|  
     img.crop(geometry) 
     img 
    end  
    end 

    # Resize and crop square from Center 
    def resize_and_crop(size) 
    manipulate! do |image|     
     if image[:width] < image[:height] 
     remove = ((image[:height] - image[:width])/2).round 
     image.shave("0x#{remove}") 
     elsif image[:width] > image[:height] 
     remove = ((image[:width] - image[:height])/2).round 
     image.shave("#{remove}x0") 
     end 
     image.resize("#{size}x#{size}") 
     image 
    end 
    end 

resize_and_crop:

http://blog.aclarke.eu/crop-and-resize-an-image-using-minimagick/

0

在#cropper中,您的crop_h未初始化(而是雙重初始化crop_y)。 #crop錯誤正是錯誤消息所說的 - 您沒有爲Project類定義crop_x。

+0

是啊,我發現錯字在#cropper,但它並沒有幫助。 – Leonid

相關問題