I'm currently using Paperclip to upload an image and automatically generate a thumbnail. Now I would also like to add a second style that generates a one-pixel-wide image using the left-most column of pixels in the uploaded image (it should also have the same height as the original image). I'll be using the one-pixel-wide image as a repeating background via CSS.
Is it possible to generate that background image using Paperclip's default Thumbnail processor, or will I need to create my own custom processor? I've already tried creating a custom processor that subclasses the Paperclip::Processor
, but I didn't understand how to use the Paperclip.run
method properly. Now I'm trying to subclass Paperclip::Thumbnail
based on Ryan Bate's Railcast here: http://railscasts.com/episodes/182-cropping-images, but that is throwing this error:
NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]):
app/controllers/images_controller.rb:11:in `create'
Line 11 of images_controller.rb:
@image = @review.images.build(params[:image])
Line 11 of images_controller.rb works fine if I don't try using the Autobackground custom processor, so the error must be the code in the processor.
Here's my code so far:
#/app/models/image.rb
class Image < ActiveRecord::Base
belongs_to :review
has_attached_file :image, :styles => {
:thumb => "32x32#",
:auto_bg => { :processors => [:autobackground] }
}
end
#/lib/paperclip_processors/Autobackground.rb
module Paperclip
class Autobackground < Thumbnail
def transformation_command
if crop_command
crop_command + super.sub(/ -crop \S+/, '')
else
super
end
end
def crop_command
target = @attachment.instance
if target.cropping?
" -crop '1x#{target.height}+0+0'"
end
end
end
end