Class: ImgToPdf::Image

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#pathObject (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.

Parameters:

  • path (Pathname)

    path to instantiate.

Returns:



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

Parameters:

  • x (Numeric)
  • y (Numeric)
  • width (Numeric)
  • height (Numeric)

Yield Parameters:



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_pxImgToPdf::Dimension

Returns image dimension. pixels.

Returns:



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