11
12
13
14
15
16
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
52
53
54
|
# File 'app/models/sprite_map/image_map.rb', line 11
def self.find_or_create_by_image_map(image_map)
fingerprint = Digest::MD5.hexdigest(image_map.keys.sort.join('-'))
if (sprite_map = ImageMap.find_by(fingerprint: fingerprint))
sprite_map.touch
sprite_map
else
total_width = 0
total_height = 0
images = {}
positions = image_map.inject({}) do |memo, (identifier, path)|
image_file = Magick::Image.read(path)[0]
image_width = image_file.columns
image_height = image_file.rows
memo[identifier] = {
x: total_width,
y: 0,
width: image_width,
height: image_height
}
images[identifier] = {
path: path,
file: image_file
}
total_width += image_width
total_height = [total_height, image_height].max
memo
end
create_sprite_image(
fingerprint: fingerprint,
positions: positions,
images: images,
width: total_width,
height: total_height
)
end
end
|