Class: ImageLimitResize::Base
- Inherits:
-
Object
- Object
- ImageLimitResize::Base
- Defined in:
- lib/image_limit_resize.rb
Overview
image magick limit resize author @kanayannet
Constant Summary collapse
- FORMAT_PATTERN =
/^jpeg$|^gif$|^png$/i
Instance Attribute Summary collapse
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #format ⇒ Object
-
#initialize(file: nil, buffer: nil) ⇒ Base
constructor
A new instance of Base.
- #resize(file) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(file: nil, buffer: nil) ⇒ Base
Returns a new instance of Base.
8 9 10 11 12 |
# File 'lib/image_limit_resize.rb', line 8 def initialize(file: nil, buffer: nil) @img = Magick::Image.read(file).first if !file.nil? && File.exist?(file) @img = Magick::Image.from_blob(buffer).shift unless buffer.nil? @size = nil end |
Instance Attribute Details
#size ⇒ Object
Returns the value of attribute size.
13 14 15 |
# File 'lib/image_limit_resize.rb', line 13 def size @size end |
Instance Method Details
#format ⇒ Object
15 16 17 |
# File 'lib/image_limit_resize.rb', line 15 def format @img.format end |
#resize(file) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/image_limit_resize.rb', line 26 def resize(file) valid? @img.auto_orient! @img.strip! width = @img.columns height = @img.rows size = width_height(width, height) image = @img.resize(size[:width], size[:height]) image.write(file) end |
#valid? ⇒ Boolean
19 20 21 22 23 24 |
# File 'lib/image_limit_resize.rb', line 19 def valid? raise 'buffer and file is nil' if @img.nil? raise "size:#{@size} value is not Integer" unless @size.is_a?(Integer) raise "#{@img.format} can't allow" if FORMAT_PATTERN !~ @img.format raise 'size 0 can\'not allow' if @size <= 0 end |