Module: Balloon::Processing

Extended by:
ActiveSupport::Concern
Defined in:
lib/balloon/processing.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#auto_orient!(img, covert) ⇒ Object



124
125
126
127
128
# File 'lib/balloon/processing.rb', line 124

def auto_orient!(img, covert)
  if img.exif["Orientation"].to_i > 1
    convert.auto_orient
  end
end

#get_image_data(img) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/balloon/processing.rb', line 130

def get_image_data(img)
  return {
    width: img.width,
    height: img.height,
    size: img.size
  }
end

#handle_original(file, ext) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/balloon/processing.rb', line 37

def handle_original(file, ext)
  original_image =  MiniMagick::Image.open(file.path)
  convert = MiniMagick::Tool::Convert.new
  convert << file.path

  auto_orient!(original_image, convert)
  convert.format ext

  cache_file = File.join(cache_path, "#{file.basename}.#{ext}")
  convert << cache_file
  convert.call

  return MiniMagick::Image.open(cache_file)
end

#handle_resize(file, ext, data) ⇒ Object



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

def handle_resize(file, ext, data)
  return unless self.respond_to?(:uploader_size)
  return if store_storage.to_s == "upyun" && upyun_is_image
  total_size = 0

  uploader_size.each do |size, o|
    img = MiniMagick::Image.open(file.path)
    raise ProcessError, "process error" unless img

    convert = MiniMagick::Tool::Convert.new
    convert << file.path

    auto_orient!(img, convert)
    convert.format ext

    resize(convert, img, o)
    cache_file = File.join(cache_path, "#{file.basename}_#{size}.#{ext}")
    convert << cache_file
    convert.call

    processed_img = MiniMagick::Image.open(cache_file)
    data[size] = get_image_data(processed_img)
    total_size += processed_img.size
  end

  return total_size
end

#image_processing(image) ⇒ Object



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
# File 'lib/balloon/processing.rb', line 7

def image_processing(image)
  data = {}
  ext = image.extension

  if respond_to?(:uploader_type_format)
    ext = uploader_type_format
  end

  processed_img = handle_original(image, ext)
  data[:original] = get_image_data(processed_img)

  total_size = handle_resize(image, ext, data)
  data[:total_size] = processed_img.size.to_i + total_size.to_i

  mime_type = processed_img.mime_type
  extension = FileExtension.get_extension(mime_type)
  filename = "#{image.basename}.#{extension}"

  return {
    basename: image.basename,
    width: processed_img.width,
    height: processed_img.height,
    size: processed_img.size,
    filename: filename,
    mime_type: mime_type,
    extension: extension,
    data: data
  }
end

#resize(convert, image, size) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/balloon/processing.rb', line 80

def resize(convert, image, size)
  width, height, symbol = size[:width], size[:height], size[:symbol]

  if !symbol.empty? || width.match(/\%/) || height.match(/\%/)
    if width == height
      shave(convert, image)
      convert.resize "#{width}"
    else
      convert.resize "#{width}x#{height}#{symbol}"
    end
    return
  end

  if width == height
    shave(convert, image)
    value = (width.to_f / image[:width].to_f) * 100
    convert.resize "#{value}%"
    return
  end

  if width.empty?
    value = (height.to_f / image[:height].to_f)  * 100
    convert.resize "#{value}%"
  elsif height.empty?
    value = (width.to_f / image[:width].to_f)  * 100
    convert.resize "#{value}%"
  else
    convert.resize "#{width}x#{height}"
  end
end

#shave(convert, image) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/balloon/processing.rb', line 111

def shave(convert, image)
  w, h = image[:width], image[:height]

  if w > h
    shave_off = ((w - h) / 2).round
    convert.shave "#{shave_off}x0"
    return
  end

  shave_off = ((h - w) / 2).round
  convert.shave "0x#{shave_off}"
end