Class: Paperclip::Vips
- Inherits:
-
Processor
- Object
- Processor
- Paperclip::Vips
- Defined in:
- lib/paperclip-vips/paperclip/vips.rb
Instance Attribute Summary collapse
-
#auto_orient ⇒ Object
Returns the value of attribute auto_orient.
-
#convert_options ⇒ Object
Returns the value of attribute convert_options.
-
#current_geometry ⇒ Object
Returns the value of attribute current_geometry.
-
#format ⇒ Object
Returns the value of attribute format.
-
#source_file_options ⇒ Object
Returns the value of attribute source_file_options.
-
#target_geometry ⇒ Object
Returns the value of attribute target_geometry.
-
#whiny ⇒ Object
Returns the value of attribute whiny.
Instance Method Summary collapse
- #crop ⇒ Object
- #height ⇒ Object
-
#initialize(file, options = {}, attachment = nil) ⇒ Vips
constructor
A new instance of Vips.
- #make ⇒ Object
- #process_convert_options(image) ⇒ Object
- #save_gif(thumbnail, path) ⇒ Object
- #save_jpg(thumbnail, path) ⇒ Object
- #save_png(thumbnail, path) ⇒ Object
- #save_thumbnail(thumbnail, path) ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(file, options = {}, attachment = nil) ⇒ Vips
Returns a new instance of Vips.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 6 def initialize(file, = {}, = nil) super geometry = [:geometry].to_s @should_crop = geometry[-1,1] == '#' @target_geometry = .fetch(:string_geometry_parser, Geometry).parse(geometry) @current_geometry = .fetch(:file_geometry_parser, Geometry).from_file(@file) @whiny = .fetch(:whiny, true) @current_format = File.extname(@file.path)&.downcase @format = [:format] || @current_format @basename = File.basename(@file.path, @current_format) end |
Instance Attribute Details
#auto_orient ⇒ Object
Returns the value of attribute auto_orient.
3 4 5 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 3 def auto_orient @auto_orient end |
#convert_options ⇒ Object
Returns the value of attribute convert_options.
3 4 5 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 3 def @convert_options end |
#current_geometry ⇒ Object
Returns the value of attribute current_geometry.
3 4 5 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 3 def current_geometry @current_geometry end |
#format ⇒ Object
Returns the value of attribute format.
3 4 5 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 3 def format @format end |
#source_file_options ⇒ Object
Returns the value of attribute source_file_options.
3 4 5 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 3 def @source_file_options end |
#target_geometry ⇒ Object
Returns the value of attribute target_geometry.
3 4 5 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 3 def target_geometry @target_geometry end |
#whiny ⇒ Object
Returns the value of attribute whiny.
3 4 5 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 3 def whiny @whiny end |
Instance Method Details
#crop ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 21 def crop if @should_crop return @options[:crop] || :centre end nil end |
#height ⇒ Object
54 55 56 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 54 def height @target_geometry&.height || @current_geometry.height end |
#make ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 29 def make source = @file filename = [@basename, @format ? ".#{@format}" : ""].join destination = TempfileFactory.new.generate(filename) begin thumbnail = ::Vips::Image.thumbnail(source.path, width, { height: crop ? height : nil, crop: crop }) if @target_geometry thumbnail = ::Vips::Image.new_from_file(source.path) if !defined?(thumbnail) || thumbnail.nil? thumbnail = (thumbnail) save_thumbnail(thumbnail, destination.path) rescue => e if @whiny = "There was an error processing the thumbnail for #{@basename}:\n" + e. raise Paperclip::Error, end end return destination end |
#process_convert_options(image) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 58 def (image) if image && @options[:convert_options].present? commands = JSON.parse(@options[:convert_options], symbolize_names: true) commands.each do |cmd| image = ::Vips::Operation.call(cmd[:cmd], [image, *cmd[:args]], cmd[:optional] || {}) end end return image end |
#save_gif(thumbnail, path) ⇒ Object
84 85 86 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 84 def save_gif(thumbnail, path) thumbnail.magicksave(path) end |
#save_jpg(thumbnail, path) ⇒ Object
80 81 82 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 80 def save_jpg(thumbnail, path) thumbnail.jpegsave(path) end |
#save_png(thumbnail, path) ⇒ Object
88 89 90 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 88 def save_png(thumbnail, path) thumbnail.pngsave(path) end |
#save_thumbnail(thumbnail, path) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 69 def save_thumbnail(thumbnail, path) case @current_format when ".jpeg", ".jpg" save_jpg(thumbnail, path) when ".gif" save_gif(thumbnail, path) when ".png" save_png(thumbnail, path) end end |
#width ⇒ Object
50 51 52 |
# File 'lib/paperclip-vips/paperclip/vips.rb', line 50 def width @target_geometry&.width || @current_geometry.width end |