# encoding: BINARY # frozen_string_literal: true require 'image_size/reader' require 'image_size/seekable_io_reader' require 'image_size/stream_io_reader' require 'image_size/string_reader' # Determine image format and size class ImageSize class FormatError < StandardError; end # Array joining with 'x' class Size < Array # join using 'x' def to_s join('x') end # get first element def width self[0] end alias_method :w, :width # get second element def height self[1] end alias_method :h, :height end # Given path to image finds its format, width and height def self.path(path) new(Pathname.new(path)) end # Used for svg def self.dpi @dpi || 72 end # Used for svg def self.dpi=(dpi) @dpi = dpi.to_f end # Given image as any class responding to read and eof? or data as String, finds its format and dimensions def initialize(data) Reader.open(data) do |ir| @format = detect_format(ir) @width, @height = send("size_of_#{@format}", ir) if @format end end # Image format attr_reader :format # Image width attr_reader :width alias_method :w, :width # Image height attr_reader :height alias_method :h, :height # get image width and height as an array which to_s method returns "#{width}x#{height}" def size Size.new([width, height]) if format end private SVG_R = /]*)>/.freeze XML_R = /<\?xml|