Class: MiniMagick::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/images_convert/mini_magick_stub.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Image

Returns a new instance of Image.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/images_convert/mini_magick_stub.rb', line 54

def initialize(path)
  @source_path = path
  @resize_value = nil
  @format = File.extname(path).delete('.').to_s.downcase
  @quality_value = nil
  dims = self.class.stub_dimension_store[path]
  unless dims
    dim_sidecar = self.class.dimension_sidecar_path(path)
    if File.exist?(dim_sidecar)
      begin
        loaded = YAML.safe_load(File.read(dim_sidecar)) || {}
        width = loaded['width'] || loaded[:width]
        height = loaded['height'] || loaded[:height]
        dims = { width: width.to_i, height: height.to_i }
        self.class.stub_dimension_store[path] = dims
      rescue StandardError
        dims = nil
      end
    end
  end
  @width = dims ? dims[:width].to_i : 0
  @height = dims ? dims[:height].to_i : 0
end

Instance Attribute Details

#height ⇒ Object (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/images_convert/mini_magick_stub.rb', line 8

def height
  @height
end

#quality_value ⇒ Object (readonly)

Returns the value of attribute quality_value.



8
9
10
# File 'lib/images_convert/mini_magick_stub.rb', line 8

def quality_value
  @quality_value
end

#source_path ⇒ Object (readonly)

Returns the value of attribute source_path.



8
9
10
# File 'lib/images_convert/mini_magick_stub.rb', line 8

def source_path
  @source_path
end

#width ⇒ Object (readonly)

Returns the value of attribute width.



8
9
10
# File 'lib/images_convert/mini_magick_stub.rb', line 8

def width
  @width
end

Class Method Details

.dimension_sidecar_path(path) ⇒ Object



38
39
40
# File 'lib/images_convert/mini_magick_stub.rb', line 38

def dimension_sidecar_path(path)
  "#{path}.dims"
end

.open(path) ⇒ Object



47
48
49
50
51
52
# File 'lib/images_convert/mini_magick_stub.rb', line 47

def self.open(path)
  unless File.exist?(path)
    raise Errno::ENOENT, "No such file or directory - #{path}"
  end
  new(path)
end

.ping(path) ⇒ Object



42
43
44
# File 'lib/images_convert/mini_magick_stub.rb', line 42

def ping(path)
  new(path)
end

.reset_stub! ⇒ Object



29
30
31
32
# File 'lib/images_convert/mini_magick_stub.rb', line 29

def reset_stub!
  @stub_exif_store = {}
  @stub_dimension_store = {}
end

.set_stub_dimensions(path, width:, height:) ⇒ Object



25
26
27
# File 'lib/images_convert/mini_magick_stub.rb', line 25

def set_stub_dimensions(path, width:, height:)
  stub_dimension_store[path] = { width: width.to_i, height: height.to_i }
end

.set_stub_exif(path, data) ⇒ Object



19
20
21
22
23
# File 'lib/images_convert/mini_magick_stub.rb', line 19

def set_stub_exif(path, data)
  normalized = data.transform_keys(&:to_s)
  stub_exif_store[path] = normalized
  File.write(sidecar_path(path), normalized.to_yaml)
end

.sidecar_path(path) ⇒ Object



34
35
36
# File 'lib/images_convert/mini_magick_stub.rb', line 34

def sidecar_path(path)
  "#{path}.exif"
end

.stub_dimension_store ⇒ Object



15
16
17
# File 'lib/images_convert/mini_magick_stub.rb', line 15

def stub_dimension_store
  @stub_dimension_store ||= {}
end

.stub_exif_store ⇒ Object



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

def stub_exif_store
  @stub_exif_store ||= {}
end

Instance Method Details

#[](key) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/images_convert/mini_magick_stub.rb', line 101

def [](key)
  data = self.class.stub_exif_store[@source_path]
  unless data
    sidecar = self.class.sidecar_path(@source_path)
    if File.exist?(sidecar)
      begin
        loaded = YAML.safe_load(File.read(sidecar)) || {}
        data = loaded.transform_keys(&:to_s)
        self.class.stub_exif_store[@source_path] = data
      rescue StandardError
        data = {}
      end
    else
      data = {}
    end
  end

  data[key.to_s]
end

#format(value) ⇒ Object



82
83
84
# File 'lib/images_convert/mini_magick_stub.rb', line 82

def format(value)
  @format = value.to_s.downcase
end

#quality(value) ⇒ Object



86
87
88
# File 'lib/images_convert/mini_magick_stub.rb', line 86

def quality(value)
  @quality_value = value.to_i
end

#resize(value) ⇒ Object



78
79
80
# File 'lib/images_convert/mini_magick_stub.rb', line 78

def resize(value)
  @resize_value = value
end

#write(destination) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/images_convert/mini_magick_stub.rb', line 90

def write(destination)
  FileUtils.mkdir_p(File.dirname(destination))
  File.open(destination, 'wb') do |f|
    f.write("mini_magick_stub\n")
    f.write("source: #{@source_path}\n")
    f.write("resize: #{@resize_value}\n")
    f.write("format: #{@format}\n")
    f.write("quality: #{@quality_value}\n") if @quality_value
  end
end