How to crop image on upload with Rails, Carrierwave and Minimagick?

只谈情不闲聊 提交于 2021-02-17 16:58:47

问题


I have read:

  • Undefined Method crop! Using Carrierwave with MiniMagick on rails 3.1.3
  • Carrierwave Cropping
  • http://pastebin.com/ue4mVbC8

And so I tried:

# 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

Then I used cropper: undefined local variable or method `crop_h' for /uploads/tmp/20121006-2227-4220-9621/thumb_Koala.jpg:#

Then crop: undefined method `crop_x' for #

Model:

class Project < ActiveRecord::Base
  ...
  mount_uploader :picture, ProjectPictureUploader
end

Rails 3.2, Win7,

convert -version Version: ImageMagick 6.7.9-4 2012-09-08 Q16 http://www.imagemagick.org Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC Features: OpenMP


回答1:


I have understood.

  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 from here:

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




回答2:


In #cropper your crop_h is not initialized(you double initialize crop_y instead). In #crop error is exactly what error message said - you don't have defined crop_x for Project class.



来源:https://stackoverflow.com/questions/12762728/how-to-crop-image-on-upload-with-rails-carrierwave-and-minimagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!