Class: ImgToPdf::Image
- Inherits:
-
Object
- Object
- ImgToPdf::Image
- Defined in:
- lib/img_to_pdf/image.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.from_path(path) ⇒ ImgToPdf::Image
Instance.
Instance Method Summary collapse
- #crop(x, y, width, height) {|sub_image| ... } ⇒ Object
-
#dimension_px ⇒ ImgToPdf::Dimension
Image dimension.
-
#initialize(path) ⇒ Image
constructor
A new instance of Image.
Constructor Details
#initialize(path) ⇒ Image
Returns a new instance of Image.
19 20 21 22 |
# File 'lib/img_to_pdf/image.rb', line 19 def initialize(path) @dimension_px = nil @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/img_to_pdf/image.rb', line 9 def path @path end |
Class Method Details
.from_path(path) ⇒ ImgToPdf::Image
Returns instance.
14 15 16 |
# File 'lib/img_to_pdf/image.rb', line 14 def from_path(path) return new(path) end |
Instance Method Details
#crop(x, y, width, height) {|sub_image| ... } ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/img_to_pdf/image.rb', line 39 def crop(x, y, width, height) Tempfile.create(["img_to_pdf-image-", path.extname]) do |f| f.close sub_raw_image_path = Pathname(f.path) raw_image = MiniMagick::Image.open(path) raw_image.crop("#{width}x#{height}+#{x}+#{y}") raw_image.write(sub_raw_image_path) sub_image = self.class.from_path(sub_raw_image_path) yield(sub_image) end end |
#dimension_px ⇒ ImgToPdf::Dimension
Returns image dimension. pixels.
25 26 27 28 29 30 31 32 |
# File 'lib/img_to_pdf/image.rb', line 25 def dimension_px return @dimension_px if @dimension_px image_klass = Prawn::Images.const_get(path.extname.upcase.sub(/\A\./, "")) image = image_klass.new(path.read) @dimension_px = ImgToPdf::Dimension.new(width: image.width, height: image.height) return @dimension_px end |