class Jekyll::GalleryPhoto

Public Class Methods

new(site, base, basepath, name) click to toggle source
# File lib/jekyll-photo-gallery.rb, line 13
def initialize(site, base, basepath, name)
  @site = site
  @base = base
  @name = name
  @basepath = basepath
  @path = File.join(basepath, name)
  # Create symlink in site destination.
  create_symlink
  # Read exif data.
  @exif = read_exif
  # Fetch date time field from exif data.
  @date_time = nil
  if !@exif.nil?
    @date_time = @exif[:date_time]
  end
  # Create thumbnail.
  @thumbnail = create_thumbnail
end

Public Instance Methods

create_thumbnail() click to toggle source
# File lib/jekyll-photo-gallery.rb, line 56
def create_thumbnail
  config = @site.config["photo_gallery"] || {}
  thumbnail_size = config["thumbnail_size"] || 256
  # Compile thumbnail path
  thumb_dir = File.join(@site.dest, @basepath, "thumbs")
  thumb = File.join(thumb_dir, @name)
  thumb_name = File.join("thumbs", @name)
  # Add thumbnail to static assets
  @site.static_files << GalleryFile.new(@site, @base, thumb_dir, @name)
  # Create if it does not exist.
  FileUtils.mkdir_p(thumb_dir, :mode => 0755)
  if File.exists?(thumb)
    return thumb_name
  end
  begin
    puts "Creating thumbnail for #{@path}"
    # Read image.
    img = Magick::Image.read(@path).first
    # Resize Flickr style
    img = img.resize_to_fill(thumbnail_size, thumbnail_size)
    # Write thumbnail
    img.write(thumb)
  rescue Exception => e
    puts "Error generating thumbnail for #{@path}: #{e}"
    puts e.backtrace
    thumb_name = nil
  end
  thumb_name
end
read_exif() click to toggle source
# File lib/jekyll-photo-gallery.rb, line 86
def read_exif
  exif = nil
  begin
    exif = EXIFR::JPEG.new(@path).to_hash
  rescue EXIFR::MalformedJPEG
    puts "No EXIF data in #{@path}"
  rescue Exception => e
    puts "Error reading EXIF data for #{@path}: #{e}"
  end
  exif
end
to_liquid() click to toggle source
# File lib/jekyll-photo-gallery.rb, line 98
def to_liquid
  {
    'name' => @name,
    'date_time' => @date_time,
    'thumbnail' => @thumbnail,
    'exif' => @exif.nil? ? nil : @exif.collect{|k,v| [k.to_s, v]}.to_h
  }
end
to_s() click to toggle source
# File lib/jekyll-photo-gallery.rb, line 107
def to_s
  @path
end