Module: Shrink

Defined in:
lib/image_magick/shrink.rb

Class Method Summary collapse

Class Method Details

.shrink(file, size, type) ⇒ Object



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
# File 'lib/image_magick/shrink.rb', line 5

def self.shrink(file,size,type)

  temp_file="./temp.#{type}"

  p "file exists?"
  p File.exist?(file)
  original_image = Image.new(file)

  p original_image
  image = original_image.compress(newsize: original_image.width, type: type)
  exit if image.size < @new_size

  low  = 0
  high = original_image.width 

  while (high - low) > 1 do 
    @try_size = ( high + low  ) / 2
    p "low: #{low}, high #{high}, try #{@try_size}"
    p "reducing to (#{@try_size})"
    image = original_image.compress(newsize: @try_size, type: type)
    p image.size
    if image.size < @new_size 
      low = @try_size
    else
      high = @try_size
    end
  end

  if image.size > @new_size
    image = original_image.compress(newsize: high - 1, type: type)
  end

end