Create Paperclip Attachment from Rmagick Image

Create paperclip attachment from rmagick image

Let's see if that's what you need

picture = imageList.flatten_images
file = Tempfile.new('my_picture.jpg')
picture.write(file.path)
YourModel.create(:picture => file, ...)

Change YourModel with the model you are using...

RMagick mask Paperclip image attachment

After a little trial and error, I ended up writing a Paperclip processor:

model:

:styles =>{
:main_feature => {:geometry => "1020x470", :processors => [:masker] },
:top_feature => "345x159",
:smallest => "229x131"
}

/lib/paperclip_processors/masker.rb:

module Paperclip
class Masker < Processor
def initialize file, options = {}, attachment = nil
super
@format = File.extname(@file.path)
@basename = File.basename(@file.path, @format)
end

def make

src = @file
dst = Tempfile.new([@basename, @format])
dst.binmode

begin
parameters = []

parameters << ':source'
parameters << ':mask'
parameters << '-alpha'
parameters << 'on'
parameters << '-compose'
parameters << 'CopyOpacity'
parameters << '-composite'
parameters << ':dest'

parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

mask_path = File.expand_path('lib/paperclip_processors/mask.png')
success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}[0]", :mask => "#{mask_path}[0]", :dest => File.expand_path(dst.path))

rescue PaperclipCommandLineError => e
raise PaperclipError, "There was an error during the mask for #{@basename}" if @whiny
end

dst
end

end
end

How to create an RMagick image from a Paperclip attachment?

You're using Ruby. Monkey-patch Paperclip::Attachment!

# lib/paperclip_magick.rb
require 'RMagick'

module PaperclipMagick
def to_rmagick(style_name = default_style)
@image_list ||= ImageList.new(self.path(style_name))
end
end

Paperclip::Attachment.send(:include, Magick)
Paperclip::Attachment.send(:include, PaperclipMagick)

Or something along those lines.

How to save RMagick processed image using Paperclip without writing to a file

Okay, the solution was to change the Magic image object to File object before uploading it to Paperlip

so,

processed_image = StringIO.open(trimmed_img.to_blob)

then upload processed image directly with paperclip

Paperclip & RMagick - 3-page thumbnail of PDF and renaming

I didn't test this, but how about:

has_attached_file :file, :styles => { :pdf_thumbnail => ["", :jpg] } ...

According to the Paperclip docs the second item in the array should force the format, although it does not specify if it works with custom processors also. But worth a shot.

Paperclip image upload in rails

Sample Image

I did a mistake by overriding the paperclip default path without :style specified. So it stores only the default size. The processed image (by ImageMagick) does not store as I didn't mention at override path.



Related Topics



Leave a reply



Submit