Class: Paperclip::Processor

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

Overview

Paperclip processors allow you to modify attached files when they are attached in any way you are able. Paperclip itself uses command-line programs for its included Thumbnail processor, but custom processors are not required to follow suit.

Processors are required to be defined inside the Paperclip module and are also required to be a subclass of Paperclip::Processor. There is only one method you must implement to properly be a subclass: #make, but #initialize may also be of use. #initialize accepts 3 arguments: the file that will be operated on (which is an instance of File), a hash of options that were defined in has_attached_file’s style hash, and the Paperclip::Attachment itself. These are set as instance variables that can be used within ‘#make`.

#make must return an instance of File (Tempfile is acceptable) which contains the results of the processing.

See Paperclip.run for more information about using command-line utilities from within Processors.

Direct Known Subclasses

Thumbnail

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}, attachment = nil) ⇒ Processor

Returns a new instance of Processor.



26
27
28
29
30
# File 'lib/paperclip/processor.rb', line 26

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

Instance Attribute Details

#attachmentObject

Returns the value of attribute attachment.



24
25
26
# File 'lib/paperclip/processor.rb', line 24

def attachment
  @attachment
end

#fileObject

Returns the value of attribute file.



24
25
26
# File 'lib/paperclip/processor.rb', line 24

def file
  @file
end

#optionsObject

Returns the value of attribute options.



24
25
26
# File 'lib/paperclip/processor.rb', line 24

def options
  @options
end

Class Method Details

.make(file, options = {}, attachment = nil) ⇒ Object



34
35
36
# File 'lib/paperclip/processor.rb', line 34

def self.make(file, options = {}, attachment = nil)
  new(file, options, attachment).make
end

Instance Method Details

#convert(arguments = "", local_options = {}) ⇒ Object

The convert method runs the convert binary with the provided arguments. See Paperclip.run for the available options.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/paperclip/processor.rb', line 40

def convert(arguments = "", local_options = {})
  command =
    if Paperclip.imagemagick7? # IMv7 on any OS
      "magick"
    elsif Paperclip.options[:is_windows] # IMv6 on Windows
      "magick convert"
    else
      "convert"
    end
  Paperclip.run(
    command,
    arguments,
    local_options,
  )
end

#identify(arguments = "", local_options = {}) ⇒ Object

The identify method runs the identify binary with the provided arguments. See Paperclip.run for the available options.



58
59
60
61
62
63
64
# File 'lib/paperclip/processor.rb', line 58

def identify(arguments = "", local_options = {})
  Paperclip.run(
    (Paperclip.options[:is_windows] || Paperclip.imagemagick7?) ? "magick identify" : "identify",
    arguments,
    local_options,
  )
end

#makeObject



32
# File 'lib/paperclip/processor.rb', line 32

def make; end

#vips(arguments = "", local_options = {}) ⇒ Object

Runs libvips command



67
68
69
# File 'lib/paperclip/processor.rb', line 67

def vips(arguments = "", local_options = {})
  Paperclip.run("vips", arguments, local_options)
end

#vips_image(file_path, **options) ⇒ Vips::Image

Returns a Vips::Image object for the given file path. This provides access to the full ruby-vips API for image manipulation.

Parameters:

  • file_path (String)

    Path to the image file

  • options (Hash)

    Options to pass to Vips::Image.new_from_file

Returns:

  • (Vips::Image)

    The loaded image



81
82
83
84
85
86
87
88
# File 'lib/paperclip/processor.rb', line 81

def vips_image(file_path, **options)
  begin
    require "vips"
  rescue LoadError
    raise Errors::CommandNotFoundError.new("Could not load ruby-vips. Please install libvips.")
  end
  Vips::Image.new_from_file(file_path, **options)
end

#vipsheader(arguments = "", local_options = {}) ⇒ Object

Runs libvips header command



72
73
74
# File 'lib/paperclip/processor.rb', line 72

def vipsheader(arguments = "", local_options = {})
  Paperclip.run("vipsheader", arguments, local_options)
end