photos.amdavidson.com/_bin/gallery.rb
2023-01-02 18:43:03 -08:00

56 lines
1.8 KiB
Ruby
Executable file

#!/usr/bin/env ruby
require 'exifr'
puts "Gallery title?"
gallery_title = gets.chomp
gallery_slug = gallery_title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
year = Time.now.year
month = Time.now.strftime("%m")
puts "Getting jpg images for gallery."
@images = Dir.glob("#{Dir.pwd}/*.jpg")
Dir.mkdir gallery_slug
Dir.mkdir File.join(gallery_slug, "assets")
out = "---\ngallery-title: #{gallery_title}\n"
out += "gallery-slug: #{gallery_slug}\n"
out += "gallery-date: #{Time.now.to_s}\n"
@images.each_with_index do |image, i|
basename = File.basename(image, '.jpg')
filename = year.to_s+"-"+month.to_s+"-"+basename
exif = EXIFR::JPEG.new(image)
t = " - basename: #{basename}\n"
t += " date: #{exif.date_time}\n"
t += " thumb-src: /assets/#{filename}_t.jpg\n"
t += " mid-src: /assets/#{filename}_m.jpg\n"
t += " image-src: /assets/#{filename}.jpg\n"
t += " mid-page: /#{gallery_slug}/#{basename}/\n"
t += " aspect: #{(exif.pixel_x_dimension.to_f / exif.pixel_y_dimension).round(2)}\n"
if i == 0 then
out += "gallery-key:\n"
out += t
out += "images:\n"
out += t
else
out += t
end
puts "Resizing #{basename}."
thumb = "convert '#{image}' -quality 80 -resize 300x300 './#{gallery_slug}/assets/#{filename}_t.jpg'"
`#{thumb}`
mid = "convert '#{image}' -filter Lanczos -sampling-factor 1x1 -unsharp 1.5x1+0.7+0.02 -quality 90 -resize 1300x1300 './#{gallery_slug}/assets/#{filename}_m.jpg'"
`#{mid}`
full = "convert '#{image}' -filter Lanczos -sampling-factor 1x1 -unsharp 1.5x1+0.7+0.02 -quality 90 -resize 2500x2500 './#{gallery_slug}/assets/#{filename}.jpg'"
`#{full}`
`cp '#{image}' #{gallery_slug}/`
end
out += "---\n"
File.open("./#{gallery_slug}/#{gallery_slug}.yaml", "w") { |file| file.write(out) }
puts "Gallery complete."