Class: Sequel::Plugins::Paperclip::Processors::Image::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_paperclip/processors/image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width = nil, height = nil, modifiers = nil) ⇒ Geometry

Returns a new instance of Geometry.



64
65
66
67
68
# File 'lib/sequel_paperclip/processors/image.rb', line 64

def initialize(width = nil, height = nil, modifiers = nil)
  self.width = width.to_f
  self.height = height.to_f
  self.modifiers = modifiers
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



62
63
64
# File 'lib/sequel_paperclip/processors/image.rb', line 62

def height
  @height
end

#modifiersObject

Returns the value of attribute modifiers.



62
63
64
# File 'lib/sequel_paperclip/processors/image.rb', line 62

def modifiers
  @modifiers
end

#widthObject

Returns the value of attribute width.



62
63
64
# File 'lib/sequel_paperclip/processors/image.rb', line 62

def width
  @width
end

Class Method Details

.from_file(file) ⇒ Object



70
71
72
73
74
# File 'lib/sequel_paperclip/processors/image.rb', line 70

def self.from_file(file)
  path = file.respond_to?(:path) ? file.path : file
  geometry = `identify -format %wx%h #{path}`
  from_s(geometry)
end

.from_s(string) ⇒ Object



76
77
78
79
80
81
# File 'lib/sequel_paperclip/processors/image.rb', line 76

def self.from_s(string)
  match = string.match(/(\d+)x(\d+)(.*)/)
  if match
    Geometry.new(match[1], match[2], match[3])
  end
end

Instance Method Details

#aspectObject



99
100
101
# File 'lib/sequel_paperclip/processors/image.rb', line 99

def aspect
  width/height
end

#cropping(dst, ratio, scale) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/sequel_paperclip/processors/image.rb', line 131

def cropping(dst, ratio, scale)
  if ratio.horizontal? || ratio.square?
    "%dx%d+%d+%d"%[dst.width, dst.height, 0, (self.height*scale-dst.height)/2]
  else
    "%dx%d+%d+%d"%[dst.width, dst.height, (self.width*scale-dst.width)/2, 0]
  end
end

#horizontal?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/sequel_paperclip/processors/image.rb', line 91

def horizontal?
  height < width
end

#largerObject



103
104
105
# File 'lib/sequel_paperclip/processors/image.rb', line 103

def larger
  [height, width].max
end

#scaling(dst, ratio) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/sequel_paperclip/processors/image.rb', line 123

def scaling(dst, ratio)
  if ratio.horizontal? || ratio.square?
    ["%dx"%dst.width, ratio.width]
  else
    ["x%d"%dst.height, ratio.height]
  end
end

#smallerObject



107
108
109
# File 'lib/sequel_paperclip/processors/image.rb', line 107

def smaller
  [height, width].min
end

#square?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/sequel_paperclip/processors/image.rb', line 87

def square?
  height == width
end

#to_sObject



83
84
85
# File 'lib/sequel_paperclip/processors/image.rb', line 83

def to_s
  "%dx%d"%[self.width, self.height]+self.modifiers
end

#transform(dst, crop = false) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sequel_paperclip/processors/image.rb', line 111

def transform(dst, crop = false)
  if crop
    ratio = Geometry.new(dst.width/self.width, dst.height/self.height)
    scale_geometry, scale = scaling(dst, ratio)
    crop_geometry = cropping(dst, ratio, scale)
  else
    scale_geometry = dst.to_s
    crop_geometry = nil
  end
  [scale_geometry, crop_geometry]
end

#vertical?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/sequel_paperclip/processors/image.rb', line 95

def vertical?
  height > width
end