Class: CropToelie

Inherits:
Object
  • Object
show all
Includes:
Magick
Defined in:
lib/croptoelie.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ CropToelie

Create a new CropToelie object from a ImageList single image object.

If you want to provide a file by its path use CropToelie.from_file('/path/to/image.png').


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/croptoelie.rb', line 10

def initialize(image)
  @image = image

  # Hardcoded (but overridable) defaults.
  @steps  = 10

  # Preprocess image.
  @image = @image.quantize

  # Prepare some often-used internal variables.
  @rows = @image.rows
  @columns = @image.columns

  warn "DEPRECATED: 'croptoelie' gem is replaced by 'smartcropper'. http://github.com/berkes/smartcropper"
end

Instance Attribute Details

#origObject

Returns the value of attribute orig.



5
6
7
# File 'lib/croptoelie.rb', line 5

def orig
  @orig
end

#stepsObject

Returns the value of attribute steps.



6
7
8
# File 'lib/croptoelie.rb', line 6

def steps
  @steps
end

Class Method Details

.from_file(image_path) ⇒ Object

Open create a croptoelie from a file on disk.



27
28
29
30
# File 'lib/croptoelie.rb', line 27

def self.from_file(image_path)
  image = ImageList.new(image_path).last
  return CropToelie.new(image)
end

Instance Method Details

#smart_crop(width, height) ⇒ Object

Crops an image to width x height



33
34
35
36
# File 'lib/croptoelie.rb', line 33

def smart_crop(width, height)
  sq = square(width, height)
  return @image.crop!(sq[:left], sq[:top], width, height, true)
end

#smart_crop_and_scale(width, height) ⇒ Object

Squares an image (with smart_square) and then scales that to width, heigh



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

def smart_crop_and_scale(width, height)
  smart_square
  return @image.scale!(width, height)
end

#smart_squareObject

Squares an image by slicing off the least interesting parts. Usefull for squaring images such as thumbnails. Usefull before scaling.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/croptoelie.rb', line 46

def smart_square
  if @rows != @columns #None-square images must be shaved off.
    if @rows < @columns #landscape
      crop_height = crop_width = @rows
    else # portrait
      crop_height = crop_width = @columns
    end

    sq = square(crop_width, crop_height)
    @image.crop!(sq[:left], sq[:top], crop_width, crop_height, true)
  end

  @image
end

#square(width, height) ⇒ Object

Finds the most interesting square with size width x height.

Returns a hash => left, :top => top, :right => right, :bottom => bottom



64
65
66
# File 'lib/croptoelie.rb', line 64

def square(width, height)
  return smart_crop_by_trim(width, height)
end