Class: ZooniverseData::Helpers::Images::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/zooniverse_data/helpers/images.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, raise_exceptions: true, remove_original: true, optimize: true) ⇒ Converter

Returns a new instance of Converter.



80
81
82
83
84
85
# File 'lib/zooniverse_data/helpers/images.rb', line 80

def initialize(path: nil, raise_exceptions: true, remove_original: true, optimize: true)
  self.input_image = Image.new path: path, raise_exceptions: raise_exceptions
  self.remove_original = remove_original
  self.optimize = optimize
  self.flags = []
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



77
78
79
# File 'lib/zooniverse_data/helpers/images.rb', line 77

def flags
  @flags
end

#input_imageObject

Returns the value of attribute input_image.



77
78
79
# File 'lib/zooniverse_data/helpers/images.rb', line 77

def input_image
  @input_image
end

#optimizeObject

Returns the value of attribute optimize.



78
79
80
# File 'lib/zooniverse_data/helpers/images.rb', line 78

def optimize
  @optimize
end

#output_imageObject

Returns the value of attribute output_image.



77
78
79
# File 'lib/zooniverse_data/helpers/images.rb', line 77

def output_image
  @output_image
end

#raise_exceptionsObject

Returns the value of attribute raise_exceptions.



78
79
80
# File 'lib/zooniverse_data/helpers/images.rb', line 78

def raise_exceptions
  @raise_exceptions
end

#remove_originalObject

Returns the value of attribute remove_original.



78
79
80
# File 'lib/zooniverse_data/helpers/images.rb', line 78

def remove_original
  @remove_original
end

Instance Method Details

#adaptive_percentage_resize(percentage) ⇒ Object



116
117
118
# File 'lib/zooniverse_data/helpers/images.rb', line 116

def adaptive_percentage_resize(percentage)
  percentage_resize percentage, type: 'adaptive'
end

#adaptive_resize(width: nil, height: nil, force: true) ⇒ Object



105
106
107
# File 'lib/zooniverse_data/helpers/images.rb', line 105

def adaptive_resize(width: nil, height: nil, force: true)
  resize width: width, height: height, type: 'adaptive', force: force
end

#command(string) ⇒ Object



87
88
89
90
91
# File 'lib/zooniverse_data/helpers/images.rb', line 87

def command(string)
  tap do
    self.flags << string
  end
end

#crop(width: nil, height: nil, top: nil, left: nil) ⇒ Object



126
127
128
129
130
# File 'lib/zooniverse_data/helpers/images.rb', line 126

def crop(width: nil, height: nil, top: nil, left: nil)
  tap do
    self.flags << "-crop #{ width }x#{ height }+#{ left }+#{ top } +repage"
  end
end

#crop_center(width: nil, height: nil, top: 0, left: 0) ⇒ Object



132
133
134
135
136
# File 'lib/zooniverse_data/helpers/images.rb', line 132

def crop_center(width: nil, height: nil, top: 0, left: 0)
  tap do
    self.flags << "-gravity Center -crop #{ width }x#{ height }+#{ left }+#{ top } +repage"
  end
end

#negateObject Also known as: invert



138
139
140
141
142
# File 'lib/zooniverse_data/helpers/images.rb', line 138

def negate
  tap do
    self.flags << "-negate"
  end
end

#percentage_resize(percentage, type: nil) ⇒ Object



109
110
111
112
113
114
# File 'lib/zooniverse_data/helpers/images.rb', line 109

def percentage_resize(percentage, type: nil)
  tap do
    resize_type = type ? "#{ type }-resize" : 'resize'
    self.flags << "-#{ resize_type } #{ percentage }% +repage"
  end
end

#quality(percentage) ⇒ Object



120
121
122
123
124
# File 'lib/zooniverse_data/helpers/images.rb', line 120

def quality(percentage)
  tap do
    self.flags << "-quality #{ percentage }%"
  end
end

#resize(width: nil, height: nil, force: true, type: nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zooniverse_data/helpers/images.rb', line 93

def resize(width: nil, height: nil, force: true, type: nil)
  tap do
    resize_type = type ? "#{ type }-resize" : 'resize'

    if width && height
      self.flags << "-#{ resize_type } #{ width }x#{ height }#{ force ? '\!' : '' }"
    elsif width || height
      self.flags << "-#{ resize_type } #{ [width, height].join('x') }"
    end
  end
end

#to(path: nil, prefix: nil, postfix: nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/zooniverse_data/helpers/images.rb', line 149

def to(path: nil, prefix: nil, postfix: nil)
  raise ImageConversionError.new('Cannot convert an image without an output path') unless path || prefix || postfix
  tap do
    if prefix || postfix
      input_file = input_image.path
      input_path = File.dirname input_file
      input_ext = File.extname input_file
      input_name = File.basename(input_file).sub input_ext, ''

      input_name = "#{ prefix }_#{ input_name }" if prefix
      input_name = "#{ input_name }_#{ postfix }" if postfix
      path = "#{ input_path }/#{ input_name }#{ input_ext }"
    end

    self.output_image = Image.new path: path, raise_exceptions: raise_exceptions
  end
end

#writeObject



167
168
169
170
171
172
173
174
175
176
# File 'lib/zooniverse_data/helpers/images.rb', line 167

def write
  raise ImageConversionError.new('Cannot convert an image without an output path') unless output_image

  output_image.tap do
    success = system "convert #{ input_image.path } #{ flags.join(' ') } #{ output_image.path }"
    raise ImageConversionError.new('Image conversion failed') unless success
    `rm -f '#{ input_image.path }'` if remove_original && input_image.path != output_image.path
    output_image.optimize if optimize
  end
end

#write_to(path: nil, prefix: nil, postfix: nil) ⇒ Object



145
146
147
# File 'lib/zooniverse_data/helpers/images.rb', line 145

def write_to(path: nil, prefix: nil, postfix: nil)
  to(path: path, prefix: prefix, postfix: postfix).write
end