小编Leo*_*nid的帖子

如何使用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! …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-3 minimagick

6
推荐指数
1
解决办法
1万
查看次数