Module: ImageQualityCheck

Extended by:
DSL
Defined in:
lib/image_quality_check.rb,
lib/image_quality_check/version.rb

Defined Under Namespace

Modules: DSL Classes: DetermineQuality, Engine, Result

Constant Summary collapse

VERSION =
"0.2.4"

Class Method Summary collapse

Methods included from DSL

define_rules_for, preferred_formats_rule, preferred_size_rule, rule, rules_for

Class Method Details

.analyze(path_to_image) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/image_quality_check.rb', line 15

def self.analyze(path_to_image)
  out = `convert #{Shellwords.escape path_to_image} json:`
  # image magick gif delay bug invalid json
  # https://github.com/ImageMagick/ImageMagick/issues/1624
  out.gsub!(/("delay": "[^"]+")\n/m, "\\1,\n")
  return {} if out.empty?

  raw_json = JSON.parse(out)
  json = raw_json.is_a?(Array) ? raw_json.first['image'] : raw_json['image']
  background_is_transparent =
    json.dig('channelDepth', 'alpha') &&
    json['channelStatistics']['alpha']['min'] != json['channelStatistics']['alpha']['max']
  {
    format: json['format'].downcase,
    mime_type: json['mimeType'],
    background_is_transparent: background_is_transparent,
    width: json.dig('geometry', 'width').to_i,
    height: json.dig('geometry', 'height').to_i,
    quality: json['quality'],
    blur: blur_detect(path_to_image).map { |k, v| [ k.to_sym, v ] }.to_h
  }
end

.blur_detect(path_to_image) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/image_quality_check.rb', line 38

def self.blur_detect(path_to_image)
  script = File.join(File.dirname(__FILE__), '..', 'exe', 'image_quality_blur')
  out, err, value = Open3.capture3("#{script} #{Shellwords.escape(path_to_image)}")
  if value.success?
    JSON.parse(out.gsub('NaN', '0'))
  else
    if out[/^\{/]
      JSON.parse(out)
    else
      {
        error: err.to_s,
        out: out.to_s
      }
    end
  end
end

.determine_quality(model, attachment, &block) ⇒ Object



11
12
13
# File 'lib/image_quality_check.rb', line 11

def self.determine_quality(model, attachment, &block)
  ImageQualityCheck::DetermineQuality.run(model, attachment, &block)
end