Class: Libis::Format::Converter::ImageConverter
- Defined in:
- lib/libis/format/converter/image_converter.rb
Overview
noinspection RubyTooManyInstanceVariablesInspection
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #assemble_and_convert(sources, target, format) ⇒ Object
- #colorspace(value) ⇒ Object
- #convert(source, target, format, opts = {}) ⇒ Object
- #delete_date(value = true) ⇒ Object
- #dpi(value) ⇒ Object
- #flatten(value = true) ⇒ Object
- #image_convert(_) ⇒ Object
-
#initialize ⇒ ImageConverter
constructor
A new instance of ImageConverter.
- #page(nr) ⇒ Object
- #profile(icc) ⇒ Object
- #quality(value) ⇒ Object
- #quiet(v) ⇒ Object
- #resample(value) ⇒ Object
- #resize(geometry) ⇒ Object
- #scale(percent) ⇒ Object
-
#watermark(options = {}) ⇒ Object
Create or use a watermark image.
Methods inherited from Base
inherited, using_temp, #using_temp
Constructor Details
#initialize ⇒ ImageConverter
Returns a new instance of ImageConverter.
38 39 40 41 |
# File 'lib/libis/format/converter/image_converter.rb', line 38 def initialize @wm_image = nil super end |
Class Method Details
.input_types ⇒ Object
25 26 27 |
# File 'lib/libis/format/converter/image_converter.rb', line 25 def self.input_types [:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2] end |
.multipage?(format) ⇒ Boolean
34 35 36 |
# File 'lib/libis/format/converter/image_converter.rb', line 34 def self.multipage?(format) [:PDF, :TIFF, :GIF, :PBM, :PGM, :PPM].include?(format) end |
.output_types(format = nil) ⇒ Object
29 30 31 32 |
# File 'lib/libis/format/converter/image_converter.rb', line 29 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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/libis/format/converter/image_converter.rb', line 188 def assemble_and_convert(sources, target, format) warn 'Received multiple images as input and single page format as target.' unless self.class.multipage?(format) converted_pages = sources.inject([]) do |list, path| # noinspection RubyArgCount 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| b.append unless self.class.multipage?(format) 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
79 80 81 |
# File 'lib/libis/format/converter/image_converter.rb', line 79 def colorspace(value) @options[:colorspace] = value end |
#convert(source, target, format, opts = {}) ⇒ Object
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 182 183 184 185 186 |
# File 'lib/libis/format/converter/image_converter.rb', line 154 def convert(source, target, format, opts = {}) super FileUtils.mkpath(File.dirname(target)) if source.is_a? Array assemble_and_convert(source, target, format) elsif File.directory?(source) source_list = Dir[File.join(source, '**', '*')].reject {|p| File.directory? p} assemble_and_convert(source_list, target, format) else image = MiniMagick::Image.open(source) { |b| b.quiet } if image.pages.size > 1 if @page convert_image(image.pages[@page].path, target, format) else # noinspection RubyBlockToMethodReference assemble_and_convert(image.pages.map {|page| page.path}, target, format) end else convert_image(source, target, format) end end target end |
#delete_date(value = true) ⇒ Object
83 84 85 |
# File 'lib/libis/format/converter/image_converter.rb', line 83 def delete_date(value = true) @delete_date = !!value end |
#dpi(value) ⇒ Object
67 68 69 |
# File 'lib/libis/format/converter/image_converter.rb', line 67 def dpi(value) @options[:density] = value end |
#flatten(value = true) ⇒ Object
75 76 77 |
# File 'lib/libis/format/converter/image_converter.rb', line 75 def flatten(value = true) @options[:flatten] = !!value end |
#image_convert(_) ⇒ Object
43 44 45 |
# File 'lib/libis/format/converter/image_converter.rb', line 43 def image_convert(_) #force usage of this converter end |
#page(nr) ⇒ Object
51 52 53 |
# File 'lib/libis/format/converter/image_converter.rb', line 51 def page(nr) @page = nr end |
#profile(icc) ⇒ Object
87 88 89 |
# File 'lib/libis/format/converter/image_converter.rb', line 87 def profile(icc) @profile = icc end |
#quality(value) ⇒ Object
63 64 65 |
# File 'lib/libis/format/converter/image_converter.rb', line 63 def quality(value) @options[:quality] = value end |
#quiet(v) ⇒ Object
47 48 49 |
# File 'lib/libis/format/converter/image_converter.rb', line 47 def quiet(v) @quiet = !!v end |
#resample(value) ⇒ Object
71 72 73 |
# File 'lib/libis/format/converter/image_converter.rb', line 71 def resample(value) @options[:resample] = value end |
#resize(geometry) ⇒ Object
59 60 61 |
# File 'lib/libis/format/converter/image_converter.rb', line 59 def resize(geometry) @options[:resize] = geometry end |
#scale(percent) ⇒ Object
55 56 57 |
# File 'lib/libis/format/converter/image_converter.rb', line 55 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
- tiles: number of tiles of the watermark - default 4
- 0: no tiling, so only 1 watermark will be placed with the original size
- 1: 1 tile, so the watermark will be scaled up to fill the image
- n > 1: minimum n tiles in both directions
- n < 0: tile without scaling the watermark
- size: same as tiles - for backwards compatibility
- resize: fraction 0.0 - 1.0
- gap: size of the gap between watermark instances. Fractions as percentage of widht/height. - default 0.2
- opacity: opacity of the watermark (fraction 0.0 - 1.0) - default 0.1
- gravity: center point of the overlay - default 'center'
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.
114 115 116 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 |
# File 'lib/libis/format/converter/image_converter.rb', line 114 def watermark( = {}) text = [:text] || '© LIBIS' @wm_tiles = ([:tiles] || '4').to_i @wm_tiles ||= ([:size] || '4').to_i @wm_resize = (([:resize]).to_f * 100).to_i if [:resize] @wm_opacity = (([:opacity] || 0.1).to_f * 100).to_i @wm_composition = [:composition] || 'modulate' @wm_gravity = [:gravity] || 'center' @wm_gap = (([:gap] || 0.2).to_f * 100).to_i rotation = 360 - ([:rotation] || 30).to_i @wm_image = MiniMagick::Image.new([:file]) if [:file] unless @wm_image && @wm_image.valid? image = [: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 << image end if [: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 |