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.



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

def initialize
  @wm_image = nil
  super
end

Class Method Details

.input_typesObject



17
18
19
# File 'lib/libis/format/converter/image_converter.rb', line 17

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

.output_types(format = nil) ⇒ Object



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

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

Instance Method Details

#assemble_and_convert(sources, target, format) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/libis/format/converter/image_converter.rb', line 165

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



59
60
61
# File 'lib/libis/format/converter/image_converter.rb', line 59

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

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
# File 'lib/libis/format/converter/image_converter.rb', line 125

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
      assemble_and_convert(image.pages.map {|page| page.path}, target, format)
    else
      convert_image(source, target, format)
    end
  end

  target

end

#delete_dateObject



63
64
65
# File 'lib/libis/format/converter/image_converter.rb', line 63

def delete_date
  @delete_date = true
end

#dpi(value) ⇒ Object



47
48
49
# File 'lib/libis/format/converter/image_converter.rb', line 47

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

#flattenObject



55
56
57
# File 'lib/libis/format/converter/image_converter.rb', line 55

def flatten
  @flags[:flatten] = true
end

#image_convert(_) ⇒ Object



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

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

#profile(icc) ⇒ Object



67
68
69
# File 'lib/libis/format/converter/image_converter.rb', line 67

def profile(icc)
  @profile = icc
end

#quality(value) ⇒ Object



43
44
45
# File 'lib/libis/format/converter/image_converter.rb', line 43

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

#resample(value) ⇒ Object



51
52
53
# File 'lib/libis/format/converter/image_converter.rb', line 51

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

#resize(geometry) ⇒ Object



39
40
41
# File 'lib/libis/format/converter/image_converter.rb', line 39

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

#scale(percent) ⇒ Object



35
36
37
# File 'lib/libis/format/converter/image_converter.rb', line 35

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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/libis/format/converter/image_converter.rb', line 87

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