Class: Libis::Format::Converter::ImageConverter

Inherits:
Base
  • Object
show all
Defined in:
lib/libis/format/converter/image_converter.rb

Instance Attribute Summary

Attributes inherited from Base

#flags, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, using_temp, #using_temp

Constructor Details

#initializeImageConverter

Returns a new instance of ImageConverter.



32
33
34
35
# File 'lib/libis/format/converter/image_converter.rb', line 32

def initialize
  @wm_image = nil
  super
end

Class Method Details

.input_typesObject



23
24
25
# File 'lib/libis/format/converter/image_converter.rb', line 23

def self.input_types
  [:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2]
end

.output_types(format = nil) ⇒ Object



27
28
29
30
# File 'lib/libis/format/converter/image_converter.rb', line 27

def self.output_types(format = nil)
  return [] unless input_types.include?(format)
  [:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2]
end

Instance Method Details

#assemble_and_convert(sources, target, format) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/libis/format/converter/image_converter.rb', line 183

def assemble_and_convert(sources, target, format)
  converted_pages = sources.inject([]) do |list, path|
    converted = Tempfile.new(['page-', ".#{Libis::Format::TypeDatabase.type_extentions(format).first}"])
    convert_image(path, converted.path, format)
    list << converted
  end
  MiniMagick::Tool::Convert.new do |b|
    converted_pages.each { |page| b << page.path }
    b << target
  end
  converted_pages.each do |temp_file|
    temp_file.close
    temp_file.unlink
  end
end

#colorspace(value) ⇒ Object



73
74
75
# File 'lib/libis/format/converter/image_converter.rb', line 73

def colorspace(value)
  @options[:colorspace] = value
end

#convert(source, target, format, opts = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/libis/format/converter/image_converter.rb', line 139

def convert(source, target, format, opts = {})
  super

  FileUtils.mkpath(File.dirname(target))

  if source.is_a? Array
    sources = source

    unless [:PDF, :TIFF, :GIF, :PBM, :PGM, :PPM].include? format
      error 'Can ony assemble multiple images into multi-page/layer format'
      return nil
    end

    assemble_and_convert(sources, target, format)

  elsif File.directory?(source)
    sources = Dir[File.join(source, '**', '*')].reject { |p| File.directory? p }

    unless [:TIFF, :PDF].include? format
      error 'Can ony assemble multiple images into multi-page/layer format'
      return nil
    end

    assemble_and_convert(sources, target, format)

  else

    image = MiniMagick::Image.new(source)

    if image.pages.size > 1
      if @page
        convert_image(image.pages[@page].path, target, format)
      else
        assemble_and_convert(image.pages.map { |page| page.path }, target, format)
      end
    else
      convert_image(source, target, format)
    end
  end

  target

end

#delete_dateObject



77
78
79
# File 'lib/libis/format/converter/image_converter.rb', line 77

def delete_date
  @delete_date = true
end

#dpi(value) ⇒ Object



61
62
63
# File 'lib/libis/format/converter/image_converter.rb', line 61

def dpi(value)
  @options[:density] = value
end

#flattenObject



69
70
71
# File 'lib/libis/format/converter/image_converter.rb', line 69

def flatten
  @flags[:flatten] = true
end

#image_convert(_) ⇒ Object



37
38
39
# File 'lib/libis/format/converter/image_converter.rb', line 37

def image_convert(_)
  #force usage of this converter
end

#page(nr) ⇒ Object



45
46
47
# File 'lib/libis/format/converter/image_converter.rb', line 45

def page(nr)
  @page = nr
end

#profile(icc) ⇒ Object



81
82
83
# File 'lib/libis/format/converter/image_converter.rb', line 81

def profile(icc)
  @profile = icc
end

#quality(value) ⇒ Object



57
58
59
# File 'lib/libis/format/converter/image_converter.rb', line 57

def quality(value)
  @options[:quality] = value
end

#quiet(v) ⇒ Object



41
42
43
# File 'lib/libis/format/converter/image_converter.rb', line 41

def quiet(v)
  @flags[:quiet] = !!v
end

#resample(value) ⇒ Object



65
66
67
# File 'lib/libis/format/converter/image_converter.rb', line 65

def resample(value)
  @options[:resample] = value
end

#resize(geometry) ⇒ Object



53
54
55
# File 'lib/libis/format/converter/image_converter.rb', line 53

def resize(geometry)
  @options[:resize] = geometry
end

#scale(percent) ⇒ Object



49
50
51
# File 'lib/libis/format/converter/image_converter.rb', line 49

def scale(percent)
  @options[:scale] = percent
end

#watermark(options = {}) ⇒ Object

Create or use a watermark image.

The watermark options are:

- file: watermark image to use
- text: text to create a watermark from
- rotation: rotation of the watermark text (counter clockwise in degrees; integer number) - default 30
- size: size of the watermark (integer > 0, 1/n of image size) - default 4
- opacity: opacity of the watermark (fraction 0.0 - 1.0) - default 0.3
- gap: size of the gap between watermark instances. Fractions as percentage of widht/height. - default 0.2

If both options are given, the file will be used as-is if it exists and is a valid image file. Otherwise the file will be created or overwritten with a newly created watermark image.

The created watermark file will be a PNG image with transparent background containing the supplied text slanted by 30 degrees counter-clockwise.

Parameters:

  • options (Hash) (defaults to: {})

    Hash of options for watermark creation.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/libis/format/converter/image_converter.rb', line 101

def watermark(options = {})
  text = options[:text] || '© LIBIS'
  @wm_size = (options[:size] || '4').to_i
  @wm_opacity = ((options[:opacity] || 0.1).to_f * 100).to_i
  @wm_composition = options[:composition] || 'modulate'
  gap = ((options[:gap] || 0.2).to_f * 100).to_i
  rotation = 360 - (options[:rotation] || 30).to_i
  @wm_image = MiniMagick::Image.new(options[:file]) if options[:file]
  unless @wm_image && @wm_image.valid?
    image = options[:file] || (Dir::Tmpname.create(%w(wm_image .png)) { |_|})
    # noinspection RubyResolve
    MiniMagick::Tool::Convert.new do |convert|
      # noinspection RubyLiteralArrayInspection
      convert.quiet
      convert.background 'transparent'
      convert.size('2000x2000')
      convert.gravity 'Center'
      convert.font('Helvetica').fill('black').pointsize(72) #.stroke('black').strokewidth(1)
      convert << "label:#{text}"
      convert.rotate rotation
      convert.trim.repage.+
      convert.bordercolor('transparent').border("#{gap}%")
      convert << image
    end
    if options[:file]
      @wm_image = MiniMagick::Image.new(image)
    else
      @wm_image = MiniMagick::Image.open(image)
      File.delete(image)
    end
    # noinspection RubyResolve
    unless @wm_image.valid?
      error "Problem creating watermark image '#{image}'."
      @wm_image = nil
    end
  end
end