libis-format 1.0.5 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +5 -1
- data/.travis.yml +32 -24
- data/README.md +2 -2
- data/base/Dockerfile +24 -2
- data/base/Dockerfile.alpine +20 -0
- data/base/Dockerfile.rvm +56 -0
- data/base/rework_path +20 -0
- data/docker_cfg.yml +1 -0
- data/lib/libis/format.rb +12 -3
- data/lib/libis/format/cli/convert.rb +4 -4
- data/lib/libis/format/config.rb +16 -12
- data/lib/libis/format/converter/audio_converter.rb +2 -36
- data/lib/libis/format/converter/base.rb +22 -8
- data/lib/libis/format/converter/chain.rb +3 -3
- data/lib/libis/format/converter/image_assembler.rb +82 -0
- data/lib/libis/format/converter/image_converter.rb +20 -138
- data/lib/libis/format/converter/image_splitter.rb +84 -0
- data/lib/libis/format/converter/image_watermarker.rb +261 -0
- data/lib/libis/format/converter/jp2_converter.rb +1 -1
- data/lib/libis/format/converter/office_converter.rb +2 -2
- data/lib/libis/format/converter/pdf_assembler.rb +66 -0
- data/lib/libis/format/converter/pdf_converter.rb +6 -132
- data/lib/libis/format/converter/pdf_metadata.rb +82 -0
- data/lib/libis/format/converter/pdf_optimizer.rb +67 -0
- data/lib/libis/format/converter/pdf_protecter.rb +147 -0
- data/lib/libis/format/converter/pdf_selecter.rb +83 -0
- data/lib/libis/format/converter/pdf_splitter.rb +70 -0
- data/lib/libis/format/converter/pdf_watermarker_header.rb +71 -0
- data/lib/libis/format/converter/pdf_watermarker_image.rb +76 -0
- data/lib/libis/format/converter/pdf_watermarker_text.rb +93 -0
- data/lib/libis/format/converter/spreadsheet_converter.rb +2 -2
- data/lib/libis/format/converter/video_converter.rb +1 -1
- data/lib/libis/format/identifier.rb +3 -3
- data/lib/libis/format/info.rb +27 -0
- data/lib/libis/format/library.rb +147 -0
- data/lib/libis/format/tool.rb +4 -1
- data/lib/libis/format/tool/extension_identification.rb +4 -4
- data/lib/libis/format/tool/identification_tool.rb +6 -6
- data/lib/libis/format/tool/pdf_merge.rb +3 -3
- data/lib/libis/format/tool/{pdf_copy.rb → pdf_metadata.rb} +5 -5
- data/lib/libis/format/tool/pdf_protect.rb +47 -0
- data/lib/libis/format/tool/pdf_select.rb +47 -0
- data/lib/libis/format/tool/pdf_split.rb +4 -4
- data/lib/libis/format/tool/pdf_watermark.rb +47 -0
- data/lib/libis/format/tool/spreadsheet_to_ods.rb +1 -0
- data/lib/libis/format/version.rb +1 -1
- data/lib/libis/format/yaml_loader.rb +71 -0
- data/libis-format.gemspec +3 -2
- data/tools/PdfTool.jar +0 -0
- data/tools/bcpkix-jdk15on-167.jar +0 -0
- data/tools/bcprov-jdk15on-167.jar +0 -0
- metadata +32 -13
- data/lib/libis/format/type_database.rb +0 -134
- data/lib/libis/format/type_database_impl.rb +0 -120
- data/tools/bcpkix-jdk15on-1.49.jar +0 -0
- data/tools/bcprov-jdk15on-1.49.jar +0 -0
@@ -5,7 +5,7 @@ require 'deep_dive'
|
|
5
5
|
|
6
6
|
require 'libis/tools/logger'
|
7
7
|
require 'libis/tools/extend/hash'
|
8
|
-
require 'libis/format/
|
8
|
+
require 'libis/format/library'
|
9
9
|
|
10
10
|
module Libis
|
11
11
|
module Format
|
@@ -83,8 +83,8 @@ module Libis
|
|
83
83
|
target = target_file
|
84
84
|
|
85
85
|
if i < size - 1
|
86
|
-
target += ".temp.#{
|
87
|
-
target += ".#{
|
86
|
+
target += ".temp.#{Libis::Format::Library.get_field(target_type, :extensions).first}"
|
87
|
+
target += ".#{Libis::Format::Library.get_field(target_type, :extensions).first}" while File.exist? target
|
88
88
|
temp_files << target
|
89
89
|
end
|
90
90
|
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
require 'libis/format/identifier'
|
5
|
+
|
6
|
+
require 'mini_magick'
|
7
|
+
|
8
|
+
MiniMagick.logger.level = ::Logger::UNKNOWN
|
9
|
+
|
10
|
+
MiniMagick.configure do |config|
|
11
|
+
# config.cli = :graphicsmagick
|
12
|
+
config.validate_on_create = false
|
13
|
+
config.validate_on_write = false
|
14
|
+
config.whiny = false
|
15
|
+
end
|
16
|
+
|
17
|
+
module Libis
|
18
|
+
module Format
|
19
|
+
module Converter
|
20
|
+
|
21
|
+
# noinspection RubyTooManyInstanceVariablesInspection,DuplicatedCode
|
22
|
+
class ImageAssembler < Libis::Format::Converter::Base
|
23
|
+
|
24
|
+
def self.input_types
|
25
|
+
[:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2]
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.output_types(format = nil)
|
29
|
+
return [] unless input_types.include?(format) if format
|
30
|
+
[:PDF, :TIFF, :GIF, :PBM, :PGM, :PPM]
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.category
|
34
|
+
:assembler
|
35
|
+
end
|
36
|
+
|
37
|
+
def image_assemble(_)
|
38
|
+
#force usage of this converter
|
39
|
+
end
|
40
|
+
|
41
|
+
def quiet(v)
|
42
|
+
@quiet = !!v
|
43
|
+
end
|
44
|
+
|
45
|
+
def convert(source, target, format, opts = {})
|
46
|
+
super
|
47
|
+
|
48
|
+
FileUtils.mkpath(File.dirname(target))
|
49
|
+
|
50
|
+
if source.is_a? Array
|
51
|
+
assemble(source, target, format)
|
52
|
+
elsif File.directory?(source)
|
53
|
+
source_list = Dir[File.join(source, '**', '*')].reject {|p| File.directory? p}
|
54
|
+
assemble(source_list, target, format)
|
55
|
+
else
|
56
|
+
image = MiniMagick::Image.open(source) {|b| b.quiet}
|
57
|
+
if image.pages.size > 1
|
58
|
+
assemble(image.pages.map {|page| page.path}, target, format)
|
59
|
+
else
|
60
|
+
assemble([source], target, format)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
target
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def assemble(sources, target, format)
|
71
|
+
MiniMagick::Tool::Convert.new do |b|
|
72
|
+
sources.each {|source| b << source}
|
73
|
+
convert.format(format)
|
74
|
+
b << target
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -4,7 +4,6 @@ require_relative 'base'
|
|
4
4
|
require 'libis/format/identifier'
|
5
5
|
|
6
6
|
require 'mini_magick'
|
7
|
-
require 'fileutils'
|
8
7
|
|
9
8
|
MiniMagick.logger.level = ::Logger::UNKNOWN
|
10
9
|
|
@@ -23,11 +22,11 @@ module Libis
|
|
23
22
|
class ImageConverter < Libis::Format::Converter::Base
|
24
23
|
|
25
24
|
def self.input_types
|
26
|
-
[:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2]
|
25
|
+
[:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2, :PBM, :PGM, :PPM]
|
27
26
|
end
|
28
27
|
|
29
28
|
def self.output_types(format = nil)
|
30
|
-
return [] unless input_types.include?(format)
|
29
|
+
return [] unless input_types.include?(format) if format
|
31
30
|
[:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2]
|
32
31
|
end
|
33
32
|
|
@@ -46,109 +45,57 @@ module Libis
|
|
46
45
|
|
47
46
|
def quiet(v)
|
48
47
|
@quiet = !!v
|
48
|
+
self
|
49
49
|
end
|
50
50
|
|
51
51
|
def page(nr)
|
52
52
|
@page = nr
|
53
|
+
self
|
53
54
|
end
|
54
55
|
|
55
56
|
def scale(percent)
|
56
57
|
@options[:scale] = percent
|
58
|
+
self
|
57
59
|
end
|
58
60
|
|
59
61
|
def resize(geometry)
|
60
62
|
@options[:resize] = geometry
|
63
|
+
self
|
61
64
|
end
|
62
65
|
|
63
66
|
def quality(value)
|
64
67
|
@options[:quality] = value
|
68
|
+
self
|
65
69
|
end
|
66
70
|
|
67
71
|
def dpi(value)
|
68
72
|
@options[:density] = value
|
73
|
+
self
|
69
74
|
end
|
70
75
|
|
71
76
|
def resample(value)
|
72
77
|
@options[:resample] = value
|
78
|
+
self
|
73
79
|
end
|
74
80
|
|
75
81
|
def flatten(value = true)
|
76
|
-
@options[:flatten] =
|
82
|
+
@options[:flatten] = value
|
83
|
+
self
|
77
84
|
end
|
78
85
|
|
79
86
|
def colorspace(value)
|
80
87
|
@options[:colorspace] = value
|
88
|
+
self
|
81
89
|
end
|
82
90
|
|
83
91
|
def delete_date(value = true)
|
84
|
-
@delete_date =
|
92
|
+
@delete_date = value
|
93
|
+
self
|
85
94
|
end
|
86
95
|
|
87
96
|
def profile(icc)
|
88
97
|
@profile = icc
|
89
|
-
|
90
|
-
|
91
|
-
# Create or use a watermark image.
|
92
|
-
#
|
93
|
-
# The watermark options are:
|
94
|
-
# - file: watermark image to use
|
95
|
-
# - text: text to create a watermark from
|
96
|
-
# - rotation: rotation of the watermark text (counter clockwise in degrees; integer number) - default 30
|
97
|
-
# - tiles: number of tiles of the watermark - default 4
|
98
|
-
# - 0: no tiling, so only 1 watermark will be placed with the original size
|
99
|
-
# - 1: 1 tile, so the watermark will be scaled up to fill the image
|
100
|
-
# - n > 1: minimum n tiles in both directions
|
101
|
-
# - n < 0: tile without scaling the watermark
|
102
|
-
# - size: same as tiles - for backwards compatibility
|
103
|
-
# - resize: fraction 0.0 - 1.0
|
104
|
-
# - gap: size of the gap between watermark instances. Fractions as percentage of widht/height. - default 0.2
|
105
|
-
# - opacity: opacity of the watermark (fraction 0.0 - 1.0) - default 0.1
|
106
|
-
# - gravity: center point of the overlay - default 'center'
|
107
|
-
# If both options are given, the file will be used as-is if it exists and is a valid image file. Otherwise the
|
108
|
-
# file will be created or overwritten with a newly created watermark image.
|
109
|
-
#
|
110
|
-
# The created watermark file will be a PNG image with transparent background containing the supplied text
|
111
|
-
# slanted by 30 degrees counter-clockwise.
|
112
|
-
#
|
113
|
-
# @param [Hash] options Hash of options for watermark creation.
|
114
|
-
def watermark(options = {})
|
115
|
-
text = options[:text] || '© LIBIS'
|
116
|
-
@wm_tiles = (options[:tiles] || '4').to_i
|
117
|
-
@wm_tiles ||= (options[:size] || '4').to_i
|
118
|
-
@wm_resize = ((options[:resize]).to_f * 100).to_i if options[:resize]
|
119
|
-
@wm_opacity = ((options[:opacity] || 0.1).to_f * 100).to_i
|
120
|
-
@wm_composition = options[:composition] || 'modulate'
|
121
|
-
@wm_gravity = options[:gravity] || 'center'
|
122
|
-
@wm_gap = ((options[:gap] || 0.2).to_f * 100).to_i
|
123
|
-
rotation = 360 - (options[:rotation] || 30).to_i
|
124
|
-
@wm_image = MiniMagick::Image.new(options[:file]) if options[:file]
|
125
|
-
unless @wm_image && @wm_image.valid?
|
126
|
-
image = options[:file] || (Dir::Tmpname.create(%w(wm_image .png)) {|_|})
|
127
|
-
# noinspection RubyResolve
|
128
|
-
MiniMagick::Tool::Convert.new do |convert|
|
129
|
-
# noinspection RubyLiteralArrayInspection
|
130
|
-
convert.quiet
|
131
|
-
convert.background 'transparent'
|
132
|
-
convert.size('2000x2000')
|
133
|
-
convert.gravity 'Center'
|
134
|
-
convert.font('Helvetica').fill('black').pointsize(72) #.stroke('black').strokewidth(1)
|
135
|
-
convert << "label:#{text}"
|
136
|
-
convert.rotate rotation
|
137
|
-
convert.trim.repage.+
|
138
|
-
convert << image
|
139
|
-
end
|
140
|
-
if options[:file]
|
141
|
-
@wm_image = MiniMagick::Image.new(image)
|
142
|
-
else
|
143
|
-
@wm_image = MiniMagick::Image.open(image)
|
144
|
-
File.delete(image)
|
145
|
-
end
|
146
|
-
# noinspection RubyResolve
|
147
|
-
unless @wm_image.valid?
|
148
|
-
error "Problem creating watermark image '#{image}'."
|
149
|
-
@wm_image = nil
|
150
|
-
end
|
151
|
-
end
|
98
|
+
self
|
152
99
|
end
|
153
100
|
|
154
101
|
def convert(source, target, format, opts = {})
|
@@ -156,90 +103,25 @@ module Libis
|
|
156
103
|
|
157
104
|
FileUtils.mkpath(File.dirname(target))
|
158
105
|
|
159
|
-
|
160
|
-
|
161
|
-
assemble_and_convert(source, target, format)
|
162
|
-
|
163
|
-
elsif File.directory?(source)
|
164
|
-
source_list = Dir[File.join(source, '**', '*')].reject {|p| File.directory? p}
|
165
|
-
|
166
|
-
assemble_and_convert(source_list, target, format)
|
167
|
-
|
168
|
-
else
|
169
|
-
|
170
|
-
image = MiniMagick::Image.open(source) { |b| b.quiet }
|
171
|
-
|
172
|
-
if image.pages.size > 1
|
173
|
-
if @page
|
174
|
-
convert_image(image.pages[@page].path, target, format)
|
175
|
-
else
|
176
|
-
# noinspection RubyBlockToMethodReference
|
177
|
-
assemble_and_convert(image.pages.map {|page| page.path}, target, format)
|
178
|
-
end
|
179
|
-
else
|
180
|
-
convert_image(source, target, format)
|
181
|
-
end
|
182
|
-
end
|
106
|
+
convert_image(source, target, format)
|
183
107
|
|
184
108
|
target
|
185
109
|
|
186
110
|
end
|
187
111
|
|
188
|
-
def assemble_and_convert(sources, target, format)
|
189
|
-
|
190
|
-
warn 'Received multiple images as input and single page format as target.' unless self.class.multipage?(format)
|
191
|
-
converted_pages = sources.inject([]) do |list, path|
|
192
|
-
# noinspection RubyArgCount
|
193
|
-
converted = Tempfile.new(['page-', ".#{Libis::Format::TypeDatabase.type_extentions(format).first}"])
|
194
|
-
convert_image(path, converted.path, format)
|
195
|
-
list << converted
|
196
|
-
end
|
197
|
-
MiniMagick::Tool::Convert.new do |b|
|
198
|
-
b.append unless self.class.multipage?(format)
|
199
|
-
converted_pages.each {|page| b << page.path}
|
200
|
-
b << target
|
201
|
-
end
|
202
|
-
converted_pages.each do |temp_file|
|
203
|
-
temp_file.close
|
204
|
-
temp_file.unlink
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
112
|
protected
|
209
113
|
|
210
114
|
def convert_image(source, target, format)
|
211
115
|
|
212
|
-
|
213
|
-
|
116
|
+
if @page
|
117
|
+
image = MiniMagick::Image.open(source) { |b| b.quiet }
|
118
|
+
source = image.pages[@page].path if image.pages.size > 1
|
119
|
+
end
|
214
120
|
|
215
121
|
MiniMagick::Tool::Convert.new do |convert|
|
216
|
-
convert.quiet if @quiet
|
217
|
-
if @wm_image
|
218
|
-
convert << @wm_image.path
|
219
|
-
convert.bordercolor('transparent').border("#{@wm_gap}%") if @wm_gap > 0
|
220
|
-
convert.filter('Lagrange')
|
221
|
-
convert.resize("#{image_info['width'] / @wm_tiles}x#{image_info['height'] / @wm_tiles}") if @wm_tiles > 0
|
222
|
-
convert.resize("#{@wm_resize}%") if @wm_resize
|
223
|
-
convert.write('mpr:watermark').delete.+
|
224
|
-
end
|
225
|
-
|
226
122
|
convert.quiet if @quiet
|
227
123
|
convert << source
|
228
124
|
convert.flatten if @options[:flatten].nil? && format == :JPG
|
229
|
-
if @wm_image
|
230
|
-
if @wm_tiles >= 0 and @wm_tiles <= 1
|
231
|
-
convert << 'mpr:watermark'
|
232
|
-
else
|
233
|
-
convert.stack do |stack|
|
234
|
-
stack.size("#{image_info['width']}x#{image_info['height']}")
|
235
|
-
stack << 'xc:transparent'
|
236
|
-
stack.tile('mpr:watermark')
|
237
|
-
stack.draw "rectangle 0,0,#{image_info['width']},#{image_info['height']}"
|
238
|
-
end
|
239
|
-
end
|
240
|
-
convert.compose(@wm_composition).gravity(@wm_gravity).define("compose:args=#{@wm_opacity}%").composite
|
241
|
-
end
|
242
|
-
|
243
125
|
@flags.each {|f, v| v.is_a?(TrueClass) ? convert.send(f).+ : convert.send(f)}
|
244
126
|
if @delete_date
|
245
127
|
convert << '+set' << 'modify-date' << '+set' << 'create-date'
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
require 'libis/format/identifier'
|
5
|
+
|
6
|
+
require 'mini_magick'
|
7
|
+
|
8
|
+
MiniMagick.logger.level = ::Logger::UNKNOWN
|
9
|
+
|
10
|
+
MiniMagick.configure do |config|
|
11
|
+
# config.cli = :graphicsmagick
|
12
|
+
config.validate_on_create = false
|
13
|
+
config.validate_on_write = false
|
14
|
+
config.whiny = false
|
15
|
+
end
|
16
|
+
|
17
|
+
module Libis
|
18
|
+
module Format
|
19
|
+
module Converter
|
20
|
+
|
21
|
+
class ImageSplitter < Libis::Format::Converter::Base
|
22
|
+
|
23
|
+
def self.input_types
|
24
|
+
[:PDF, :TIFF, :GIF, :PBM, :PGM, :PPM]
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.output_types(format = nil)
|
28
|
+
return [] unless input_types.include?(format) if format
|
29
|
+
[:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2]
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.multipage?(format)
|
33
|
+
[:PDF, :TIFF, :GIF, :PBM, :PGM, :PPM].include?(format)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.category
|
37
|
+
:splitter
|
38
|
+
end
|
39
|
+
|
40
|
+
def image_split(_)
|
41
|
+
#force usage of this converter
|
42
|
+
end
|
43
|
+
|
44
|
+
def quiet(v)
|
45
|
+
@quiet = !!v
|
46
|
+
end
|
47
|
+
|
48
|
+
def convert(source, target, format, opts = {})
|
49
|
+
super
|
50
|
+
|
51
|
+
FileUtils.mkpath(File.dirname(target))
|
52
|
+
|
53
|
+
if self.class.multipage?(format)
|
54
|
+
target = File.join(File.dirname(target), "#{File.basename(target, '.*')}-%d#{File.extname(target)}")
|
55
|
+
end
|
56
|
+
|
57
|
+
result = split_image(source, target, format)
|
58
|
+
return nil unless result
|
59
|
+
target
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def split_image(source, target, format)
|
66
|
+
|
67
|
+
MiniMagick::Tool::Convert.new do |convert|
|
68
|
+
convert.quiet if @quiet
|
69
|
+
convert << source
|
70
|
+
convert.format(format)
|
71
|
+
convert << target
|
72
|
+
|
73
|
+
debug "ImageMagick command: '#{convert.command.join(' ')}'"
|
74
|
+
end
|
75
|
+
|
76
|
+
target
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
require 'libis/format/identifier'
|
5
|
+
|
6
|
+
require 'mini_magick'
|
7
|
+
# noinspection RubyResolve
|
8
|
+
require 'fileutils'
|
9
|
+
|
10
|
+
MiniMagick.logger.level = ::Logger::UNKNOWN
|
11
|
+
|
12
|
+
MiniMagick.configure do |config|
|
13
|
+
# config.cli = :graphicsmagick
|
14
|
+
config.validate_on_create = false
|
15
|
+
config.validate_on_write = false
|
16
|
+
config.whiny = false
|
17
|
+
end
|
18
|
+
|
19
|
+
module Libis
|
20
|
+
module Format
|
21
|
+
module Converter
|
22
|
+
|
23
|
+
# noinspection RubyTooManyInstanceVariablesInspection
|
24
|
+
class ImageWatermarker < Libis::Format::Converter::Base
|
25
|
+
|
26
|
+
def self.input_types
|
27
|
+
[:TIFF, :JPG, :PNG, :BMP, :GIF, :PDF, :JP2]
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.output_types(format = nil)
|
31
|
+
return [] unless input_types.include?(format) if format
|
32
|
+
[format]
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.multipage?(format)
|
36
|
+
[:PDF, :TIFF, :GIF, :PBM, :PGM, :PPM].include?(format)
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize
|
40
|
+
super
|
41
|
+
@quiet = true
|
42
|
+
@wm_image = nil
|
43
|
+
@wm_file = nil
|
44
|
+
@wm_text = '© LIBIS'
|
45
|
+
@wm_tiles = 4
|
46
|
+
@wm_resize = nil
|
47
|
+
@wm_gap = 20
|
48
|
+
@wm_gravity = 'Center'
|
49
|
+
@wm_rotation = 30
|
50
|
+
@wm_composition ='modulate'
|
51
|
+
@wm_composition_args = '10'
|
52
|
+
end
|
53
|
+
|
54
|
+
def image_watermark(_)
|
55
|
+
#force usage of this converter
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def quiet(v)
|
60
|
+
@quiet = !!v
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
def page(v)
|
65
|
+
@page = v.to_i
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
# watermark image to use
|
70
|
+
def file(v)
|
71
|
+
@wm_file = v.blank? ? nil : v
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
# text to create a watermark from
|
76
|
+
def text(v)
|
77
|
+
@wm_text = v.blank? ? nil : v
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
# rotation of the watermark text (counter clockwise in degrees; integer number)
|
82
|
+
# default 30
|
83
|
+
def rotation(v)
|
84
|
+
@wm_rotation = v.to_i
|
85
|
+
self
|
86
|
+
end
|
87
|
+
|
88
|
+
# number of tiles of the watermark
|
89
|
+
# default 4
|
90
|
+
# 0: no tiling, so only 1 watermark will be placed with the original size
|
91
|
+
# 1: 1 tile, so the watermark will be scaled up to fill the image
|
92
|
+
# n > 1: minimum n tiles in both directions
|
93
|
+
# n < 0: tile without scaling the watermark
|
94
|
+
def tiles(v)
|
95
|
+
@wm_tiles = v.to_i
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
# fraction 0.0 - 1.0
|
100
|
+
def resize(v)
|
101
|
+
@wm_resize = (v.to_f * 100).to_i
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
105
|
+
# size of the gap between watermark instances. Fractions as percentage of widht/height
|
106
|
+
# default 0.2
|
107
|
+
def gap(v)
|
108
|
+
@wm_gap = (v.to_f * 100).to_i
|
109
|
+
self
|
110
|
+
end
|
111
|
+
|
112
|
+
# center point for the watermark overlay
|
113
|
+
# default 'center'
|
114
|
+
def gravity(v)
|
115
|
+
@wm_gravity = v.blank? ? nil : v
|
116
|
+
self
|
117
|
+
end
|
118
|
+
|
119
|
+
# the image composition method for merging the watermark image
|
120
|
+
# default 'modulate'
|
121
|
+
# See https://imagemagick.org/script/compose.php for more information
|
122
|
+
def composition(v)
|
123
|
+
@wm_composition = v.blank? ? nil : v
|
124
|
+
self
|
125
|
+
end
|
126
|
+
|
127
|
+
# arguments for the composition method
|
128
|
+
# default '10'
|
129
|
+
# See https://imagemagick.org/script/compose.php for more information
|
130
|
+
def composition_args(v)
|
131
|
+
@wm_composition_args = v.blank? ? nil : v
|
132
|
+
self
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
def convert(source, target, format, opts = {})
|
137
|
+
super
|
138
|
+
|
139
|
+
FileUtils.mkpath(File.dirname(target))
|
140
|
+
|
141
|
+
if source.is_a?(Array) || File.directory?(source)
|
142
|
+
error 'Only a single image file is allowed for input'
|
143
|
+
else
|
144
|
+
image = MiniMagick::Image.open(source) { |b| b.quiet }
|
145
|
+
|
146
|
+
if image.pages.size > 1
|
147
|
+
if @page
|
148
|
+
convert_image(image.pages[@page].path, target, format)
|
149
|
+
else
|
150
|
+
error 'multipage input file detecte; you need to supply a page number'
|
151
|
+
end
|
152
|
+
else
|
153
|
+
convert_image(source, target, format)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
target
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
protected
|
162
|
+
|
163
|
+
# noinspection DuplicatedCode
|
164
|
+
def convert_image(source, target, format)
|
165
|
+
|
166
|
+
wm_image = watermark_image
|
167
|
+
return nil unless wm_image
|
168
|
+
image_info = MiniMagick::Image::Info.new(source) {|b| b.quiet}
|
169
|
+
|
170
|
+
MiniMagick::Tool::Convert.new do |convert|
|
171
|
+
convert.quiet if @quiet
|
172
|
+
|
173
|
+
# adapt watermark image to tile size and apply gap and resize if necessary
|
174
|
+
convert << @wm_image.path
|
175
|
+
# noinspection RubyResolve
|
176
|
+
convert.bordercolor('transparent').border("#{@wm_gap}%") if @wm_gap > 0
|
177
|
+
convert.filter('Lagrange')
|
178
|
+
convert.resize("#{image_info['width'] / @wm_tiles}x#{image_info['height'] / @wm_tiles}") if @wm_tiles > 0
|
179
|
+
convert.resize("#{@wm_resize}%") if @wm_resize
|
180
|
+
convert.write('mpr:watermark').delete.+
|
181
|
+
|
182
|
+
# convert the source image
|
183
|
+
convert << source
|
184
|
+
if @wm_tiles >= 0 and @wm_tiles <= 1
|
185
|
+
# only 1 watermark required (tiles = 0/1 => scaled no/yes)
|
186
|
+
convert << 'mpr:watermark'
|
187
|
+
else
|
188
|
+
# fill the image size with a pattern of the watermark image
|
189
|
+
convert.stack do |stack|
|
190
|
+
stack.size("#{image_info['width']}x#{image_info['height']}")
|
191
|
+
stack << 'xc:transparent'
|
192
|
+
# noinspection RubyResolve
|
193
|
+
stack.tile('mpr:watermark')
|
194
|
+
# noinspection RubyResolve
|
195
|
+
stack.draw "rectangle 0,0,#{image_info['width']},#{image_info['height']}"
|
196
|
+
end
|
197
|
+
end
|
198
|
+
# perform the blending operation
|
199
|
+
convert.compose(@wm_composition).gravity(@wm_gravity).define("compose:args=#{@wm_composition_args}").composite
|
200
|
+
|
201
|
+
convert.format(format)
|
202
|
+
convert << target
|
203
|
+
|
204
|
+
debug "ImageMagick command: '#{convert.command.join(' ')}'"
|
205
|
+
end
|
206
|
+
|
207
|
+
target
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
private
|
212
|
+
|
213
|
+
# Create or use a watermark image.
|
214
|
+
#
|
215
|
+
# If both text and image are set, the file will be used as-is if it exists and is a valid image file. Otherwise the
|
216
|
+
# file will be created or overwritten with a newly created watermark image.
|
217
|
+
#
|
218
|
+
# The created watermark file will be a 2000x2000 pixels PNG image with transparent background
|
219
|
+
#
|
220
|
+
# The text will be slanted by given rotation degrees counter-clockwise
|
221
|
+
def watermark_image
|
222
|
+
rotation = 360 - @wm_rotation
|
223
|
+
@wm_image = MiniMagick::Image.new(@wm_file) if @wm_file
|
224
|
+
# only create image if file is not an image
|
225
|
+
unless @wm_image&.valid?
|
226
|
+
# noinspection RubyResolve
|
227
|
+
# Create image file (as given or temp file)
|
228
|
+
image = @wm_file || (Dir::Tmpname.create(%w(wm_image .png)) {|_|})
|
229
|
+
# noinspection RubyResolve
|
230
|
+
MiniMagick::Tool::Convert.new do |convert|
|
231
|
+
convert.quiet # allways quiet
|
232
|
+
convert.background 'transparent'
|
233
|
+
convert.size('2000x2000')
|
234
|
+
convert.gravity 'Center'
|
235
|
+
convert.font('Helvetica').fill('black').pointsize(72) #.stroke('black').strokewidth(1)
|
236
|
+
convert << "label:#{@wm_text}"
|
237
|
+
convert.rotate rotation
|
238
|
+
convert.trim.repage.+
|
239
|
+
convert << image
|
240
|
+
end
|
241
|
+
if @wm_file
|
242
|
+
@wm_image = MiniMagick::Image.new(image)
|
243
|
+
else # delete temp file
|
244
|
+
@wm_image = MiniMagick::Image.open(image)
|
245
|
+
File.delete(image)
|
246
|
+
end
|
247
|
+
# noinspection RubyResolve
|
248
|
+
unless @wm_image.valid?
|
249
|
+
error "Problem creating watermark image '#{image}'."
|
250
|
+
@wm_image = nil
|
251
|
+
end
|
252
|
+
@wm_image
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|