Sha256: db0a94f5c90044bc87b199b1c704ab75a128bf52b956b25075f5ba779e0cc8e3

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

# typed: strict
# frozen_string_literal: true

require 'rmagick'
require 'cheesy-gallery/base_image_file'

# This StaticFile subclass adds additional functionality for images in the
# gallery
class CheesyGallery::ImageFile < CheesyGallery::BaseImageFile
  @@geometry_cache = T.let(Jekyll::Cache.new('CheesyGallery::Geometry'), Jekyll::Cache) # don't need to worry about inheritance here # rubocop:disable Style/ClassVars

  sig { params(site: Jekyll::Site, collection: Jekyll::Collection, file: Jekyll::StaticFile, max_size: String, quality: Integer).void }
  def initialize(site, collection, file, max_size:, quality:)
    super(site, collection, file)

    @max_size = T.let(max_size, String)
    @quality = T.let(quality, Integer)

    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

  # instead of copying, renders an optimised version
  sig { params(img: Magick::ImageList, path: String).void }
  def process_and_write(img, path)
    img.change_geometry!(@max_size) do |cols, rows, i|
      i.resize!(cols, rows)
    end
    # workaround weird {self} initialisation pattern
    quality = @quality
    img.write(path) { self.quality = quality }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cheesy-gallery-1.0.0 lib/cheesy-gallery/image_file.rb
cheesy-gallery-0.8.0 lib/cheesy-gallery/image_file.rb
cheesy-gallery-0.7.0 lib/cheesy-gallery/image_file.rb
cheesy-gallery-0.6.0 lib/cheesy-gallery/image_file.rb