Class: Paperclip::GMagick

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip_processors/gmagick.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GMagick.



6
7
8
9
10
11
12
13
14
15
# File 'lib/paperclip_processors/gmagick.rb', line 6

def initialize(file, options = {}, attachment = nil)
  @geometry            = options[:geometry]
  @file                = file
  @format              = options[:format]
  @current_format      = File.extname(@file.path)
  @basename            = File.basename(@file.path, @current_format)

  @target_geometry     = (options[:string_geometry_parser] || Geometry).parse(@geometry)
  @current_geometry    = (options[:file_geometry_parser] || Geometry).from_file(@file)
end

Instance Attribute Details

#convert_optionsObject

Returns the value of attribute convert_options.



4
5
6
# File 'lib/paperclip_processors/gmagick.rb', line 4

def convert_options
  @convert_options
end

#source_file_optionsObject

Returns the value of attribute source_file_options.



4
5
6
# File 'lib/paperclip_processors/gmagick.rb', line 4

def source_file_options
  @source_file_options
end

Class Method Details

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



17
18
19
# File 'lib/paperclip_processors/gmagick.rb', line 17

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

Instance Method Details

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



45
46
47
# File 'lib/paperclip_processors/gmagick.rb', line 45

def convert(arguments = "", local_options = {})
  Paperclip.run('gm convert', arguments, local_options)
end

#convert_options?Boolean

Returns true if the image is meant to make use of additional convert options.

Returns:

  • (Boolean)


73
74
75
# File 'lib/paperclip_processors/gmagick.rb', line 73

def convert_options?
  !@convert_options.nil? && !@convert_options.empty?
end

#crop?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/paperclip_processors/gmagick.rb', line 64

def crop?
  @crop
end

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



49
50
51
# File 'lib/paperclip_processors/gmagick.rb', line 49

def identify(arguments = "", local_options = {})
  Paperclip.run('gm identify', arguments, local_options)
end

#makeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/paperclip_processors/gmagick.rb', line 21

def make
  src = @file
  dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])

  begin
    parameters = []
    parameters << source_file_options
    parameters << ":source"
    parameters << transformation_command
    parameters << convert_options
    parameters << ":dest"

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

    success = convert(parameters, :source => "#{File.expand_path(src.path)}#{'[0]'}", :dest => File.expand_path(dst.path))
  rescue Cocaine::ExitStatusError => e
    raise Paperclip::Error, "There was an error processing the thumbnail for #{@basename}" if @whiny
  rescue Cocaine::CommandNotFoundError => e
    raise Paperclip::Errors::CommandNotFoundError.new("Could not run the `gm convert` command. Please install GraphicsMagick.")
  end

  dst
end

#transformation_commandObject

Returns the command ImageMagick’s convert needs to transform the image into the thumbnail.



55
56
57
58
59
60
61
62
# File 'lib/paperclip_processors/gmagick.rb', line 55

def transformation_command
  scale = transformation_to(@geometry)
  trans = []
  #trans << "-auto-orient" if auto_orient
  trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
  #trans << "-crop" << %["#{crop}"] << "+repage" if crop
  trans
end

#transformation_to(geometry) ⇒ Object



68
69
70
# File 'lib/paperclip_processors/gmagick.rb', line 68

def transformation_to(geometry)
  geometry.gsub("#","")
end