start_regex = /data:image/[a-z]{3,4};base64,/
filename ||= SecureRandom.hex
regex_result = start_regex.match(base64_data)
if base64_data && regex_result
start = regex_result.to_s
tempfile = Tempfile.new(filename)
tempfile.binmode
tempfile.write(Base64.decode64(base64_data[start.length..-1]))
end