Module: JekyllAutoThumbnails::Scanner

Defined in:
lib/jekyll-auto-thumbnails/scanner.rb

Overview

HTML scanning for images

Class Method Summary collapse

Class Method Details

.image_dimensions(file_path) ⇒ Array<Integer, Integer>?

Get image dimensions from file

Parameters:

  • file_path (String)

    path to image file

Returns:

  • (Array<Integer, Integer>, nil)
    width, height

    or nil



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jekyll-auto-thumbnails/scanner.rb', line 80

def self.image_dimensions(file_path)
  # Use ImageMagick identify command (shell-free, cross-platform)
  # Use [0] to get only first frame (important for animated GIFs)
  # Wrapper handles both ImageMagick v6 and v7
  output, status = ImageMagickWrapper.execute_identify("-format", "%wx%h", "#{file_path}[0]")
  return nil unless status.success? && !output.strip.empty?

  width, height = output.strip.split("x").map(&:to_i)
  [width, height]
rescue StandardError
  nil
end

.scan_html(html, registry, config, site_source = nil) ⇒ Object

Scan HTML for images needing optimization

Parameters:

  • html (String)

    HTML content

  • registry (Registry)

    image registry

  • config (Configuration)

    configuration

  • site_source (String) (defaults to: nil)

    Jekyll site source (optional, for unsized image checking)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jekyll-auto-thumbnails/scanner.rb', line 17

def self.scan_html(html, registry, config, site_source = nil)
  doc = HtmlParser.parse(html, config.parser)

  # Only process images in <article> tags
  doc.css("article img").each do |img|
    src = img["src"]
    next unless src
    next if UrlResolver.external?(src)

    # Check for explicit dimensions
    width = parse_dimension(img["width"])
    height = parse_dimension(img["height"])

    if width || height
      # Explicitly sized - calculate missing dimension if needed
      if site_source && (width.nil? || height.nil?)
        Jekyll.logger.debug "AutoThumbnails:", "Calculating dimensions for #{src} (#{width}x#{height})"
        width, height = calculate_dimensions(src, width, height, site_source)
        Jekyll.logger.debug "AutoThumbnails:", "Calculated: #{width}x#{height}"
      end

      # Skip if dimensions match original (no thumbnail needed)
      if site_source && dimensions_match_original?(src, width, height, site_source)
        Jekyll.logger.debug "AutoThumbnails:", "Skipping #{src} - dimensions match original"
        next
      end

      Jekyll.logger.debug "AutoThumbnails:", "Registering #{src} at #{width}x#{height}"
      registry.register(src, width, height)
    elsif site_source && (config.max_width || config.max_height)
      # Unsized but max config exists - check actual dimensions
      check_and_register_oversized(src, registry, config, site_source)
    end
  end
end