0

我无法在 RoR 中使用 Carrierwave/Minimagick gem 实现简单的图像上传器。

我正在尝试在上传时将文件转换为灰度,但出现错误。这是代码:

image_uploader.rb:

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Process files as they are uploaded:
   process :convert_to_grayscale

  def convert_to_grayscale
    manipulate! do |img|
      img.quantize(256, Magick::GRAYColorspace)
      img = yield(img) if block_given?
      img
    end
  end

当我尝试上传文件时,出现以下错误:

uninitialized constant ImageUploader::Magick

app/uploaders/image_uploader.rb:36:in `block in convert_to_grayscale'
app/uploaders/image_uploader.rb:35:in `convert_to_grayscale'

我相信这是由于 Magick::GRAYColorspace 枚举常量。任何想法为什么这不起作用?

4

1 回答 1

1

manipulate将图像加载到内存的功能吗?它是否返回图像列表?

我认为图像未正确加载。问题不在于 Magick 枚举。

这是一个示例:

require 'RMagick'

clown = Magick::ImageList.new("clown.jpg")
clown = clown.quantize(256, Magick::GRAYColorspace)
clown.write('monochrome.jpg')
于 2013-01-30T14:40:18.053 回答