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

Constructor Details

#initialize ⇒ ImageConverter

Returns a new instance of ImageConverter.



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

def initialize
  @wm_image = nil
  super
end

Class Method Details

.input_types(format = nil) ⇒ Object



19
20
21
# File 'lib/libis/format/converter/image_converter.rb', line 19

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

.output_types(_ = nil) ⇒ Object



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

def self.output_types(_ = nil)
  [:TIFF, :JPG, :PNG, :BMP, :GIF, :JP2, :PDF]
end

Instance Method Details

#assemble_and_convert(sources, target, format) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/libis/format/converter/image_converter.rb', line 157

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



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

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

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



117
118
119
120
121
122
123
124
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
# File 'lib/libis/format/converter/image_converter.rb', line 117

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

#dpi(value) ⇒ Object



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

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

#flatten ⇒ Object



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

def flatten
  @flags[:flatten] = true
end

#image_convert(_) ⇒ Object



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

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

#quality(value) ⇒ Object



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

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

#resample(value) ⇒ Object



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

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

#resize(geometry) ⇒ Object



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

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

#scale(percent) ⇒ Object



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

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.



80
81
82
83
84
85
86
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
# File 'lib/libis/format/converter/image_converter.rb', line 80

def watermark(options = {})
  text = options[:text] || '© LIBIS'
  image = options[:file] || (Dir::Tmpname.create(%w(wm_image .png)) { |_|})
  @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(image)
  unless @wm_image.valid?
    # noinspection RubyResolve
    MiniMagick::Tool::Convert.new do |convert|
      # noinspection RubyLiteralArrayInspection
      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