Class: ImageScience

Inherits:
Object
  • Object
show all
Defined in:
lib/image_science.rb

Overview

Provides a clean and simple API to generate thumbnails using FreeImage as the underlying mechanism.

For more information or if you have build issues with FreeImage, see seattlerb.rubyforge.org/ImageScience.html

Constant Summary collapse

VERSION =
'1.2.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.image_type(path) ⇒ Object

Returns the type of the image.



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
60
61
62
63
64
65
66
# File 'lib/image_science.rb', line 31

def self.image_type(path)
  case file_type(path)
    when 0 then 'BMP'
    when 1 then 'ICO'
    when 2 then 'JPEG'
    when 3 then 'JNG'
    when 4 then 'KOALA'
    when 5 then 'IFF'
    when 6 then 'MNG'
    when 7 then 'PBM'
    when 8 then 'PBMRAW'
    when 9 then 'PCD'
    when 10 then 'PCX'
    when 11 then 'PGM'
    when 12 then 'PGMRAW'
    when 13 then 'PNG'
    when 14 then 'PPM'
    when 15 then 'PPMRAW'
    when 16 then 'RAS'
    when 17 then 'TARGA'
    when 18 then 'TIFF'
    when 19 then 'WBMP'
    when 20 then 'PSD'
    when 21 then 'CUT'
    when 22 then 'XBM'
    when 23 then 'XPM'
    when 24 then 'DDS'
    when 25 then 'GIF'
    when 26 then 'HDR'
    when 27 then 'FAXG3'
    when 28 then 'SGI'
    when 29 then 'EXR'
    when 30 then 'J2K'
    when 31 then 'JP2'
  end
end

.with_image(path) ⇒ Object

The top-level image loader opens path and then yields the image.



19
20
# File 'lib/image_science.rb', line 19

def self.with_image(path) # :yields: image
end

.with_image_from_memory(data) ⇒ Object

The top-level image loader, opens an image from the string data and then yields the image.



25
26
# File 'lib/image_science.rb', line 25

def self.with_image_from_memory(data) # :yields: image
end

Instance Method Details

#colorspaceObject

Returns the size of one pixel of the image. Possible bit depths are 1, 4, 8, 16, 24, 32 for standard bitmaps and 16, 32, 48, 64, 96 and 128 bits for non standard bitmaps.



97
98
99
100
101
102
103
104
105
106
# File 'lib/image_science.rb', line 97

def colorspace
  case colortype
    when 0 then depth == 1 ? 'InvertedMonochrome' : 'InvertedGrayscale'
    when 1 then depth == 1 ? 'Monochrome' : 'Grayscale'
    when 2 then 'RGB'
    when 3 then 'Indexed'
    when 4 then 'RGBA'
    when 5 then 'CMYK'
  end
end

#cropped_thumbnail(size) ⇒ Object

Creates a square thumbnail of the image cropping the longest edge to match the shortest edge, resizes to size, and yields the new image.



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/image_science.rb', line 139

def cropped_thumbnail(size) # :yields: image
  w, h = width, height
  l, t, r, b, half = 0, 0, w, h, (w - h).abs / 2

  l, r = half, half + h if w > h
  t, b = half, half + w if h > w

  with_crop(l, t, r, b) do |img|
    img.thumbnail(size) do |thumb|
      yield thumb
    end
  end
end

#depthObject

Returns the size of one pixel of the image. Possible bit depths are 1, 4, 8, 16, 24, 32 for standard bitmaps and 16, 32, 48, 64, 96 and 128 bits for non standard bitmaps.



90
# File 'lib/image_science.rb', line 90

def depth; end

#heightObject

Returns the height of the image, in pixels.



83
# File 'lib/image_science.rb', line 83

def height; end

#resize(width, height) ⇒ Object

Resizes the image to width and height using a cubic-bspline filter and yields the new image.



118
119
# File 'lib/image_science.rb', line 118

def resize(width, height) # :yields: image
end

#save(path) ⇒ Object

Saves the image out to path. Changing the file extension will convert the file type to the appropriate format.



112
# File 'lib/image_science.rb', line 112

def save(path); end

#thumbnail(size) ⇒ Object

Creates a proportional thumbnail of the image scaled so its longest edge is resized to size and yields the new image.



125
126
127
128
129
130
131
132
# File 'lib/image_science.rb', line 125

def thumbnail(size) # :yields: image
  w, h = width, height
  scale = size.to_f / (w > h ? w : h)

  self.resize((w * scale).to_i, (h * scale).to_i) do |image|
    yield image
  end
end

#widthObject

Returns the width of the image, in pixels.



78
# File 'lib/image_science.rb', line 78

def width; end

#with_crop(left, top, right, bottom) ⇒ Object

Crops an image to left, top, right, and bottom and then yields the new image.



72
73
# File 'lib/image_science.rb', line 72

def with_crop(left, top, right, bottom) # :yields: image
end