Class: CheesyGallery::ImageFile

Inherits:
BaseImageFile
  • Object
show all
Defined in:
lib/cheesy-gallery/image_file.rb

Overview

This StaticFile subclass adds additional functionality for images in the gallery

Constant Summary collapse

@@geometry_cache =

don’t need to worry about inheritance here # rubocop:disable Style/ClassVars

Jekyll::Cache.new('CheesyGallery::Geometry')

Instance Method Summary collapse

Methods inherited from BaseImageFile

#path, #write

Constructor Details

#initialize(site, collection, file, max_size:, quality:) ⇒ ImageFile

Returns a new instance of ImageFile.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cheesy-gallery/image_file.rb', line 11

def initialize(site, collection, file, max_size:, quality:)
  super(site, collection, file)

  @max_size = max_size
  @quality = quality

  realpath = File.realdirpath(path)
  mtime = File.mtime(realpath)
  geom = @@geometry_cache.getset("#{realpath}##{mtime}") do
    result = [100, 100]
    # read file metadata in the same way it will be processed
    Jekyll.logger.debug 'Identifying:', path
    source = Magick::Image.ping(path).first
    source.change_geometry!(@max_size) do |cols, rows, _img|
      result = [rows, cols]
    end
    source.destroy!
    result
  end

  data['height'] = geom[0]
  data['width'] = geom[1]
end

Instance Method Details

#process_and_write(img, path) ⇒ Object

instead of copying, renders an optimised version



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cheesy-gallery/image_file.rb', line 36

def process_and_write(img, path)
  img.change_geometry!(@max_size) do |cols, rows, i|
    i.resize!(cols, rows)
  end
  # follow recommendations from https://stackoverflow.com/a/7262050/4918 to get better compression
  img.interlace = Magick::PlaneInterlace
  # but skip the blur to avoid too many changes to the data
  # img.gaussian_blur(0.05)
  img.strip!
  # workaround weird {self} initialisation pattern
  quality = @quality
  img.write(path) { |image| image.quality = quality }
end