我无法在RoR中使用Carrierwave / Minimagick宝石实现简单的图像上传器。
我正在尝试在上传时将文件转换为灰度,但我收到了错误消息。这是代码:
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枚举常量。任何想法为什么这不起作用?
答案 0 :(得分:1)
是否将manipulate
函数加载到内存中?它会返回图像列表吗?
我认为图片没有正确加载。问题不在于Magick enum。
以下是一个示例示例:
require 'RMagick'
clown = Magick::ImageList.new("clown.jpg")
clown = clown.quantize(256, Magick::GRAYColorspace)
clown.write('monochrome.jpg')