Class: ImageScience

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

Constant Summary collapse

VERSION =
"1.1.7"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dpi_to_dpm(dpi) ⇒ Object

Convert a number from dots-per-inch to dots-per-meter



157
158
159
# File 'lib/image_science.rb', line 157

def self.dpi_to_dpm(dpi)
  (dpi.to_f * (1.to_f/(2.54).to_f) * (100.to_f/1.to_f)).to_f
end

.dpm_to_dpi(dpm) ⇒ Object

Convert a number from dots-per-meter to dots-per-inch



151
152
153
# File 'lib/image_science.rb', line 151

def self.dpm_to_dpi(dpm)
  (dpm.to_f * (1.to_f/100.to_f) * ((2.54).to_f/1.to_f)).to_f
end

.image_type(path) ⇒ Object

Returns the type of the image as a string.



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

def self.image_type(path)
  fif_to_string(file_type(path))
end

Instance Method Details

#[](x, y) ⇒ Object

call-seq:

img[x, y] -> index
img[x, y] -> [red, green, blue]

alias for get_pixel_color



134
135
136
# File 'lib/image_science.rb', line 134

def [](x, y)
  get_pixel_color(x, y)
end

#[]=(x, y, *args) ⇒ Object

call-seq:

img[x, y] = index
img[x, y] = [red, green, blue]

alias for set_pixel_color



144
145
146
# File 'lib/image_science.rb', line 144

def []=(x, y, *args)
  set_pixel_color(x, y, *args)
end

#colorspaceObject

Returns the colorspace of the image as a string



24
25
26
27
28
29
30
31
32
33
# File 'lib/image_science.rb', line 24

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

#colourspaceObject

:nodoc:



35
36
37
# File 'lib/image_science.rb', line 35

def colourspace # :nodoc:
  colorspace
end

#cropped_thumbnail(size) ⇒ Object

call-seq:

cropped_thumbnail(size)
cropped_thumbnail(size) { |image| ... }

Creates a square thumbnail of the image cropping the longest edge to match the shortest edge, resizes to size. If a block is given, yields the new image, else returns true on success.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/image_science.rb', line 79

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

  if block_given?
    with_crop(l, t, r, b) do |img|
      img.thumbnail(size) do |thumb|
        yield thumb
      end
    end
  else
    crop(l, t, r, b) && thumbnail(size)
  end
end

#data(*args) ⇒ Object

alias for buffer()



42
43
44
# File 'lib/image_science.rb', line 42

def data(*args)
  buffer(*args)
end

#dpiObject

Returns the DPI of image, as hash of float values



175
176
177
# File 'lib/image_science.rb', line 175

def dpi
  {:x=>dpi_x,:y=>dpi_y}
end

#dpi_xObject

Returns the DPI of x-axis (width) of the image, in dots-per-inch as float



163
164
165
# File 'lib/image_science.rb', line 163

def dpi_x
  self.class.dpm_to_dpi(dpm_x)
end

#dpi_yObject

Returns the DPI of y-axis (height) of the image, in dots-per-inch as float



169
170
171
# File 'lib/image_science.rb', line 169

def dpi_y
  self.class.dpm_to_dpi(dpm_y)
end

#fit_within(max_w, max_h) ⇒ Object

resize the image to fit within the max_w and max_h passed in without changing the aspect ratio of the original image. If a block is given, yields the new image, else returns true on success.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/image_science.rb', line 102

def fit_within(max_w, max_h)
  w, h = width, height

  if w > max_w.to_i or h > max_h.to_i

    w_ratio = max_w.quo(w)
    h_ratio = max_h.quo(h)

    if (w_ratio < h_ratio)
      h = (h * w_ratio)
      w = (w * w_ratio)
    else
      h = (h * h_ratio)
      w = (w * h_ratio)
    end
  end

  if block_given?
    self.resize(w, h) do |image|
      yield image
    end
  else
    self.resize(w, h)
  end
end

#image_typeObject

Returns the type of the image as a string.



17
18
19
# File 'lib/image_science.rb', line 17

def image_type
  ImageScience.fif_to_string(@file_type)
end

#thumbnail(size) ⇒ Object

call-seq:

thumbnail(size)
thumbnail(size) { |image| ... }

Creates a proportional thumbnail of the image scaled so its longest edge is resized to size. If a block is given, yields the new image, else returns true on success.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/image_science.rb', line 55

def thumbnail(size)
  w, h = width, height
  scale = size.to_f / (w > h ? w : h)
  w = (w * scale).to_i
  h = (h * scale).to_i

  if block_given?
    self.resize(w, h) do |image|
      yield image
    end
  else
    self.resize(w, h)
  end
end