Class: Jekyll::GalleryPhoto

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-photo-gallery.rb

Instance Method Summary collapse

Constructor Details

#initialize(site, base, basepath, name) ⇒ GalleryPhoto

Returns a new instance of GalleryPhoto.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Method Details



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jekyll-photo-gallery.rb', line 32

def create_symlink
  link_src = @site.in_source_dir(@path)
  link_dest = @site.in_dest_dir(@path)
  link_dest_basepath = @site.in_dest_dir(@basepath)
  # Delete existing static asset.
  @site.static_files.delete_if { |sf|
    sf.relative_path == "/#{@path}"
  }
  # Add new entry which prevents writing.
  @site.static_files << GalleryFile.new(@site, @base, @basepath, @name)
  # Create symlink in file system.
  if File.exists?(link_dest)
    # Correct link already exists.
    if File.readlink(link_dest) == link_src
      return
    end
    File.delete(link_dest)
  end
  puts "Creating symlink for #{@path}"
  # Create symlink
  FileUtils.mkdir_p(link_dest_basepath, :mode => 0755)
  File.symlink(link_src, link_dest)
end

#create_thumbnailObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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_exifObject



86
87
88
89
90
91
92
93
94
95
96
# 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_liquidObject



98
99
100
101
102
103
104
105
# 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_sObject



107
108
109
# File 'lib/jekyll-photo-gallery.rb', line 107

def to_s
  @path
end