/
product_image_handler.rb
59 lines (51 loc) · 1.36 KB
/
product_image_handler.rb
1
2
3
4
5
6
7
8
9
10
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
55
56
57
58
59
gem(defined?(JRUBY_VERSION) ? 'rmagick4j' : 'rmagick')
require 'RMagick'
module ProductImageHandler
require 'fileutils'
def thumbnail fd
rv = nil
i = Magick::Image.from_blob(fd).first
ow, oh = i.columns, i.rows
cw = CartConfig.get(:thumbnail_width, :image)
ch = CartConfig.get(:thumbnail_height, :image)
wp = ow.to_f / cw.to_f
hp = oh.to_f / ch.to_f
if CartConfig.get(:thumbnail_exact_dimensions, :image)
rv = i.resize(cw, ch)
elsif wp > 1.0 or hp > 1.0
mx = [wp, hp].max
rv = i.resize((ow/mx).round, (oh/mx).round)
else
rv = i
end
rv
end
def fullsize fd
rv = nil
i = Magick::Image.from_blob(fd).first
ow, oh = i.columns, i.rows
cw = CartConfig.get(:width, :image)
ch = CartConfig.get(:height, :image)
wp = ow.to_f / cw.to_f
hp = oh.to_f / ch.to_f
if CartConfig.get(:exact_dimensions, :image)
rv = i.resize(cw, ch)
elsif wp > 1.0 or hp > 1.0
mx = [wp, hp].max
rv = i.resize((ow/mx).round, (oh/mx).round)
else
rv = i
end
rv
end
def thumbnail_path original_name
File.join(image_base_path, product.slug, "t-#{original_name}")
end
def fullsize_path original_name
File.join(image_base_path, product.slug, original_name)
end
def write_file img, path
FileUtils.mkdir_p File.dirname(path)
img.write path
end
end