Module: ImageMagick

Defined in:
lib/image_magick.rb,
lib/image_magick/image.rb,
lib/image_magick/version.rb

Defined Under Namespace

Classes: Image

Constant Summary collapse

VERSION =
"0.1.9"

Class Method Summary collapse

Class Method Details

.exists?Boolean

Returns:

  • (Boolean)

Raises:

  • (RuntimeError)


9
10
11
12
# File 'lib/image_magick.rb', line 9

def self.exists?
  raise RuntimeError,"convert binary doesn't exist" unless !!find_executable('convert')
  raise RuntimeError,"identify binary doesn't exist" unless !!find_executable('identify')
end

.shrink(file, size, type) ⇒ Object



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