【问题标题】:How to crop image on upload with Rails, Carrierwave and Minimagick?如何使用 Rails、Carrierwave 和 Minimagick 在上传时裁剪图像?
【发布时间】:2012-10-06 18:45:39
【问题描述】:

我已阅读:

所以我尝试了:

# 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

然后我使用 cropper: undefined local variable or method `crop_h' for /uploads/tmp/20121006-2227-4220-9621/thumb_Koala.jpg:#

然后crop:#

的未定义方法`crop_x'

型号:

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

Rails 3.2、Win7、

转换-版本 版本:ImageMagick 6.7.9-4 2012-09-08 Q16 http://www.imagemagick.org 版权所有:版权所有 (C) 1999-2012 ImageMagick Studio LLC 特点:OpenMP

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 minimagick


    【解决方案1】:

    我明白了。

      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
    

    从这里调整大小和裁剪:

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

    【讨论】:

      【解决方案2】:

      在#cropper 中,您的cropper_h 未初始化(您改为双重初始化cropper_y)。 #crop 中的错误正是错误消息所说的 - 您没有为 Project 类定义crop_x。

      【讨论】:

      • 是的,我在#cropper 中发现了错字,但没有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多