1 # Nick's web site: imginfo filter. Generate XML representation of image
2 # metadata which can be further processed into a web page.
4 # Copyright © 2020 Nick Bowler
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 class ImgInfoFilter < Nanoc::Filter
26 def do_variant(xml, name, item = @item, rep: :default)
27 file = item.reps[rep].raw_path
29 w, h = FastImage.size(file)
34 xml.uri(item_uri(item, rep: rep))
37 xml.filesize(human_filesize(sz))
42 def run(filename, params = {})
43 exif = EXIFR::JPEG.new(filename).to_hash
45 b = Nokogiri::XML::Builder.new do |xml|
47 do_variant(xml, "Large", rep: :large)
48 do_variant(xml, "Medium", rep: :medium)
49 do_variant(xml, "Original")
52 if @item[:description]
53 xml.description(:xmlns => 'http://www.w3.org/1999/xhtml') {
54 xml << @item[:description]
59 exif.each do |key, value|
60 # Convert some fields to more useful forms...
62 when :f_number, :exposure_bias_value
63 value = sprintf("%.1f", value)
65 value = sprintf("%d\n", value)
67 value = value.bytes.join(".")
68 when :exposure_program
70 when 1 then value = "Manual"
71 when 2 then value = "Normal program"
72 when 3 then value = "Aperture priority"
73 when 4 then value = "Shutter priority"
77 when 1 then value = "Average"
78 when 2 then value = "Center weighted average"
79 when 3 then value = "Spot"
80 when 4 then value = "Multi-spot"
81 when 5 then value = "Pattern"
82 when 6 then vlaue = "Partial"
85 tmp = if value & 1 == 1 then "Yes" else "No" end
87 when 1, 2 then tmp += ", compulsory"
88 when 3 then tmp += ", auto"
91 when 2 then tmp += ", return light not detected"
92 when 3 then tmp += ", return light detected"
102 # EXIF does not do timezones so don't display one
103 value = value.strftime("%Y-%m-%d %H:%M:%S")
106 xml.send((key.to_s << "_").to_sym, value.to_s)