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
|
# File 'lib/image_magick.rb', line 14
def self.shrink(file,size,type)
temp_file="./temp.#{type}"
original_image = ImageMagick::Image.new(file)
image = original_image.compress(newsize: original_image.width, type: type)
return image.name if image.size < size
low = 0
high = original_image.width
while (high - low) > 1 do
@try_size = ( high + low ) / 2
image = original_image.compress(newsize: @try_size, type: type)
if image.size < size
low = @try_size
else
high = @try_size
end
end
if image.size > size
image = original_image.compress(newsize: high - 1, type: type)
end
end
|