Sha256: b1a7d7149359bc907aa4063fdc802a1a18a27e94331f9ff7b61f754baab1ef01

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

module PDFToImage

  class Image
    attr_reader :filename
    attr_reader :width
    attr_reader :height
    attr_reader :format
    attr_reader :page
    attr_reader :args

    # We currently only support resizing, as that's all I need at the moment
    # selfish, but I need to return to the main project
    CUSTOM_IMAGE_METHODS = [
      "resize"
    ]

    CUSTOM_IMAGE_METHODS.each do |method|
      define_method(method.to_sym) do |*args|
        @args << "-#{method} #{args.join(' ')}"

        self
      end
    end

    # Image constructor
    #
    # @param [filename] The name of the image file to open
    def initialize(filename)
      @args = []

      @filename = filename

      info = identify()

      @width = info[:width]
      @height = info[:height]
      @format = info[:format]

      tmp_base = File.basename(filename, File.extname(filename))
      pieces = tmp_base.split('-')
      @page = pieces[-1].to_i
    end

    # Saves the converted image to the specified location
    #
    # @param [outname] The output filename of the image
    #
    def save(outname)
      cmd = "convert "

      if not @args.empty?
        cmd += "#{@args.join(' ')} "
      end
      
      cmd += "#{@filename} #{outname}"

      `#{cmd}`
      if $? != 0
        raise PDFError, "Error converting file: #{cmd}"
      end

      return true
    end

    def <=>(img)
      if @page == img.page
        return 0
      elsif @page < img.page
        return -1
      else
        return 1
      end
    end

    private 

    def identify
      cmd = "identify #{@filename}"

      result = `#{cmd}`
      unless $?.success?
        raise PDFToImage::PDFError, "Error executing #{cmd}"
      end

      info = result.strip.split(' ')
      dimensions = info[2].split('x')

      return {
        :format => info[1],
        :width => dimensions[0],
        :height => dimensions[1]
      }
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pdftoimage-0.1.1 lib/pdftoimage/image.rb