Reputation: 123
I am having trouble implementing a simple image uploader with Carrierwave/Minimagick gems in RoR.
I'm trying to convert the file to grayscale upon upload, but I am getting an error. Here is the code:
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
When I try to upload a file, I get the following error:
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'
I believe this is due to the Magick::GRAYColorspace enum constant. Any ideas why this isnt working?
Upvotes: 0
Views: 1584
Reputation: 1002
Is the manipulate
function that loads images to memory? Does It return a image list?
I think that the images aren't loaded correctly. The problem isn't the Magick enum.
Here is a sample example:
require 'RMagick'
clown = Magick::ImageList.new("clown.jpg")
clown = clown.quantize(256, Magick::GRAYColorspace)
clown.write('monochrome.jpg')
Upvotes: 1