Sha256: df8b94d3273b93d1ed0b23e1c0c88b34e1295f9ea03762434667a5cf87135465

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'fileutils'
require 'oily_png'

module Gnawrnip
  class Image
    #
    # @parma [String] filepath  Screenshot image filepath
    #
    def initialize(filepath)
      @filepath = filepath
      analysis
    end

    def to_html(format = :png)
      width  = width
      height = height
      src    = "data:image/#{format};base64,#{to_base64}"

      %Q(<img width="#{width}" height="#{height}" src="#{src}"/>)
    end

    #
    # @return [Fixnum] Width of image
    #
    def width
      @dimension.width
    end

    #
    # @return [Fixnum] Height of image
    #
    def height
      @dimension.height
    end

    def to_base64
      Base64.strict_encode64(File.read(@filepath))
    end

    def resize(width, height)
      canvas.resample_bilinear(width, height).save(@filepath)
      analysis
    end

    private

      #
      # Update dimension (OilyPNG::Dimension) of Image
      #
      def analysis
        @dimension = canvas.dimension
      end

      def canvas
        OilyPNG::Canvas.from_file(@filepath)
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gnawrnip-0.3.0 lib/gnawrnip/image.rb