Class: PixScale::Pic
- Inherits:
-
Object
- Object
- PixScale::Pic
- Defined in:
- lib/pix_scale/pic.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(path) ⇒ Pic
constructor
A new instance of Pic.
- #save(output_path, type = nil) ⇒ Object
- #scale(scale) ⇒ Object
- #scale!(scale) ⇒ Object
- #scale_and_save(scale, type = nil) ⇒ Object
Constructor Details
#initialize(path) ⇒ Pic
Returns a new instance of Pic.
14 15 16 17 18 |
# File 'lib/pix_scale/pic.rb', line 14 def initialize(path) @path = path @pic = Gdk::Pixbuf.new(path) @type = (/\A\.jpg\z/i =~ extname) ? "jpeg" : extname.sub(/^\./, "").downcase end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/pix_scale/pic.rb', line 13 def path @path end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
13 14 15 |
# File 'lib/pix_scale/pic.rb', line 13 def type @type end |
Class Method Details
.scale_and_save(path, scale, type = nil) ⇒ Object
9 10 11 |
# File 'lib/pix_scale/pic.rb', line 9 def self.scale_and_save(path, scale, type=nil) new(path).scale_and_save(scale, type) end |
Instance Method Details
#save(output_path, type = nil) ⇒ Object
57 58 59 60 |
# File 'lib/pix_scale/pic.rb', line 57 def save(output_path, type=nil) output_type = type || @type @pic.save(output_path, output_type) end |
#scale(scale) ⇒ Object
28 29 30 31 32 |
# File 'lib/pix_scale/pic.rb', line 28 def scale(scale) new_pic = self.dup new_pic.scale!(scale) new_pic end |
#scale!(scale) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pix_scale/pic.rb', line 34 def scale!(scale) if scale.is_a?(Float) width = @pic.width * scale height = @pic.height * scale elsif scale.is_a?(Integer) width = scale height = @pic.height * (scale.to_f / @pic.width) elsif /\A[0-9]+\.[0-9]+\z/ =~ scale width = @pic.width * scale.to_f height = @pic.height * scale.to_f elsif /\A[0-9]+[^\.0-9][0-9]+\z/ =~ scale width, height = scale.split(/[^\.0-9]/).map(&:to_i) elsif /\A[0-9]+\z/ =~ scale width = scale.to_i height = @pic.height * (scale.to_f / @pic.width) elsif /\A[^\.0-9]([0-9]+)\z/ =~ scale width = @pic.width * ($1.to_f / @pic.height) height = $1.to_i end @pic = @pic.scale(width, height) end |
#scale_and_save(scale, type = nil) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/pix_scale/pic.rb', line 20 def scale_and_save(scale, type=nil) scale_string = scale.to_s.sub(/[^\.0-9]/, "_") extention = type || extname extention = ".#{extention}" unless /\A\./ =~ extention output_path = "#{dirname}/#{basename}-#{scale_string}#{extention}" scale(scale).save(output_path, type) end |