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

Based on ImageScience 1.2.1

Constant Summary collapse

VERSION =
'1.2.10'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.with_image(path) ⇒ Object

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



21
22
# File 'lib/image_science.rb', line 21

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.



27
28
# File 'lib/image_science.rb', line 27

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

Instance Method Details

#buffer(extension) ⇒ Object

Returns the image in a buffer (String). Changing the file extension converts the file type to the appropriate format. Note: returns! Does not yield!



58
59
# File 'lib/image_science.rb', line 58

def buffer(extension) # :returns: string
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.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/image_science.rb', line 86

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

#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



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

def fit_within(max_w, max_h) # :yields: image
  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

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

#heightObject

Returns the height of the image, in pixels.



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

def height; end

#resize(width, height) ⇒ Object

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



65
66
# File 'lib/image_science.rb', line 65

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

#resize_with_crop(width, height, &block) ⇒ Object

Resizes an image to the specified size without stretching or compressing the original. If the aspect ratio of the new height/width does not match the aspect ratio of the original (as when converting portrait to landscape or landscape to portrait), the resulting image will be cropped. Cropping preserves the center of the image, with content trimmed evenly from the top and bottom and/or left and right edges of the image. This can cause some less than ideal conversions. For example, converting a portrait to a landscape can result in the portrait’s head being cut off.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/image_science.rb', line 137

def resize_with_crop(width, height, &block)

  # ---------------------------------------------------------------
  # We want to adjust both height and width by the same ratio,
  # so the image is not stretched. Adjust everything by the
  # larger ratio, so that portrait to landscape and landscape
  # to portrait transformations come out right.
  # ---------------------------------------------------------------
  src2target_height_ratio = height.to_f / self.height
  src2target_width_ratio = width.to_f / self.width
  height_ratio_is_larger = src2target_height_ratio > src2target_width_ratio
  
  if height_ratio_is_larger
    target_height = (self.height * src2target_height_ratio).round
    target_width = (self.width * src2target_height_ratio).round
  else
    target_height = (self.height * src2target_width_ratio).round
    target_width = (self.width * src2target_width_ratio).round
  end

  # ---------------------------------------------------------------
  # Create a version of this image whose longest
  # side is equal to max_dimension. We'll add two
  # to this value, since floating point arithmetic
  # often produces values 1-2 pixels short of what we want.
  # ---------------------------------------------------------------
  max_dimension = (target_height > target_width ?
                   target_height : target_width)

  self.thumbnail(max_dimension + 2) do |img1|
    top, left = 0, 0
    top = (img1.height - height) / 2 unless img1.height < height
    left = (img1.width - width) / 2 unless img1.width < width
    right = width + left
    bottom = height + top

    # ---------------------------------------------------------------
    # Crop the resized image evenly at top/bottom, left/right,
    # so that we preserve the center.
    # ---------------------------------------------------------------
    result = img1.with_crop(left, top, right, bottom) do |img2|
      if block_given?
        yield img2
      end
      img2
    end

    if result.nil?
      message = "Crop/resize failed... is some dimension is out of bounds?"
      message += "Original Height = #{self.height}, Width = #{self.width}"
      message += "Target Height   = #{height}, Width = #{width}"
      message += "Actual Height   = #{self.height}, Width = #{self.width}"
      message += "Left=#{left}, Top=#{top}, Right=#{right}, Bottom=#{bottom}"
      raise message
    end
  end
end

#save(path) ⇒ Object

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



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

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.



72
73
74
75
76
77
78
79
# File 'lib/image_science.rb', line 72

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.



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

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.



34
35
# File 'lib/image_science.rb', line 34

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