Class: ImageScience

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

Constant Summary collapse

Image =
AppEngine::Images::Image

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ ImageScience

Returns a new instance of ImageScience.



30
31
32
# File 'lib/imagescience.rb', line 30

def initialize(image)
  @image = image
end

Class Method Details

.with_image(path) {|ImageScience.new(Image.open(path))| ... } ⇒ Object

Yields:



22
23
24
# File 'lib/imagescience.rb', line 22

def self.with_image(path)
  yield ImageScience.new(Image.open(path))
end

.with_image_data(data) {|ImageScience.new(Image.new(data))| ... } ⇒ Object

Yields:



26
27
28
# File 'lib/imagescience.rb', line 26

def self.with_image_data(data)
  yield ImageScience.new(Image.new(data))
end

Instance Method Details

#cropped_thumbnail(size) ⇒ Object

:yields: image



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/imagescience.rb', line 44

def cropped_thumbnail(size) # :yields: image
  width, height = self.width, self.height
  left, top, right, bottom, half = 0, 0, width, half, (width - half).abs / 2

  left, right = half, half + height if width > height
  top, bottom = half, half + width if height > width

  with_crop(left, top, right, bottom) do |img|
    img.thumbnail(size) do |thumb|
      yield thumb
    end
  end
end

#heightObject



78
79
80
# File 'lib/imagescience.rb', line 78

def height
  @image.height
end

#resize(width, height) {|@image.resize(width, height)| ... } ⇒ Object

Yields:



58
59
60
# File 'lib/imagescience.rb', line 58

def resize(width, height)
  yield @image.resize(width, height)
end

#save(path) ⇒ Object



34
35
36
37
38
# File 'lib/imagescience.rb', line 34

def save(path)
  File.open(path,"w") do |f|
    f.write @image.to_s
  end
end

#thumbnail(size) ⇒ Object

:yields: image



62
63
64
65
66
67
68
# File 'lib/imagescience.rb', line 62

def thumbnail(size) # :yields: image
  width, height = self.width, self.height
  scale = size.to_f / (width > height ? width : height)
  self.resize((width * scale).to_i, (height * scale).to_i) do |image|
    yield image
  end
end

#to_sObject



40
41
42
# File 'lib/imagescience.rb', line 40

def to_s
  @image.to_s
end

#widthObject



82
83
84
# File 'lib/imagescience.rb', line 82

def width
  @image.width
end

#with_crop(left, top, right, bottom) {|ImageScience.new(cropped)| ... } ⇒ Object

Yields:



70
71
72
73
74
75
76
# File 'lib/imagescience.rb', line 70

def with_crop(left, top, right, bottom)
  cropped = @image.crop(left.to_f / width.to_f,
                        top.to_f / height.to_f ,
                        right.to_f / width.to_f,
                        bottom.to_f / height.to_f)
  yield ImageScience.new(cropped)
end