Class: Paperclip::Vips

Inherits:
Processor
  • Object
show all
Defined in:
lib/paperclip/vips.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
20
21
22
# File 'lib/paperclip/vips.rb', line 6

def initialize(file, options = {}, attachment = nil)
  super

  geometry = options[:geometry].to_s
  @should_crop = geometry[-1, 1] == "#"
  @target_geometry = options.fetch(:string_geometry_parser, Geometry).parse(geometry)
  @current_geometry = options.fetch(:file_geometry_parser, Geometry).from_file(@file)
  @whiny = options.fetch(:whiny, true)

  @auto_orient = options.fetch(:auto_orient, true)
  @convert_options = options[:convert_options]

  @current_format = current_format(file).downcase
  @format = options[:format] || @current_format

  @basename = File.basename(@file.path, @current_format)
end

Instance Attribute Details

#auto_orientObject

Returns the value of attribute auto_orient.


3
4
5
# File 'lib/paperclip/vips.rb', line 3

def auto_orient
  @auto_orient
end

#convert_optionsObject

Returns the value of attribute convert_options.


3
4
5
# File 'lib/paperclip/vips.rb', line 3

def convert_options
  @convert_options
end

#current_geometryObject

Returns the value of attribute current_geometry.


3
4
5
# File 'lib/paperclip/vips.rb', line 3

def current_geometry
  @current_geometry
end

#formatObject

Returns the value of attribute format.


3
4
5
# File 'lib/paperclip/vips.rb', line 3

def format
  @format
end

#source_file_optionsObject

Returns the value of attribute source_file_options.


3
4
5
# File 'lib/paperclip/vips.rb', line 3

def source_file_options
  @source_file_options
end

#target_geometryObject

Returns the value of attribute target_geometry.


3
4
5
# File 'lib/paperclip/vips.rb', line 3

def target_geometry
  @target_geometry
end

#whinyObject

Returns the value of attribute whiny.


3
4
5
# File 'lib/paperclip/vips.rb', line 3

def whiny
  @whiny
end

Instance Method Details

#makeObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/paperclip/vips.rb', line 24

def make
  source = @file
  filename = [@basename, @format ? ".#{@format}" : ""].join
  destination = TempfileFactory.new.generate(filename)

  begin
    if @target_geometry.present?
      target_width = @target_geometry.width
      target_height = @target_geometry.height
      modifier = @target_geometry.modifier # e.g., ">", "#", etc.

      # Use thumbnail for efficient resizing and cropping
      crop = (modifier == "#")
      thumbnail = ::Vips::Image.thumbnail(
        source.path,
        target_width,
        height: crop ? target_height : nil,
        size: (modifier == ">" ? :down : :both), # ">" means shrink only
        crop: crop ? :centre : :none
      )

      # Additional cropping for exact "#" dimensions if needed
      if crop && (thumbnail.width != target_width || thumbnail.height != target_height)
        left = [0, (thumbnail.width - target_width) / 2].max
        top = [0, (thumbnail.height - target_height) / 2].max
        crop_width = [target_width, thumbnail.width - left].min
        crop_height = [target_height, thumbnail.height - top].min
        if crop_width.positive? && crop_height.positive?
          thumbnail = thumbnail.crop(left, top, crop_width,
            crop_height)
        end
      end
    end

    # Fallback to original image if no geometry
    thumbnail = ::Vips::Image.new_from_file(source.path) unless defined?(thumbnail) && thumbnail

    # Apply convert options
    thumbnail = process_convert_options(thumbnail)

    # Save the processed image
    save_thumbnail(thumbnail, destination.path)
  rescue StandardError => e
    puts e.message, e.backtrace
    if @whiny
      message = "There was an error processing the thumbnail for #{@basename}:\n#{e.message}"
      raise Paperclip::Error, message
    end
  end

  destination # Return the Tempfile object
end