class Html2Odt::Image

Attributes

source[R]

Public Class Methods

new(target_base) click to toggle source
# File lib/html2odt/image.rb, line 4
def initialize(target_base)
  @target_base = target_base
  @valid = nil
end

Public Instance Methods

angle() click to toggle source
# File lib/html2odt/image.rb, line 85
def angle
  return unless valid?

  @angle
end
extension() click to toggle source
# File lib/html2odt/image.rb, line 57
def extension
  return unless valid?

  if type == :jpeg
    "jpg"
  else
    type
  end
end
height() click to toggle source
# File lib/html2odt/image.rb, line 79
def height
  return unless valid?

  @height
end
mime_type() click to toggle source
# File lib/html2odt/image.rb, line 67
def mime_type
  return unless valid?

  "image/#{type}"
end
source=(file_or_path) click to toggle source

Assign file instead of source, if you were creating tempfiles and need them to stay around until the ODT is generated.

# File lib/html2odt/image.rb, line 11
def source=(file_or_path)
  if file_or_path.respond_to? :path
    @source = file_or_path.path
    @file = file_or_path
  else
    @source = file_or_path.to_s
  end
end
target() click to toggle source
# File lib/html2odt/image.rb, line 91
def target
  return unless valid?

  "Pictures/#{@target_base}.#{extension}"
end
type() click to toggle source
# File lib/html2odt/image.rb, line 53
def type
  valid? ? @type : nil
end
valid?() click to toggle source
# File lib/html2odt/image.rb, line 20
def valid?
  return false if source.nil? or !File.readable?(source) or !File.file?(source)

  if @valid.nil?
    File.open(source, "rb") do |io|
      Dimensions(io)
      io.extend Html2Odt::DimensionsPatches

      # Interacting with Dimensions::Reader directly to
      #
      #   a) avoid reading the file multiple times
      #   b) get type info

      io.send :peek
      reader = io.instance_variable_get :@reader

      if reader.type && reader.width && reader.height
        @type = reader.type

        @width  = reader.width
        @height = reader.height
        @angle  = reader.angle

        @valid = true
      else
        @valid = false
      end
    end
  end

  @valid
end
width() click to toggle source
# File lib/html2odt/image.rb, line 73
def width
  return unless valid?

  @width
end