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
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
Find Number of Bytes a Particular Hash Is Using in Ruby
Getaddrinfo Error with Mechanize
Best Way to Handle Category/Subcategory Relationship Ruby on Rails
Why Will a Range Not Work When Descending
Rails How to Change Attribute Name When Rendering JSON
Why Is Uri.Escape() Marked as Obsolete and Where Is This Regexp::Unsafe Constant
How to Update to Ruby 2.1.2 Using Rails 3.2.3
Ruby on Rails Add a Column After a Specific Column Name
Rails: How to Print a Decimal as a Percent
Change String in a Date Format to Another Format
Sort a List of Objects by Using Their Attributes in Ruby
Custom Filtering of Parameters in Rails 3 Using Config.Filter_Parameters
Better Way to Write "Matching Balanced Parenthesis" Program in Ruby
Bitwise Operations on Strings with Ruby
Convert Ruby Source Code from Old Style to New Style Hash
How to Use Activesupport::Configurable with Rails Engine