module FFaker::Image
def check_format!(format)
def check_format!(format) return true if SUPPORTED_FORMATS.include?(format) raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" end
def check_size!(size)
def check_size!(size) return true if size =~ /\A\d+x\d+\z/ raise ArgumentError, 'Size should be specified in format 300x300' end
def download_file(url)
def download_file(url) file = Tempfile.new('ffaker_image') file.binmode file << Kernel.open(url).read file.close File.new(file.path) end
def file(size = '300x300', format = 'png', bg_color = :random, text_color = :random, text = nil)
def file(size = '300x300', format = 'png', bg_color = :random, text_color = :random, text = nil) download_file(url(size, format, bg_color, text_color, text)) end
def url(size = '300x300', format = 'png', bg_color = :random, text_color = :random, text = nil)
def url(size = '300x300', format = 'png', bg_color = :random, text_color = :random, text = nil) check_size!(size) check_format!(format) bg_color = FFaker::Color.hex_code if bg_color == :random text_color = FFaker::Color.hex_code if text_color == :random text = CGI.escape(text.to_s) "https://via.placeholder.com/#{size}/#{bg_color}/#{text_color}.#{format}?text=#{text}" end