Class: TimestampMaker::Handlers::ImageMagick
- Inherits:
-
Object
- Object
- TimestampMaker::Handlers::ImageMagick
- Defined in:
- lib/timestamp_maker/handlers/image_magick.rb
Constant Summary collapse
- GRAVITY_MAP =
{ 'top-left' => 'NorthWest', 'top-right' => 'NorthEast', 'bottom-left' => 'SouthWest', 'bottom-right' => 'SouthEast' }.freeze
Instance Attribute Summary collapse
-
#time_zone_lookuper ⇒ Object
Returns the value of attribute time_zone_lookuper.
Instance Method Summary collapse
- #accept?(mime_type) ⇒ Boolean
- #add_timestamp(input_path, output_path, time, format:, font_size:, font_family:, font_color:, background_color:, coordinate_origin:, x:, y:, font_padding:) ⇒ Object
- #creation_time(input_path) ⇒ Object
-
#initialize(time_zone_lookuper:) ⇒ ImageMagick
constructor
A new instance of ImageMagick.
Constructor Details
#initialize(time_zone_lookuper:) ⇒ ImageMagick
Returns a new instance of ImageMagick.
20 21 22 |
# File 'lib/timestamp_maker/handlers/image_magick.rb', line 20 def initialize(time_zone_lookuper:) @time_zone_lookuper = time_zone_lookuper end |
Instance Attribute Details
#time_zone_lookuper ⇒ Object
Returns the value of attribute time_zone_lookuper.
18 19 20 |
# File 'lib/timestamp_maker/handlers/image_magick.rb', line 18 def time_zone_lookuper @time_zone_lookuper end |
Instance Method Details
#accept?(mime_type) ⇒ Boolean
24 25 26 |
# File 'lib/timestamp_maker/handlers/image_magick.rb', line 24 def accept?(mime_type) mime_type.start_with?('image/') end |
#add_timestamp(input_path, output_path, time, format:, font_size:, font_family:, font_color:, background_color:, coordinate_origin:, x:, y:, font_padding:) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/timestamp_maker/handlers/image_magick.rb', line 28 def ( input_path, output_path, time, format:, font_size:, font_family:, font_color:, background_color:, coordinate_origin:, x:, y:, font_padding: ) time_string = time.strftime(format) command = %W[ convert #{input_path} ( -background #{background_color} -fill #{font_color} -family #{font_family} -pointsize #{font_size} -gravity NorthWest -splice #{font_padding}x#{font_padding} -gravity SouthEast -splice #{font_padding}x#{font_padding} label:#{time_string} ) -gravity #{GRAVITY_MAP[coordinate_origin]} -geometry +#{x}+#{y} -composite #{output_path} ] raise "Command failed with exit #{$CHILD_STATUS.exitstatus}: #{command.first}" unless system(*command) end |
#creation_time(input_path) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/timestamp_maker/handlers/image_magick.rb', line 63 def creation_time(input_path) command = %W[ identify -format %[exif:DateTime*]%[exif:OffsetTime*]%[exif:GPSLatitude*]%[exif:GPSLongitude*] #{input_path} ] stdout_string, status = Open3.capture2(*command) raise unless status.success? parsed = Hash[stdout_string.split("\n").map! { |i| i[5..-1].split('=') }] time_string = parsed['DateTimeOriginal'] || parsed['DateTimeDigitized'] || parsed['DateTime'] raise 'Cannot find creation time' if time_string.nil? time_arguments = time_string.split(/[: ]/).map(&:to_i) if (time_zone = retrieve_time_zone_by_coordinate(parsed)) begin return TZInfo::Timezone.get(time_zone).local_time(*time_arguments) rescue TZInfo::InvalidTimezoneIdentifier warn "Can not find time zone: #{time_zone}" end end time_offset_string = parsed['OffsetTimeOriginal'] || parsed['OffsetTimeDigitized'] || parsed['OffsetTime'] raise 'Can not find time offset' if time_offset_string.nil? Time.new(*time_arguments, time_offset_string) end |